-
Notifications
You must be signed in to change notification settings - Fork 14
/
trello-export.py
100 lines (82 loc) · 2.49 KB
/
trello-export.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
import os, sys, argparse, json, csv
def main():
parser = argparse.ArgumentParser()
parser.add_argument("inputfile", help="the json file containing trello board data")
parser.add_argument("--outputfile", help="the json file containing trello board data")
args = parser.parse_args()
f = open(args.inputfile)
json_object = json.load(f)
print_cards(json_object, args.outputfile)
def print_cards(json_object, outputfile):
def checklist_data(card):
if len(card['idChecklists']) > 0:
checklist_id = card['idChecklists'][0]
else:
return ''
checklists = sorted(json_object['checklists'], key=lambda k: k['id'])
checklist = {}
for check in checklists:
if check['id'] == checklist_id:
checklist = check
break
string = ''
for item in checklist['checkItems']:
string += item['name'] + '\n'
return string[:-1]
def comments_data(card):
card_id = card['id']
comments = []
for action in json_object['actions']:
if ('card' in action['data'] and
action['data']['card']['id'] == card_id and
action['type'] == 'commentCard'):
comments.append(action['data']['text'])
return '\n'.join(comments)
def labels(card):
label = ''
for lbl in card['labels']:
label += lbl['name'] + '\n'
return label[:-1]
def members(card):
member_string = ''
for member in card['idMembers']:
for member_obj in json_object['members']:
if member_obj['id'] == member:
member_string += member_obj['fullName'] + '\n'
break
return member_string[:-1]
def print_types():
actions = []
for action in json_object['actions']:
if action['type'] in actions:
continue
else:
actions.append(action['type'])
print actions
def safe_string(string):
if string != None:
return string.encode('ascii', 'ignore')
else:
return ''
cards = sorted(json_object['cards'], key=lambda k: k['idShort'])
if outputfile != None:
filename = outputfile
else:
filename = 'output.csv'
with open(filename, 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['ID', 'Story', 'Description', 'Checklist', 'Comments', 'Labels', 'Due Date', 'Members'])
for card in cards:
writer.writerow([
card['idShort'],
safe_string(card['name']),
safe_string(card['desc']),
safe_string(checklist_data(card)),
safe_string(comments_data(card)),
safe_string(labels(card)),
safe_string(card['due']),
safe_string(members(card))
])
print_types()
if __name__ == "__main__":
main()