summaryrefslogtreecommitdiff
path: root/overlays
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2021-04-11 17:42:04 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2021-04-11 17:42:04 +0200
commit36f9b3f3039832152ba4bf554d6ad5180017a470 (patch)
treeefe33a590085de691dd7585851e1bf881ba96adc /overlays
parenta58b96622154b0dc341c77e0f6229e16313e0c6c (diff)
downloadnixos-36f9b3f3039832152ba4bf554d6ad5180017a470.tar
nixos-36f9b3f3039832152ba4bf554d6ad5180017a470.tar.gz
nixos-36f9b3f3039832152ba4bf554d6ad5180017a470.tar.bz2
nixos-36f9b3f3039832152ba4bf554d6ad5180017a470.tar.xz
nixos-36f9b3f3039832152ba4bf554d6ad5180017a470.zip
worktime: work around api restrictions
Diffstat (limited to 'overlays')
-rwxr-xr-xoverlays/worktime/worktime.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/overlays/worktime/worktime.py b/overlays/worktime/worktime.py
index b9af430a..c7b013f9 100755
--- a/overlays/worktime/worktime.py
+++ b/overlays/worktime/worktime.py
@@ -52,7 +52,7 @@ class TogglAPI(object):
52 52
53 api_path = api.value 53 api_path = api.value
54 section_path = '/'.join(section) 54 section_path = '/'.join(section)
55 uri = uricompose(scheme='https', host='www.toggl.com', path=f"{api_path}/{section_path}", query=params) 55 uri = uricompose(scheme='https', host='api.track.toggl.com', path=f"{api_path}/{section_path}", query=params)
56 56
57 return uri 57 return uri
58 58
@@ -73,12 +73,23 @@ class TogglAPI(object):
73 return response 73 return response
74 74
75 def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc), rounding=False): 75 def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc), rounding=False):
76 url = self._make_url(api = TogglAPISection.REPORTS, section = ['summary'], params={'since': start_date.astimezone(timezone.utc).isoformat(), 'until': end_date.astimezone(timezone.utc).isoformat(), 'rounding': rounding}) 76 billable_acc = timedelta(milliseconds = 0)
77 r = self._query(url = url, method='GET') 77 step = timedelta(days = 365)
78 if not r or not r.json(): 78
79 raise TogglAPIError(r) 79 for req_start in [start_date + x * step for x in range(0, ceil((end_date - start_date) / step))]:
80 80 req_end = end_date
81 return timedelta(milliseconds=r.json()['total_billable']) if r.json()['total_billable'] else timedelta(milliseconds=0) 81 if end_date > req_start + step:
82 req_end = datetime.combine((req_start + step).astimezone(timezone.utc).date(), time(tzinfo=timezone.utc))
83 elif req_start > start_date:
84 req_start = datetime.combine(req_start.astimezone(timezone.utc).date(), time(tzinfo=timezone.utc)) + timedelta(days = 1)
85
86 url = self._make_url(api = TogglAPISection.REPORTS, section = ['summary'], params={'since': req_start.astimezone(timezone.utc).isoformat(), 'until': req_end.astimezone(timezone.utc).isoformat(), 'rounding': rounding})
87 r = self._query(url = url, method='GET')
88 if not r or not r.json():
89 raise TogglAPIError(r)
90 billable_acc += timedelta(milliseconds=r.json()['total_billable']) if r.json()['total_billable'] else timedelta(milliseconds=0)
91
92 return billable_acc
82 93
83 def get_running_clock(self, now=datetime.now(timezone.utc)): 94 def get_running_clock(self, now=datetime.now(timezone.utc)):
84 url = self._make_url(api = TogglAPISection.TOGGL, section = ['time_entries', 'current']) 95 url = self._make_url(api = TogglAPISection.TOGGL, section = ['time_entries', 'current'])