-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrello.py
377 lines (285 loc) · 11.2 KB
/
trello.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#############################
# Info
# Get an API Key: https://trello.com/1/appKey/generate
# Original project: https://github.com/bmccormack/trello-python-demo
# Python Requests documentation: https://2.python-requests.org//en/master/api/#requests.Response
# Trello API documentation: https://developers.trello.com/reference#listsidcards
#############################
import req
import json
#############################
# Global variables
#############################
# Base trello API URL
base = 'https://trello.com/1/'
#############################
# Functions
#############################
def _get_config(config_file):
# Gets config from config.json
try:
cfg = json.load(open(config_file))
return cfg
except Exception as e:
print("[ERR] Cannot get config from %s (%s)" % (config_file, e) )
quit()
def get_boards():
# Returns an Array of board dictionary objects
# Get config
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
# Build out the URL based on the documentation
url = base + 'members/me/boards'
# Populate any required arguments
arguments = {}
headers = {}
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'get', headers)
if 'response' in r:
return r['response']
else:
print(r['error'])
return []
def get_lists(board_name):
# Returns the lists associated with a board
#print("Getting lists for board: %s" % (board_name) )
board_found = 0
# Get config
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
# Get the board ID
# Get boards
boards = get_boards()
for board in boards:
if board['name'] == board_name:
board_found = 1
# Build out the URL based on the documentation
url = base + '/boards/' + board['id'] + '/lists'
# Populate any required arguments
arguments = {}
headers = {}
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'get', headers)
if 'response' in r:
return r['response']
else:
print(r['error'])
return []
if not board_found:
print("[ERR] Board '%s' not found" % board_name)
return []
def get_cards(board_name, list_name):
board_found = 0
list_found = 0
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
boards = get_boards()
for board in boards:
if board['name'] == board_name:
board_found = 1
# Get lists
lists = get_lists(board_name)
for list in lists:
if list['name'] == list_name:
list_found = 1
# Build URL
url = base + '/lists/' + list['id'] + '/cards'
# Define arguments
arguments = {}
headers = {}
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'get', headers)
if 'response' in r:
return r['response']
else:
print(r['error'])
return []
if not board_found:
print("[ERR] Board '%s' not found" % board_name)
return []
if not list_found:
print("[ERR] List '%s' not found" % list_name)
return []
def get_card_actions(board_name, list_name, card_name):
card_found = 0
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
cards = get_cards(board_name, list_name)
for card in cards:
if card['name'] == card_name:
card_found = 1
url = base + '/cards/' + card['id'] + '/actions'
# Call the Trello API, passing params and arguments
arguments = {}
headers = {}
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'get', headers)
if 'response' in r:
return r['response']
else:
print(r['error'])
return []
if not card_found:
print("[ERR] Card '%s' not found" % card_name)
return []
def create_board(board_name, description):
# Get config
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
boards = get_boards()
for board in boards:
if board['name'] == board_name:
#print("[ERR] Board '%s' already exists" % board_name)
return 2
arguments = {'name': board_name, 'desc': description }
headers = {}
url = base + 'boards'
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'post', headers)
if 'response' in r:
return 0
else:
print(r['error'])
return 1
def create_list(board_name, list_name):
# Creates a new card in a given board
# Get config
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
board_found = 0
boards = get_boards()
for board in boards:
if board['name'] == board_name:
board_found = 1
lists = get_lists(board_name)
# Check if list already exists
if len(lists) > 0:
for list in lists:
if list['name'] == list_name:
#print("[ERR] List '%s' already exists in board '%s'" % (list['name'],board['name']) )
return 2
# Build the new list data dict object
arguments = {'name': list_name, 'pos': 0, 'idBoard': board['id'] }
headers = {}
# Build URL
url = base + 'lists'
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'post', headers)
if 'response' in r:
return 0
else:
print(r['error'])
return 1
if not board_found:
print("[ERR] Board '%s' not found" % (board_name) )
return 1
def create_card(board_name, list_name, card_name, card_description, card_members, closed):
# Creates a new card in a given board
#print("\nCreating card: %s in board: %s, list: %s. Description: %s" % (card_name, list_name, board_name, card_description) )
# Get config
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
rc = {}
list_found = 0
# Get lists
lists = get_lists(board_name)
# Quit if no lists returned
if len(lists) < 1:
print("[ERR] No list called %s found in board %s" % (list_name, board_name) )
return 1
for list in lists:
if list['name'] == list_name:
list_found = 1
#print("Found List: [%s] %s" % (list['id'], list['name']))
cards = get_cards(board_name, list_name)
for card in cards:
#print("\nCard: %s. Closed: %s\n" % (card['name'], card['closed']) )
if card['name'] == card_name:
#print("[ERR] Card '%s' already exists in list '%s'" % (card_name, list_name) )
rc['code'] = 2
return rc
# Build the new list data dict object
arguments = {'name': card_name, 'desc': card_description, 'pos': "top", 'idList': list['id'], 'idMembers': card_members, 'closed': closed}
headers = {}
# Build URL
url = base + 'cards'
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'post', headers)
if 'response' in r:
card_dict = r['response']
rc['code'] = 0
rc['id'] = card_dict['id']
return rc
else:
print(r['error'])
rc['code'] = 1
return rc
if not list_found:
print("[ERR] List '%s' not found" % (list_name) )
rc['code'] = 1
return rc
def create_card_comment(board_name, list_name, card_name, comment):
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
card_found = 0
rc = {}
cards = get_cards(board_name, list_name)
# Quit if no cards returned
if len(cards) < 1:
print("[ERR] No card called '%s' found in list '%s' in board '%s'" % (card_name, list_name, board_name) )
return 1
for card in cards:
if card['name'] == card_name:
card_found = 1
# Build the new list data dict object
arguments = {'text': comment}
headers = {}
# Build URL
url = base + '/cards/' + card['id'] + '/actions/comments'
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'post', headers)
if 'response' in r:
resp_dict = r['response']
rc['code'] = 0
rc['id'] = resp_dict['id']
return rc
else:
print(r['error'])
rc['code'] = 1
return rc
if not card_found:
print("[ERR] Card '%s' not found" % (card_name) )
rc['code'] = 1
return rc
def archive_card_by_id(card_id):
# Archives a card given a card id
# in Trello Archiving sets 'Closed' to 'true'
# Get config
cfg = _get_config('config.json')
params_key_and_token = {'key':cfg["TrelloAPIKey"],'token':cfg["TrelloAPIToken"]}
rc = {}
# Build the new list data dict object
arguments = {'id': card_id, 'closed': 'true'}
headers = {}
# Build URL
url = base + 'cards/' + card_id
# Call the Trello API, passing params and arguments
# The Trello API returns an array of dicts
r = req.request_json(url, params_key_and_token, arguments, 'put', headers)
if 'response' in r:
resp_dict = r['response']
rc['code'] = 0
rc['id'] = resp_dict['id']
return rc
else:
print(r['error'])
rc['code'] = 1
return rc