-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_toggl.py
213 lines (175 loc) · 7 KB
/
class_toggl.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import urllib.parse
import requests
import configparser
from datetime import datetime
from tools import get_date_range, milliseconds_to_str, taskdatetime_to_datetime, milliseconds_to_hours_decimal
# https://github.com/toggl/toggl_api_docs/blob/master/reports.md
class TogglClient:
def __init__(self):
self.version = 1
self.config = configparser.ConfigParser()
self.config.read('config.ini')
self.workspace_id = self.config['toggl']['workspace_id']
self.api_token = self.config['toggl']['api_token']
self.tag_urgent = self.config['toggl']['tag_urgent']
self.tag_insitu = self.config['toggl']['tag_insitu']
def get_data(self, url):
data = requests.get(url, auth=(self.api_token, 'api_token'))
return data.json()
def list_clients(self):
url = 'https://www.toggl.com/api/v8/workspaces/%s/clients' % self.workspace_id
clients = []
for i in self.get_data(url):
clients.append({
'id': i['id'],
'name': i['name']
})
return clients
def list_date_range(self):
list = ['Today', 'Yesterday',
'ThisWeek', 'LastWeek',
'ThisMonth', 'LastMonth',
'ThisYear', 'LastYear']
return list
def get_client_id(self, client_name):
url = 'https://www.toggl.com/api/v8/workspaces/%s/clients' % self.workspace_id
for i in self.get_data(url):
if i['name'] == client_name:
cliente_id = i['id']
break
else:
cliente_id = False
return cliente_id
def list_tags(self):
url = 'https://www.toggl.com/api/v8/workspaces/%s/tags' % self.workspace_id
tags = []
for i in self.get_data(url):
tags.append({
'id': i['id'],
'name': i['name']
})
return tags
def get_tag_id(self, tag_name):
url = 'https://www.toggl.com/api/v8/workspaces/%s/tags' % self.workspace_id
for i in self.get_data(url):
if i['name'] == tag_name:
tag_id = i['id']
break
else:
tag_id = False
return tag_id
def query_date_range(self, date_range="LastMonth"):
# https://github.com/toggl/toggl_api_docs/blob/master/reports.md#request-parameters
data = {}
data['page'] = 1
data['user_agent'] = "Vanguard Toggl Report"
data['workspace_id'] = self.workspace_id
data['since'] = get_date_range(date_range)['start']
data['until'] = get_date_range(date_range)['end']
data['order_field'] = 'amount'
return data
def report_by_client(self, client, date_range):
"""
Reporte de horas trabajadas por clientes
:param client:
:return:
"""
url = 'https://toggl.com/reports/api/v2/details'
# url = 'https://toggl.com/reports/api/v2/summary' # Summary report URL
monthly_summary = {
'in-situ': {
'label': 'Horas in-situ (planificada)',
'value': 0.0 # Horas en decimal
},
'in-situ-urgent': {
'label': 'Horas in-situ (urgentes)',
'value': 0.0 # Horas en decimal
},
'remote': {
'label': 'Horas remotas (planificada)',
'value': 0.0 # Horas en decimal
},
'remote-urgent': {
'label': 'Horas remotas (urgentes)',
'value': 0.0 # Horas en decimal
},
'visit': {
'label': 'Cantidad de visitas (planificada)',
'value': 0 # Cantidad de visitas
},
'visit-urgent': {
'label': 'Cantidad de visitas (urgentes)',
'value': 0 # Cantidad de visitas
}
}
# https://github.com/toggl/toggl_api_docs/blob/master/reports.md#request-parameters
data = self.query_date_range(date_range)
data['client_ids'] = self.get_client_id(client)
data['order_field'] = 'date'
data['order_desc'] = 'off'
url_values = urllib.parse.urlencode(data)
full_url = url + '?' + url_values
x = self.get_data(full_url)
print("\n==== Trabajos realizados ====")
conunt = 1
toggl_data = x['data']
while (x['total_count'] > (x['per_page'] * data['page'])):
# x['total_count'] > x['per_page']
data['page'] += 1
url_values = urllib.parse.urlencode(data)
full_url = url + '?' + url_values
x = self.get_data(full_url)
toggl_data +=x['data']
for i in toggl_data:
# Make summary
if self.tag_insitu in i['tags']:
if self.tag_urgent in i['tags']:
v = self.visit(i['dur'])
monthly_summary['visit-urgent']['value'] += v['visit']
monthly_summary['in-situ-urgent']['value'] += v['time']
else:
v = self.visit(i['dur'])
monthly_summary['visit']['value'] += v['visit']
monthly_summary['in-situ']['value'] += v['time']
else:
if self.tag_urgent in i['tags']:
monthly_summary['remote-urgent']['value'] += milliseconds_to_hours_decimal(i['dur'])
else:
monthly_summary['remote']['value'] += milliseconds_to_hours_decimal(i['dur'])
stime = milliseconds_to_str(i['dur'])
dtime = milliseconds_to_hours_decimal(i['dur'])
task_start = datetime.strftime(taskdatetime_to_datetime(i['start'])[0], "%d/%m/%Y %H:%M")
print("%s - %s (%s [%sh])" % (task_start, i['description'], stime, dtime))
# print("%s | %s %s" % (('%02d' % conunt), milliseconds_to_hours(i['time']), i['title']['time_entry']))
conunt += 1
print("\n==== Resumen ====")
for k, v in sorted(monthly_summary.items()):
print("%-35s = %6.2f" % (v['label'], v['value']))
def report_by_tag_totaltime(self, tag, date_range):
url = 'https://toggl.com/reports/api/v2/summary' # Summary report URL
data = self.query_date_range(date_range)
data['tag_ids'] = self.get_tag_id(tag)
url_values = urllib.parse.urlencode(data)
full_url = url + '?' + url_values
# print(full_url)
x = self.get_data(full_url)
return milliseconds_to_str(x['total_grand'])
def visit(self, milliseconds):
"""
:param milliseconds: Duracion en milisegundos
:return:
"""
tasktime = milliseconds_to_hours_decimal(milliseconds)
if tasktime > 1:
visit = 1
rtime = tasktime-1
else:
visit = 0
rtime = tasktime
data = {
'visit': visit,
'time': rtime
}
return data