-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathm2em.py
executable file
·445 lines (353 loc) · 13.8 KB
/
m2em.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env python
""" Main Wrapper Module """
import os
import sys
import logging
import time
import argparse
import datetime
import validators
from bin._version import __version__
# Start of the fun!
import bin.Config as Config
import bin.Helper as helper
import bin.RssParser as mparser
import bin.DownloaderHandler as mdownloader
import bin.ConverterHandler as mconverter
import bin.SenderHandler as msender
import bin.Migrator as migrator
class M2em:
""" Main Class """
def __init__(self):
# Python 3 is required!
if sys.version_info[0] < 3:
sys.stdout.write("Sorry, requires Python 3.x, not Python 2.x\n")
sys.exit(1)
# Get args right at the start
self.args = None
if not self.args:
self.read_arguments()
# Load config right at the start
self.config = None
if not self.config:
self.config = Config.load_config()
logging.debug("Loaded Config:")
logging.debug(self.config)
# Check if Database exists, else create
if not os.path.isfile(self.config["Database"]):
helper.createFolder(self.config["SaveLocation"])
helper.createDB()
# Check weather there are some database migrations
mversion = helper.getMigrationVersion() + ".py"
if self.config["DisableMigrations"] == "True":
logging.debug("Migrations disabled! Current version: %s ", mversion)
else:
if mversion in os.listdir(os.getcwd() + "/migrations"):
logging.debug("No migrations required! Current version: %s ", mversion)
else:
migrator.migrate()
def read_arguments(self):
""" function that reads all arguments """
# Get user Input
parser = argparse.ArgumentParser(description='Manga to eManga - m2em')
parser.add_argument("-af", "--add-feed",
help="Add RSS Feed of Manga. Only Mangastream & MangaFox are supported")
parser.add_argument("-au", "--add-user", help="Adds new user",
action="store_true")
parser.add_argument("-lm", "--list-manga",
help="Lists Manga saved in database. If a Manga is passed, lists chapters to said Manga",
nargs="?", const='all')
parser.add_argument("-lc", "--list-chapters", help="Lists the last 10 Chapters",
action="store_true")
parser.add_argument("-Lc", "--list-chapters-all", help="Lists all Chapters",
action="store_true")
parser.add_argument("-lf", "--list-feeds", help="Lists all feeds",
action="store_true")
parser.add_argument("-lu", "--list-users", help="Lists all Users",
action="store_true")
parser.add_argument("-cd", "--create-db", help="Creates DB. Uses Configfile for Naming",
action="store_true")
parser.add_argument("-s", "--start", help="Starts one loop",
action="store_true")
parser.add_argument("--send",
help="Sends Chapter directly by chapter ID. Multiple IDs can be given",
default=[], nargs='*',)
parser.add_argument("--convert",
help="Converts Chapter directly by chapter ID. Multiple IDs can be given",
default=[], nargs='*',)
parser.add_argument("--download",
help="Downloads Chapter directly by chapter ID. Multiple IDs can be given",
default=[], nargs='*',)
parser.add_argument("-p", "--process",
help="Processes chapter(s) by chapter ID, Download, convert, send. Multiple IDs can be given",
default=[], nargs='*',)
parser.add_argument("-a", "--action",
help="Start action. Options are: rssparser (collecting feed data), downloader, converter or sender ")
parser.add_argument("-ss", "--switch-send",
help="Pass ID of User. Switches said user Send eBook status")
parser.add_argument("-dc", "--delete-chapter",
help="Pass ID of Chapter. Deletes said Chapter")
parser.add_argument("-du", "--delete-user",
help="Pass ID of User. Deletes said User")
parser.add_argument("-df", "--delete-feed",
help="Pass ID of Feed. Deletes said Feed")
parser.add_argument("--daemon", help="Run as daemon",
action="store_true")
parser.add_argument("-d", "--debug", help="Debug Mode",
action="store_true")
parser.add_argument('-v', '--version',
action='version', version='%(prog)s ' + __version__)
parser.add_argument('-f', '--filter',
help='Adds a filter(python regex format), to filter the title of any manga parsed. Example: "(?i)one-punch"',
nargs=1, metavar='"filter_regex"')
parser.add_argument('-fl', '--filter-list',
help='Lists all filters',
action='store_true')
self.args = parser.parse_args()
# Logging
if self.args.debug:
outputlevel = "debug"
else:
outputlevel = "info"
helper.initialize_logger("log/", outputlevel)
# Check if Arguments are passed or not. At least one is required
if self.args.action is None \
and self.args.add_feed is None \
and self.args.delete_chapter is None \
and self.args.delete_feed is None \
and self.args.delete_user is None \
and self.args.switch_send is None \
and self.args.add_user is False \
and self.args.list_manga is None \
and self.args.filter is None \
and self.args.filter_list is None \
and not any([self.args.add_user,
self.args.create_db,
self.args.daemon,
self.args.list_chapters,
self.args.list_chapters_all,
self.args.list_feeds,
self.args.list_users,
self.args.download,
self.args.convert,
self.args.send,
self.args.process,
self.args.start,]):
logging.error("At least one argument is required!")
logging.debug("Passed arguments: \n %s", self.args)
'''
Catch -af/--add-feed
'''
def save_feed_to_db(self):
logging.debug("Entered URL: %s", self.args.add_feed)
if validators.url(self.args.add_feed):
helper.writeFeed(self.args.add_feed)
else:
logging.error("You need to enter an URL!")
'''
Catch -f/--filter
'''
def add_filter(self):
if len(self.args.filter) > 0:
filter_value = self.args.filter[0]
logging.debug("Entered filter: %s", filter_value)
helper.writeFilter(filter_value)
else:
logging.error("You need to enter a filter value!")
'''
Catch -s/--switch-user
'''
def switch_user_status(self):
logging.debug("Entered USERID: %s", self.args.switch_send)
helper.switchUserSend(self.args.switch_send)
'''
Delete Stuff functions
'''
def delete_user(self):
logging.debug("Entered USERID: %s", self.args.delete_user)
helper.deleteUser(self.args.delete_user)
def delete_chapter(self):
logging.debug("Entered USERID: %s", self.args.delete_chapter)
helper.deleteChapter(self.args.delete_chapter)
def delete_feed(self):
logging.debug("Entered USERID: %s", self.args.delete_feed)
helper.deleteFeed(self.args.delete_feed)
'''
Catch -lf/--list-feeds
'''
def list_feeds(self):
helper.printFeeds()
'''
Catch -fl/--filter-list
'''
def filter_list(self):
helper.printFilters()
'''
Catch -L/--list-chapters-all
'''
def list_all_chapters(self):
helper.printChaptersAll()
'''
Catch -l/--list-chapters
'''
def list_chapters(self):
helper.printChapters()
'''
Catch -lm/--list-manga
'''
def list_manga(self):
helper.printManga(self.args)
'''
Catch --list-users
'''
def list_users(self):
helper.printUsers()
'''
Catch -u/--add-user
'''
def add_user(self):
helper.createUser()
'''
Catch -cd/--create-db
'''
def create_db(self):
helper.createDB()
'''
Catch -a / --action
'''
def start_action(self):
# Start downloader
if self.args.action == "downloader":
logging.info("Starting downloader to get all outstanding/selected chapters")
self.images_fetcher()
logging.info("Finished downloading all chapters.")
elif self.args.action == "rssparser":
logging.info("Action '%s' is not yet implemented.", self.args.action)
elif self.args.action == "converter":
logging.info("Starting converter to convert all outstanding/selected chapters")
self.image_converter()
logging.info("Finished converting all chapters!")
elif self.args.action == "sender":
logging.info("Starting sender to send all outstanding/selected chapters")
self.send_ebooks()
logging.info("Finished sending all chapters!")
else:
logging.info("%s is not a valid action. Choose between 'rssparser', 'downloader', 'converter' or 'sender'", self.args.action)
'''
direct callers
'''
def send_chapter(self):
msender.directSender(self.args.send)
def convert_chapter(self):
mconverter.directConverter(self.args.convert)
def download_chapter(self):
mdownloader.directDownloader(self.args.download)
def process_chapter(self):
mdownloader.directDownloader(self.args.process)
mconverter.directConverter(self.args.process)
msender.directSender(self.args.process)
'''
This are the worker, one round
'''
# Worker to get and parse rss feeds
def parse_add_feeds(self):
mparser.RssParser()
# Worker to fetch all images
def images_fetcher(self):
mdownloader.downloader(self.args)
# Worker to convert all downloaded chapters into ebooks
def image_converter(self):
mconverter.ConverterHandler(self.args)
# Worker to convert all downloaded chapters into ebooks
def send_ebooks(self):
msender.SenderHandler(self.args)
'''
Application Run & Daemon loop
'''
def run(self):
if self.args.add_feed:
self.save_feed_to_db()
return
if self.args.switch_send:
self.switch_user_status()
return
if self.args.list_feeds:
self.list_feeds()
return
if self.args.list_chapters_all:
self.list_all_chapters()
return
if self.args.list_chapters:
self.list_chapters()
return
if self.args.list_manga:
self.list_manga()
return
if self.args.add_user:
self.add_user()
return
if self.args.list_users:
self.list_users()
return
if self.args.delete_user:
self.delete_user()
return
if self.args.delete_chapter:
self.delete_chapter()
return
if self.args.delete_feed:
self.delete_feed()
return
if self.args.create_db:
self.create_db()
return
if self.args.action:
self.start_action()
return
if self.args.send:
self.send_chapter()
return
if self.args.download:
self.download_chapter()
return
if self.args.convert:
self.convert_chapter()
return
if self.args.process:
self.process_chapter()
return
if self.args.filter:
self.add_filter()
return
if self.args.filter_list:
self.filter_list()
return
# Mainloop
if self.args.start:
daemon = True
while daemon:
if self.args.daemon:
logging.info("Don't forget that the daemon only handles data younger than 24h Hours!")
else:
daemon = False
logging.info("#########################")
logging.info("Starting Loop at %s", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
logging.info("Starting RSS Data Fetcher!")
self.parse_add_feeds()
logging.info("Finished Loading RSS Data")
logging.info("Starting all outstanding Chapter Downloads!")
self.images_fetcher()
logging.info("Finished all outstanding Chapter Downloads")
logging.info("Starting recursive image conversion!")
self.image_converter()
logging.info("Finished recursive image conversion!")
logging.info("Starting to send all ebooks!")
self.send_ebooks()
logging.info("Finished sending ebooks!")
if daemon:
logging.info("Sleeping for %s seconds...\n", (self.config["Sleep"]))
time.sleep(int(self.config["Sleep"]))
# Execute Main
if __name__ == '__main__':
me = M2em()
me.run()