-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcow_rest.py
290 lines (208 loc) · 9.04 KB
/
cow_rest.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
"""
skill the-cows-lists
Copyright (C) 2017 Carsten Agerskov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from hashlib import md5
from urllib.request import urlopen, quote
import urllib
import json
import configparser
import os
__author__ = 'cagerskov'
RTM_URL = "https://api.rememberthemilk.com/services/rest/"
AUTH_URL = "https://www.rememberthemilk.com/services/auth/"
HOME_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = HOME_DIR + "/cowslist.cfg"
config = configparser.ConfigParser()
api_key = None
secret = None
auth_token = None
frob = None
timeline = None
class RtmRest:
def __init__(self, default_param):
self.param = list([['format', 'json']])
self.param.extend(default_param)
def add(self, param_list):
self.param.extend(param_list)
def get_param_string(self):
self.param.sort()
return '?' + ''.join(map((lambda x: x[0] + '=' + quote(x[1].encode('utf8')) + '&'), self.param)) \
+ 'api_sig=' + md5((secret + ''.join(map((lambda x: x[0] + x[1]), self.param))).encode('utf8')).hexdigest()
def call(self):
s = urlopen(RTM_URL + self.get_param_string())
response = json.loads(s.read().decode())
s.close()
return response
def _get_basic_param():
return list([['api_key', api_key]])
def _get_full_param():
return _get_basic_param() + [['auth_token', auth_token]]
def _get_full_timeline_param():
return _get_full_param() + [['timeline', timeline]]
def find_task_id(task_list, taskseries_id, task_id):
if 'list' not in task_list:
return None
task_match = None
taskseries_match = None
for taskseries in task_list['list']:
if not isinstance(taskseries['taskseries'], list):
if taskseries['taskseries']['id'] == taskseries_id:
taskseries_match = taskseries['taskseries']
else:
taskseries_match = [x for x in taskseries['taskseries'] if x['id'] == taskseries_id]
if not taskseries_match:
return None
if not isinstance(taskseries_match, list):
task_match = [x for x in taskseries_match['task'] if x['id'] == task_id]
else:
for task in taskseries_match:
task_match = [x for x in task['task'] if x['id'] == task_id]
return task_match
def flat_task_list(task_list):
flat_task = []
if 'list' not in task_list:
return flat_task
for taskseries in task_list['list']:
if isinstance(taskseries['taskseries'], list):
for t in taskseries['taskseries']:
if isinstance(t['task'], list):
for x in t['task']:
flat_task.append({'task_name': t['name'],
'taskseries_id': t['id'],
'task_id': x['id']})
else:
flat_task.append({'task_name': t['name'],
'taskseries_id': t['id'],
'task_id' : t['task']['id']})
else:
if isinstance(taskseries['taskseries']['task'], list):
for x in taskseries['taskseries']['task']:
flat_task.append(
{'task_name': taskseries['taskseries']['name'],
'taskseries_id': taskseries['taskseries']['id'],
'task_id': x['id']})
else:
flat_task.append({ 'task_name': taskseries['taskseries']['name'],
'taskseries_id': taskseries['taskseries']['id'],
'task_id': taskseries['taskseries']['task']['id']})
return flat_task
def get_token(self):
if not self.auth_token:
config.read(CONFIG_FILE)
if config.has_option('auth', 'auth_token'):
self.auth_token = config.get('auth', 'auth_token')
def get_new_token(self):
r = RtmRest(_get_basic_param())
r.add([["method", 'rtm.auth.getToken'],
["frob", frob]])
response = r.call()
if response['rsp']['stat'] == 'fail':
return response['rsp']['err']['msg'], response['rsp']['err']['code']
if not config.has_section('auth'):
config.add_section('auth')
config.set('auth', 'auth_token', response['rsp']['auth']['token'])
with open(CONFIG_FILE, 'w') as configfile:
config.write(configfile)
self.auth_token = response['rsp']['auth']['token']
return None, None
def get_timeline(self):
r = RtmRest(_get_full_param())
r.add([['method', 'rtm.timelines.create']])
timeline_result = r.call()
if timeline_result['rsp']['stat'] == 'fail':
return timeline_result['rsp']['err']['msg'], timeline_result['rsp']['err']['code']
self.timeline = timeline_result['rsp']['timeline']
return None, None
def verify_token_validity():
r = RtmRest(_get_full_param())
r.add([["method", 'rtm.auth.checkToken']])
check_token = r.call()
if check_token['rsp']['stat'] == 'fail':
return check_token['rsp']['err']['msg'], check_token['rsp']['err']['code']
return None, None
def get_frob(self):
r = RtmRest(_get_basic_param())
r.add([["method", 'rtm.auth.getFrob']])
response = r.call()
if response['rsp']['stat'] == 'fail':
return response['rsp']['err']['msg'], response['rsp']['err']['code']
self.frob = str(response['rsp']['frob'])
return None, None
def get_auth_url():
return AUTH_URL + '?api_key=' + str(api_key) + '&perms=delete&frob=' + str(frob) \
+ '&api_sig=' + md5((secret + 'api_key' + api_key + 'frob'
+ frob + 'permsdelete').encode('utf8')).hexdigest()
def get_list():
r = RtmRest(_get_full_param())
r.add([["method", "rtm.lists.getList"]])
list_result = r.call()
if list_result['rsp']['stat'] == 'fail':
return None, list_result['rsp']['err']['msg'], list_result['rsp']['err']['code']
return list_result['rsp']['lists']['list'], None, None
def add_task(item_name, list_id):
r = RtmRest(_get_full_timeline_param())
r.add([['method', 'rtm.tasks.add'],
['list_id', list_id],
['name', item_name],
['parse', '1']])
insert_result = r.call()
if insert_result['rsp']['stat'] == 'fail':
return None, None, insert_result['rsp']['err']['msg'], insert_result['rsp']['err']['code']
if isinstance(insert_result['rsp']['list']['taskseries'], list):
taskseries = insert_result['rsp']['list']['taskseries'][0]
else:
taskseries = insert_result['rsp']['list']['taskseries']
if isinstance(taskseries['task'], list):
task = taskseries['task'][0]
else:
task = taskseries['task']
return taskseries['id'], task['id'], None, None
def delete_task(task_id, taskseries_id, list_id):
r = RtmRest(_get_full_timeline_param())
r.add([['method', 'rtm.tasks.delete'],
['task_id', task_id],
['taskseries_id', taskseries_id],
['list_id', list_id]])
delete_result = r.call()
if delete_result['rsp']['stat'] == 'fail':
return None, delete_result['rsp']['err']['msg'], delete_result['rsp']['err']['code']
return delete_result['rsp']['transaction']['id'], None, None
def list_task(list_filter, list_id):
# This call may return a huge amount of data, be sure to use filter!
r = RtmRest(_get_full_param())
r.add([['method', 'rtm.tasks.getList'], ['filter', list_filter]])
if list_id:
r.add([['list_id', list_id]])
list_result = r.call()
if list_result['rsp']['stat'] == 'fail':
return None, list_result['rsp']['err']['msg'], list_result['rsp']['err']['code']
return list_result['rsp']['tasks'], None, None
def roll_back(transaction_id):
r = RtmRest(_get_full_timeline_param())
r.add([['method', 'rtm.transactions.undo'],
['transaction_id', transaction_id]])
roll_back_result = r.call()
if roll_back_result['rsp']['stat'] == 'fail':
return roll_back_result['rsp']['err']['msg'], roll_back_result['rsp']['err']['code']
return None, None
def complete_task(task_id, taskseries_id, list_id):
r = RtmRest(_get_full_timeline_param())
r.add([['method', 'rtm.tasks.complete'],
['task_id', task_id],
['taskseries_id', taskseries_id],
['list_id', list_id]])
complete_result = r.call()
if complete_result['rsp']['stat'] == 'fail':
return None, complete_result['rsp']['err']['msg'], complete_result['rsp']['err']['code']
return complete_result['rsp']['transaction']['id'], None, None