diff options
Diffstat (limited to 'overlays/worktime')
-rwxr-xr-x | overlays/worktime/worktime.py | 25 |
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']) |