-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrello-export.py
148 lines (122 loc) · 5.52 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
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
import os
from trello import TrelloClient
import requests
import json
import argparse
import datetime
TRELLO_JSON_URL = 'https://trello.com/1/boards/{}?key={}&token={}&fields=all&actions=all&action_fields=all&actions_limit={}&cards={}&card_fields=all&card_attachments=true&lists=all&list_fields=all&members=all&member_fields=all&checklists=all&checklist_fields=all&organization=false'
parser = argparse.ArgumentParser(description='Trello command line utility')
parser.add_argument('-A', '--authenticate', action='store_true', default=False)
parser.add_argument('-t', '--token', action='store', type=str)
parser.add_argument('-a', '--all', action='store_true', default=False)
parser.add_argument('-b', '--board', action='store', type=str)
parser.add_argument('-B', '--boards', action='store_true', default=False)
parser.add_argument('-f', '--filter', action='store', type=str)
parser.add_argument('-u', '--user', action='store', type=str)
parser.add_argument('-l', '--lists', action='store_true', default=False)
trelloConfig = os.environ['HOME'] + '/.config/trello-export/config.json'
class Auth:
@classmethod
def authenticate(cls):
config = json.load(open(trelloConfig))
api_key = config.get('api_key')
auth_url = 'https://trello.com/1/authorize?expiration=30days&name=TrelloExport&scope=read,write&response_type=token&key={}'.format(
api_key)
print('Please visit the url: \n\n{}\nand run \n\ntrello-export --token <token>'.format(auth_url))
@classmethod
def set_token(cls, token):
config = json.load(open(trelloConfig))
config['token'] = token
with open(trelloConfig, 'w') as f:
json.dump(config, f)
print('Token set successfully')
class TrelloExport:
def __init__(self):
config = json.load(open(trelloConfig))
self.api_key = config.get('api_key')
self.token = config.get('token')
self.client = TrelloClient(
api_key=self.api_key,
token=self.token
)
def save_json_to_file(self, content, file_name):
with open(file_name, 'w') as f:
json.dump(content, f)
def to_json(self):
all_boards = self.client.list_boards()
resp = []
for board in all_boards:
resp.append(self.board_to_json(board.id))
return resp
def board_to_json(self, board_id, actions=1000, cards='all'):
board_resp = requests.get(TRELLO_JSON_URL.format(
board_id, self.api_key, self.token, actions, cards))
actions_retrieved = board_resp.json()['actions']
if len(actions_retrieved) > 1000:
print('More than 1000 actions. Truncating to recent 1000.')
response = board_resp.json()
return response
def list_boards(self):
for board in self.client.list_boards():
print('{}: {}'.format(board.id, board.name))
def get_filters_from_file(self, filter_file):
with open(filter_file) as f:
content = f.readlines()
filters = [(c.split(' ')[0].strip(), ' '.join(c.split(' ')[1:]).strip())
for c in content if c.strip()[0] != '#']
return filters
def get_cards(self, board_id, trello_list=None, user=None):
board = self.board_to_json(board_id, actions=0, cards='visible')
all_cards = board.get('cards')
if trello_list:
all_cards = list(filter(
lambda item: item.get('idList') == trello_list, all_cards))
if user:
found = False
for found_user in filter(lambda item: item.get(
'username') == user, board.get('members')):
found = True
all_cards = list(filter(lambda item: found_user.get(
'id') in item.get('idMembers'), all_cards))
if not found:
all_cards = []
return all_cards
def list_lists(self, board_id=None):
boards = [self.client.get_board(
board_id)] if board_id else self.client.list_boards()
for board in boards:
print('{} {}'.format(board.id, board.name))
for lst in board.all_lists():
print('\t{} {}'.format(lst.id, lst.name))
if __name__ == "__main__":
arguments = parser.parse_args()
if arguments.authenticate:
print('Authenticate request')
Auth.authenticate()
elif arguments.token:
print('Setting token in config')
Auth.set_token(arguments.token)
else:
trello_export = TrelloExport()
output = None
output_file = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
if arguments.all:
output = trello_export.to_json()
output_file = "all_{}.json".format(output_file)
elif arguments.lists:
trello_export.list_lists(arguments.board)
elif arguments.board:
output = trello_export.board_to_json(arguments.board)
output_file = "{}_{}.json".format(arguments.board, output_file)
elif arguments.boards:
trello_export.list_boards()
elif arguments.filter:
filters = trello_export.get_filters_from_file(arguments.filter)
output = []
for (board_id, list_id) in filters:
output += trello_export.get_cards(
board_id, list_id, arguments.user)
#output_file = "{}_{}.json".format(arguments.filter, output_file)
output_file = "filter_{}.json".format(output_file)
if output:
trello_export.save_json_to_file(output, output_file)