-
Notifications
You must be signed in to change notification settings - Fork 0
/
trellodumper.py
138 lines (119 loc) · 4.96 KB
/
trellodumper.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
#-------------------------------------------------------------------------------
# Name: trellodumper
# Purpose:
#
# Author: Lorenzo Bolognese
#
# Created: 05/11/2019
# Copyright: (c) Lorenzo Bolognese 2019
# Licence: MIT License
#-------------------------------------------------------------------------------
# Included libraries
import datetime
import sys
import time
# User libraries
import trelloauth
import misc
# py-trello (https://pypi.org/project/py-trello)
from trello import TrelloClient
class MainData(object):
"""
Trello account: grants access to its boards, lists and cards
"""
def __init__(self, fileName, debug = True):
self.fileName = fileName
self.debug = debug
def __GetClientBoards(self, client):
try: return client.list_boards()
except Exception as e:
self.__PrintOut("ERROR: unable to get boards. Details: " + str(e))
return []
def __GetBoardLists(self, board):
try: return board.list_lists()
except Exception as e:
self.__PrintOut("ERROR: unable to get lists. Details: " + str(e))
return []
def __GetListCards(self, tlist):
try: return tlist.list_cards()
except Exception as e:
self.__PrintOut("ERROR: unable to get cards. Details: " + str(e))
return []
def __PrintOut(self, text):
if self.debug == True: print(text)
f = open(self.fileName, "a")
f.write(text + "\n")
f.close()
def AuthorizeByTrelloRoute(self, apiKey, apiSecret):
# Less safe since that's method always uses un-variable credentials
self.client = TrelloClient(
api_key = apiKey,
api_secret = apiSecret,
)
return 0, None
def AuthorizeByOAuth(self, apiKey, oAuthSecret):
# Uses API Key and OAuth key to enquire for a temporary access token (expiration: 1 hour)
try:
credentials = trelloauth.CreateOauthToken(expiration="1hour", key=apiKey, secret=oAuthSecret, output=False)
self.client = TrelloClient(
api_key = apiKey,
token = credentials['oauth_token'],
token_secret = credentials['oauth_token_secret']
)
res = 0
excptn = None
except Exception as e:
res = -1
excptn = e
finally:
return res, excptn
def Report(self):
boards = self.__GetClientBoards(self.client)
for b in boards:
self.__PrintOut("PROJECT: " + b.name)
lists = self.__GetBoardLists(b)
for l in lists:
self.__PrintOut(" " + l.name.upper())
cards = self.__GetListCards(l)
if len(cards) == 0:
self.__PrintOut(" --- Void ---")
else:
id = 1
for c in cards:
if c.due is not None: dueDate = "--> " + c.due[:10]
else: dueDate = "--> TBD"
self.__PrintOut(" " + str(id) + ") " + c.name + " " + dueDate)
if len(c.desc) > 0: desc = self.__PrintOut(" " + c.desc.replace("\n", "\n "))
id = id + 1
self.__PrintOut("")
self.__PrintOut("Report date and time: " + str(datetime.datetime.now()))
if __name__ == '__main__':
startTime = datetime.datetime.now()
print("Trello boards, lists and cards dumper")
# Get settings from INI file
res, excptn, dbgEn, reportPath, apiKey, apiSecret, oAuthSecret, oAuthEn = misc.GetSettings('config.ini')
if res == 0:
# Check if the destination folder exists and create txt report file
res, excptn = misc.CreateFile(reportPath)
if res == 0:
# Authentication: two methods
prjBoards = MainData(reportPath, dbgEn)
if oAuthEn == True:
print('Authentication by OAuth: please grant the permission on your browser and copy and past the PIN you get')
res, excptn = prjBoards.AuthorizeByOAuth(apiKey, oAuthSecret)
else:
print('Authentication by Trello route')
res, excptn = prjBoards.AuthorizeByTrelloRoute(apiKey, apiSecret)
if res == 0:
# Authorization gone fine: fill report
print("Please wait while filling " + reportPath + "...")
prjBoards.Report()
print("Done!!!\n")
else:
print("Unable to proceed. Missing authorization: " + str(excptn) + "\n")
else:
print("ERROR: output folder is missing. Details: " + str(e) + "\n")
else:
print("Unable to proceed. INI file is wrong: " + str(excptn) + "\n")
print("Execution time: " + str(datetime.datetime.now() - startTime))
input("Press Enter to continue...")