-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescueTimeApi.py
32 lines (30 loc) · 1.22 KB
/
rescueTimeApi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import datetime
import requests
import json
from models import ElectronicActivity, ElectronicActivityHistory
def call(request, start, end):
url = 'https://www.rescuetime.com/anapi/data'
params = {
'key': request['token'],
'format': 'json',
'perspective': 'interval',
'resolution_time': 'minute',
'restrict_begin': start.isoformat(),
'restrict_end': end.isoformat(),
'restrict_kind': 'document'
}
response = json.loads(requests.get(url, params=params).text)
if 'error' in response:
raise Exception(response['error'])
items = response['rows']
headers = dict((x, i) for i, x in enumerate(response['row_headers']))
for item in items:
page_title = item[headers['Document']]
if page_title == "No Details":
page_title = None
yield ElectronicActivityHistory(
timestamp=datetime.datetime.fromisoformat(item[headers['Date']]),
activity=ElectronicActivity(name=item[headers['Activity']], productivity_score=int(item[headers['Productivity']]), category=item[headers['Category']]),
duration=int(item[headers['Time Spent (seconds)']]),
page_title=page_title
)