-
Notifications
You must be signed in to change notification settings - Fork 1
/
timelog.py
300 lines (234 loc) · 10.4 KB
/
timelog.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/python
# encoding: utf-8
import sys
import collections
import xml.etree.ElementTree as ET
import argparse
import datetime
import uuid
from workflow import Workflow3, web, PasswordNotFound
from workflow.notify import notify
HELP_URL = 'https://github.com/mathiasjakobsen/alfred-timelog/issues'
Token = collections.namedtuple('Token', ['hash', 'expires', 'initials'])
Task = collections.namedtuple('Task', ['id', 'name', 'project'])
User = collections.namedtuple('User', ['password', 'username', 'token'])
def login(query):
args = query.split(' ')
username = args[0]
password = args[1]
token = fetch_token(username, password)
if not token:
notify(u'(╯°□°)╯︵ ', 'Invalid credentials!', 'Basso')
return 1
wf.save_password('password', password)
wf.settings['username'] = username
notify('You are ready to go!', 'Tracking time for ' + username)
def get_user():
try:
username = wf.settings['username']
password = wf.get_password('password')
def fetcher():
return fetch_token(username, password)
token = wf.cached_data('token', fetcher, max_age=3600)
return User(username = username, password = password, token = token)
except (PasswordNotFound, KeyError):
return None
def action(args):
action = args.split(', ')
intent = action[0]
identifier = action[1]
if intent == 'select':
task_selected(identifier)
if intent == 'stop':
active_task = get_active_task()
end_registration(active_task)
def task_selected(identifier):
active_task = get_active_task()
if active_task:
end_registration(active_task)
begin_registration(identifier)
def begin_registration(identifier):
notify(u'Yippie ki yay, motherfucker (⌐■_■)', 'Now, start being awesome.', 'Pop')
set_active_task(identifier)
def end_registration(active_task):
notify('Ended registration', active_task['id'], 'Pop')
wf.store_data('active_task', None)
insert_work(active_task['id'], active_task['datetime'], datetime.datetime.now())
def get_active_task():
return wf.stored_data('active_task')
def set_active_task(identifier):
wf.store_data('active_task', {
'id': identifier,
'datetime': datetime.datetime.now()
})
def idle():
active_task = get_active_task()
if not active_task:
wf.add_item(
title = 'If you snooze, you loose (ง •̀_•́)ง',
subtitle = 'Start typing to find a task, and get started!',
valid = False
)
else:
tasks = get_tasks()
task = (item for item in tasks if item.id == active_task['id']).next()
now = datetime.datetime.now()
hours, minutes = hours_and_minutes(active_task['datetime'], now)
wf.add_item(
title = task.name,
subtitle = 'Started tracking ' + str(hours) + ' hours and ' + str(minutes) + ' minutes ago. Hit enter to stop.',
arg = ', '.join(['stop', task.id]),
valid = True
)
wf.send_feedback()
def hours_and_minutes(then, now):
hours = 0
minutes = int((now - then).total_seconds() / 60.0)
while minutes >= 60:
hours += 1
minutes -= 60
return hours, minutes
def get_tasks():
return wf.cached_data('tasks', fetch_tasks, max_age=3600)
def search(query):
filtr = lambda t : '{} {}'.format(wf.fold_to_ascii(t.name), wf.fold_to_ascii(t.project))
items = wf.filter(wf.fold_to_ascii(query), get_tasks(), filtr)
if not items:
wf.add_item(u'No matches (╯°□°)╯︵ ┻━┻', 'Try changing your search query..')
for item in items:
wf.add_item(title = item.name,
subtitle = item.project,
arg = ', '.join(['select', item.id]),
autocomplete=item.name,
valid=True)
wf.send_feedback()
def insert_work(task_id, then, now):
now = now
user = get_user()
hours, minutes = hours_and_minutes(then, now)
duration = 'PT' + str(hours) + 'H' + str(minutes) + 'M'
data = '''
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<InsertWork xmlns="http://www.timelog.com/api/tlp/v1_6">
<work xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<WorkUnit>
<GUID>{guid}</GUID>
<AllocationGUID>{aguid}</AllocationGUID>
<TaskID>{task_id}</TaskID>
<EmployeeInitials>{initials}</EmployeeInitials>
<Duration>{duration}</Duration>
<StartDateTime>{then}</StartDateTime>
<EndDateTime>{now}</EndDateTime>
<Description>{description}</Description>
<TimeStamp i:nil="true" />
<IsEditable>false</IsEditable>
<AdditionalText i:nil="true" />
<Details i:nil="true" />
</WorkUnit>
</work>
<source>50</source>
<token xmlns:d4p1="http://www.timelog.com/api/tlp/v1_3" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:Initials>{initials}</d4p1:Initials>
<d4p1:Expires>{expires}</d4p1:Expires>
<d4p1:Hash>{hash}</d4p1:Hash>
</token>
</InsertWork>
</s:Body>
</s:Envelope>'''.format(
guid = uuid.uuid4(),
aguid = '00000000-0000-0000-0000-000000000000',
task_id = task_id,
initials = user.token.initials,
duration = duration,
then = then.isoformat(),
now = now.isoformat(),
description = '',
expires = user.token.expires,
hash = user.token.hash
)
headers = { 'Content-Type': 'text/xml', 'SOAPAction': 'InsertWorkRequest' }
response = web.post("https://app1.timelog.com/arnsbomedia/WebServices/ProjectManagement/V1_6/ProjectManagementServiceSecure.svc", data=data, headers=headers)
if response.status_code == 200:
notify(u'Done! ʕっ•ᴥ•ʔっ', 'Registed ' + str(hours) + ' hours & ' + str(minutes) + ' minutes', 'Pop')
else:
notify('Ohhh no', 'TimeLog responded with ' + str(response.status_code), 'Basso')
def fetch_token(username, password):
headers = { 'Content-Type': 'text/xml', 'SOAPAction': 'GetTokenRequest' }
data = '''
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body>
<GetToken xmlns="http://www.timelog.com/api/tlp/v1_2"><user>{user}</user><password>{password}</password></GetToken>
</s:Body></s:Envelope>'''.format(password=password, user=username)
xml = web.post("https://app1.timelog.com/arnsbomedia/WebServices/Security/V1_2/SecurityServiceSecure.svc", data=data, headers=headers).text
ns = {
's': 'http://schemas.xmlsoap.org/soap/envelope/',
'a': 'http://www.timelog.com/api/tlp/v1_1',
'i': 'http://www.w3.org/2001/XMLSchema-instance'
}
root = ET.fromstring(str(xml))
path = 's:Body/{http://www.timelog.com/api/tlp/v1_2}GetTokenResponse/'
path += '{http://www.timelog.com/api/tlp/v1_2}GetTokenResult/'
path += '{http://www.timelog.com/api/tlp/v1_1}Return/'
path += '{http://www.timelog.com/api/tlp/v1_2}SecurityToken/'
token = root.findall(path, ns)
try:
return Token(hash=token[2].text, expires=token[1].text, initials=token[0].text)
except IndexError:
return None
def fetch_tasks():
user = get_user()
data = '''
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetTasksAllocatedToEmployee xmlns="http://www.timelog.com/api/tlp/v1_6">
<initials>{initials}</initials>
<token xmlns:d4p1="http://www.timelog.com/api/tlp/v1_3" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:Initials>{initials}</d4p1:Initials>
<d4p1:Expires>{expires}</d4p1:Expires>
<d4p1:Hash>{hash}</d4p1:Hash>
</token>
</GetTasksAllocatedToEmployee>
</s:Body>
</s:Envelope>
'''.format(initials=user.token.initials, expires=user.token.expires, hash=user.token.hash)
headers = { 'Content-Type': 'text/xml', 'SOAPAction': 'GetTasksAllocatedToEmployeeRequest' }
xml = web.post("https://app1.timelog.com/arnsbomedia/WebServices/ProjectManagement/V1_6/ProjectManagementServiceSecure.svc", data=data, headers=headers).text
foo = xml.encode('utf-8')
ns = {
's': 'http://schemas.xmlsoap.org/soap/envelope/',
'a': 'http://www.timelog.com/api/tlp/v1_1',
'i': 'http://www.w3.org/2001/XMLSchema-instance'
}
root = ET.fromstring(foo)
path = 's:Body/{http://www.timelog.com/api/tlp/v1_6}GetTasksAllocatedToEmployeeResponse/'
path += '{http://www.timelog.com/api/tlp/v1_6}GetTasksAllocatedToEmployeeResult/'
path += '{http://api.timelog.com}Return/'
path += '{http://www.timelog.com/api/tlp/v1_6}Task'
tasks = []
for elm in root.findall(path, ns):
uuid = elm.find('{http://www.timelog.com/api/tlp/v1_6}TaskID', ns).text
name = elm.find('{http://www.timelog.com/api/tlp/v1_6}Name', ns).text
project = elm.find('{http://www.timelog.com/api/tlp/v1_6}Details/{http://www.timelog.com/api/tlp/v1_6}ProjectHeader/{http://www.timelog.com/api/tlp/v1_6}Name', ns).text
task = Task(id=uuid, name=name, project=project)
tasks.append(task)
return tasks
def main(wf):
parser = argparse.ArgumentParser()
parser.add_argument('query', nargs='?', default=None)
parser.add_argument('--login', dest='login', nargs='?', default=None)
parser.add_argument('--action', dest='action', nargs='?', default=None)
args = parser.parse_args(wf.args)
if args.login:
return login(args.login)
if not get_user():
wf.add_item(title="Not logged in! Please run 'timelog login'", valid=False)
wf.send_feedback()
return 1
if args.action:
return action(args.action)
if len(args.query):
return search(args.query)
return idle()
if __name__ == '__main__':
wf = Workflow3(help_url=HELP_URL)
sys.exit(wf.run(main))