forked from senshi-x/ComdirectPostboxDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·395 lines (342 loc) · 13.7 KB
/
main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
from ComdirectConnection import Connection
from settings import Settings
from pathvalidate import sanitize_filename
from enum import Enum
import os
import time
import datetime
class DownloadSource(Enum):
archivedOnly = 'archivedOnly'
notArchivedOnly = 'notArchivedOnly'
all = 'all'
class Main:
def __init__(self, dirname):
self.dirname = dirname
try:
self.settings = Settings(dirname)
except Exception as error:
print(error)
input("Press ENTER to close. Create settings.ini from the example before trying again.")
exit(0)
self.conn = False
self.onlineDocumentsDict = {}
self.onlineAdvertismentIndicesList = []
self.onlineArchivedIndicesList = []
self.onlineUnreadIndicesList = []
self.onlineFileNameMatchingIndicesList = []
self.onlineNotYetDownloadedIndicesList = []
self.onlineAlreadyDownloadedIndicesList = []
self.showMenu()
def __printFullWidth(self, printString, align="center", filler="-", width=74):
printString = str(printString)
spaces = width - len(printString)
if spaces % 2 == 1:
printString += " "
spaces = width - len(printString)
if align == "center":
filler = int(spaces / 2)
print(filler * "-" + printString + filler * "-")
elif align == "left":
filler = int(spaces)
print(printString + filler * "-")
elif align == "right":
filler = int(spaces)
print(filler * "-" + printString)
def __printLeftRight(self, printLeftString, printRightString, filler=".", width=74):
printLeftString = str(printLeftString)
printRightString = str(printRightString)
spaces = width - len(printLeftString) - len(printRightString)
print(printLeftString + (spaces * filler) + printRightString)
def showMenu(self):
def __print_menu():
self.__printFullWidth("--")
self.__printFullWidth(" Comdirect Documents Downloader ")
self.__printFullWidth(" by WGPSenshi & retiredHero ")
self.__printFullWidth("--")
onlineStatus = " online "
if not self.conn:
onlineStatus = " not online "
self.__printFullWidth("Current Status: " + onlineStatus, "left")
self.__printFullWidth("--")
print("1. Show Current Settings ")
print("2. Reload Settings From File settings.ini ")
print("3. Show Status Local Files (WIP) ")
print("4. Show Status Online Files ")
# print("5. (WIP) ")
print("6. Start Download / Update Local Files ")
print("0. Exit ")
self.__printFullWidth("--")
loop = True
while loop:
__print_menu()
user_input = input("Choose an option [1-6] / [0]: ")
val = 0
try:
val = int(user_input)
except ValueError:
print(
"No.. input is not a valid integer number! Press Enter to continue!"
)
continue
if val == 1:
# Show Current Settings
self.__printFullWidth("--")
self.__printFullWidth(" Current Settings ")
self.settings.showSettings()
self.__printFullWidth("--")
elif val == 2:
# Reload Settings from file
self.__printFullWidth("--")
self.__printFullWidth(" Reload Settings From File settings.ini ")
self.settings.readSettings()
self.__printFullWidth("--")
elif val == 3:
# show status local files
print("-3-")
elif val == 4:
# show status online files
print("-4-")
self.__startConnection()
self.__loadDocuments()
self.__showStatusOnlineDocuments()
elif val == 5:
# -
print("-5-")
elif val == 6:
# start download of files
print("-6-")
self.__startConnection()
self.__loadDocuments()
self.__processOnlineDocuments()
elif val == 0:
loop = False
else:
print("not a valid input")
if not val == 0:
input("Press Enter to Return to Menu!")
return val
def __startConnection(self):
"""
ToDo: Check if all settings are set for connection!
"""
if self.settings and not self.conn:
try:
self.conn = Connection(
username=self.settings.getValueForKey("user"),
password=self.settings.getValueForKey("pwd"),
client_id=self.settings.getValueForKey("clientId"),
client_secret=self.settings.getValueForKey("clientSecret"),
)
self.conn.login()
except Exception as err:
print(err)
else:
print("You are already online!")
def __loadDocuments(self):
if not self.conn:
raise NameError("conn not set!")
if self.onlineDocumentsDict:
return
messagesMeta = self.conn.getMessagesList(0, 1)
x = 0
# Process batches of 1000. Max batchsize is 1000 (API restriction)
batchSize = 1000
self.onlineDocumentsDict = {}
while x < messagesMeta["paging"]["matches"]:
messagesMeta = self.conn.getMessagesList(x, batchSize)
for idx, documentMeta in enumerate(messagesMeta["values"]):
self.onlineDocumentsDict[x + idx] = messagesMeta["values"][idx]
x += batchSize
def __showStatusOnlineDocuments(self):
self.onlineAdvertismentIndicesList = []
self.onlineArchivedIndicesList = []
self.onlineFileNameMatchingIndicesList = []
self.onlineNotYetDownloadedIndicesList = []
self.onlineAlreadyDownloadedIndicesList = []
self.onlineUnreadIndicesList = []
if not self.onlineDocumentsDict:
return
self.countOnlineAll = len(self.onlineDocumentsDict)
# do the count run!
self.__processOnlineDocuments(True)
# show result:
self.__printFullWidth("--")
self.__printFullWidth(" Status Online Files ")
self.__printFullWidth("--")
onlineStatus = " online "
if not self.conn:
onlineStatus = " not online "
self.__printFullWidth("Current Status: " + onlineStatus, "left")
self.__printFullWidth("--")
self.__printLeftRight(
"Files online all count: ", str(self.countOnlineAll), ".", 20
)
self.__printLeftRight(
"Files online advertisment count: ",
str(len(self.onlineAdvertismentIndicesList)),
".",
20,
)
self.__printLeftRight(
"Files online not yet read count: ",
str(len(self.onlineUnreadIndicesList)),
".",
20,
)
self.__printLeftRight(
"Files online in archive count: ",
str(len(self.onlineArchivedIndicesList)),
".",
20,
)
self.__printLeftRight(
"Files online filename matches count: ",
str(len(self.onlineFileNameMatchingIndicesList)),
".",
20,
)
self.__printLeftRight(
"Files online already downloaded count: ",
str(len(self.onlineAlreadyDownloadedIndicesList)),
".",
20,
)
self.__printLeftRight(
"Files online not yet downloaded count: ",
str(len(self.onlineNotYetDownloadedIndicesList)),
".",
20,
)
self.__printFullWidth("--")
def __processOnlineDocuments(self, isCountRun=False):
if not self.onlineDocumentsDict:
return
menuWidth = 200
def __printStatus(idx, document, status=""):
# fill idx to 5 chars
idx = str(idx)
idx = idx.zfill(5)
if not isCountRun:
self.__printLeftRight(
idx
+ " - "
+ document["dateCreation"]
+ " - "
+ document["name"]
+ " - "
+ document["mimeType"],
status,
".",
menuWidth,
)
overwrite = False # Only download new files
useSubFolders = self.settings.getBoolValueForKey("useSubFolders")
outputDir = self.settings.getValueForKey("outputDir")
isDownloadOnlyFilename = self.settings.getBoolValueForKey(
"downloadOnlyFilenames"
)
downloadFilenameList = self.settings.getValueForKey(
"downloadOnlyFilenamesArray"
)
downloadSource = self.settings.getValueForKey(
"downloadSource"
)
countAll = len(self.onlineDocumentsDict)
countProcessed = 0
countSkipped = 0
countDownloaded = 0
# for idx in range(len(self.onlineDocumentsDict)): # documentMeta in enumerate(self.onlineDocumentsDict):
for idx in self.onlineDocumentsDict:
documentMeta = self.onlineDocumentsDict[idx]
docName = documentMeta["name"]
firstFilename = docName.split(" ", 1)[0]
docMimeType = documentMeta["mimeType"]
docCreateDate = documentMeta["dateCreation"]
isDocAdvertisement = (
True if str(documentMeta["advertisement"]).lower() == "true" else False
)
isDocArchived = (
True
if str(documentMeta["documentMetaData"]["archived"]).lower() == "true"
else False
)
isAlreadyRead = (
True
if str(documentMeta["documentMetaData"]["alreadyRead"]).lower()
== "true"
else False
)
subFolder = ""
myOutputDir = outputDir
countProcessed += 1
# counting
if isDocAdvertisement:
self.onlineAdvertismentIndicesList.append(idx)
if isDocArchived:
self.onlineArchivedIndicesList.append(idx)
if firstFilename in downloadFilenameList:
self.onlineFileNameMatchingIndicesList.append(idx)
if not isAlreadyRead:
self.onlineUnreadIndicesList.append(idx)
# check for setting "download source"
if downloadSource == DownloadSource.archivedOnly.value and not isDocArchived or\
downloadSource == DownloadSource.notArchivedOnly.value and isDocArchived:
__printStatus(idx, documentMeta, "SKIPPED - not in selected download source")
countSkipped += 1
continue
# check for setting "only download if filename is in filename list"
if isDownloadOnlyFilename and not firstFilename in downloadFilenameList:
__printStatus(
idx, documentMeta, "SKIPPED - filename not in filename list"
)
countSkipped += 1
continue
if docMimeType == "application/pdf":
subFolder = firstFilename
docName += ".pdf"
elif docMimeType == "text/html":
docName += ".html"
subFolder = "html"
if useSubFolders:
myOutputDir = os.path.join(outputDir, sanitize_filename(subFolder))
if not os.path.exists(myOutputDir):
os.makedirs(myOutputDir)
filepath = os.path.join(myOutputDir, sanitize_filename(docName))
# check if already downloaded
if os.path.exists(filepath):
self.onlineAlreadyDownloadedIndicesList.append(idx)
if not overwrite:
__printStatus(idx, documentMeta, "SKIPPED - no overwrite")
countSkipped += 1
continue
else:
self.onlineNotYetDownloadedIndicesList.append(idx)
# do the download
if not bool(self.settings.getBoolValueForKey("dryRun")) and not isCountRun:
docContent = self.conn.downloadMessage(documentMeta)
moddate = time.mktime(
datetime.datetime.strptime(docCreateDate, "%Y-%m-%d").timetuple()
)
with open(filepath, "wb") as f:
f.write(docContent)
# shutil.copyfileobj(docContent, f)
os.utime(filepath, (moddate, moddate))
__printStatus(idx, documentMeta, "DOWNLOADED")
countDownloaded += 1
else:
__printStatus(
idx, documentMeta, "DOWNLOADED - dry run, so not really downloaded"
)
countDownloaded += 1
# last line, summary status:
if not isCountRun:
menuWidth = 74
self.__printFullWidth("--", "center", "-", menuWidth)
self.__printFullWidth("Status Files Downloading", "left", "-", menuWidth)
print("All: " + str(countAll) + " files")
print("Processed: " + str(countProcessed) + " files")
print("Downloaded: " + str(countDownloaded) + " files")
print("Skipped: " + str(countSkipped) + " files")
dirname = os.path.dirname(__file__)
main = Main(dirname)