From 36f9b3f3039832152ba4bf554d6ad5180017a470 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 11 Apr 2021 17:42:04 +0200 Subject: worktime: work around api restrictions --- overlays/worktime/worktime.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'overlays/worktime') 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): api_path = api.value section_path = '/'.join(section) - uri = uricompose(scheme='https', host='www.toggl.com', path=f"{api_path}/{section_path}", query=params) + uri = uricompose(scheme='https', host='api.track.toggl.com', path=f"{api_path}/{section_path}", query=params) return uri @@ -73,12 +73,23 @@ class TogglAPI(object): return response def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc), rounding=False): - 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}) - r = self._query(url = url, method='GET') - if not r or not r.json(): - raise TogglAPIError(r) - - return timedelta(milliseconds=r.json()['total_billable']) if r.json()['total_billable'] else timedelta(milliseconds=0) + billable_acc = timedelta(milliseconds = 0) + step = timedelta(days = 365) + + for req_start in [start_date + x * step for x in range(0, ceil((end_date - start_date) / step))]: + req_end = end_date + if end_date > req_start + step: + req_end = datetime.combine((req_start + step).astimezone(timezone.utc).date(), time(tzinfo=timezone.utc)) + elif req_start > start_date: + req_start = datetime.combine(req_start.astimezone(timezone.utc).date(), time(tzinfo=timezone.utc)) + timedelta(days = 1) + + 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}) + r = self._query(url = url, method='GET') + if not r or not r.json(): + raise TogglAPIError(r) + billable_acc += timedelta(milliseconds=r.json()['total_billable']) if r.json()['total_billable'] else timedelta(milliseconds=0) + + return billable_acc def get_running_clock(self, now=datetime.now(timezone.utc)): url = self._make_url(api = TogglAPISection.TOGGL, section = ['time_entries', 'current']) -- cgit v1.2.3