forked from pokhym/mdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
349 lines (292 loc) · 9.77 KB
/
handler.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
from title_metadata import TitleMetadata
from constants import *
import logging
import time
from os.path import abspath, join as path_join, exists, isdir
from os import makedirs
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
class Handler:
def __init__(self, tid, source_name):
"""
Member Variables
-----------------
tid: int
Thread id helps with identifying the Handler in the log
source_name: str
Name of the source defined as SOURCE_XX in constants.py
metadata: TitleMetadata
Store metadata about this title
downloaded_blobs_set: Set[str]
Set of downloaded blob links for the currently being handled chapter
download_title_abs_base_path: Union[None, str]
Absolute base path
download_chapter_rel_base_path: Union[None, str]
Relative base path to the current chapter download
current_title_base_url: Union[None, str]
Base URL of the title
current_chapter_base_url: Union[None, str]
Base URL of the chapter
current_download_image_number: int
A number representing the page number currently being downloaded
driver:
Firefox driver from Selenium
"""
self.tid = tid
self.source_name = source_name
self.metadata = TitleMetadata()
self.downloaded_blobs_set = set()
self.download_title_abs_base_path = None
self.download_chapter_rel_base_path = None
self.current_title_base_url = None
self.current_chapter_base_url = None
self.current_download_image_number = 1
self.driver = None
def get_tid(self):
"""
Returns the thread id as "tidX" as a str
"""
return "tid" + str(self.tid)
def save_screenshot(self):
self.driver.get_screenshot_as_file(self.get_tid + ".png")
def start_driver(self):
"""
Creates a new driver
"""
logging.info("[" + self.get_tid() + " start_driver]: Starting " + self.source_name + " driver")
# Initialize selenium
# Define the Chrome webdriver options
options = webdriver.FirefoxOptions()
# options.binary_location = r'/usr/bin/firefox'
options.add_argument("--headless") # Set the Chrome webdriver to run in headless mode for scalability
options.add_argument("--enable-javascript")
# By default, Selenium waits for all resources to download before taking actions.
# However, we don't need it as the page is populated with dynamically generated JavaScript code.
options.page_load_strategy = "none"
geckodriver_path = "/snap/bin/geckodriver" # specify the path to your geckodriver
driver_service = Service(executable_path=geckodriver_path)
# Pass the defined options objects to initialize the web driver
self.driver = Firefox(options=options, service=driver_service)
# Set an implicit wait of 5 seconds to allow time for elements to appear before throwing an exception
self.driver.implicitly_wait(SLEEP_SEC)
def terminate_driver(self):
"""
Terminates the Selenium driver
"""
logging.info("[" + self.get_tid() + " terminate_driver]: Terminating " + self.source_name + " driver")
self.driver.close()
self.driver = None
def reset_for_next_title(self):
"""
Resets all member variables for handling the next title
"""
self.downloaded_blobs_set.clear()
self.download_title_abs_base_path = None
self.download_chapter_rel_base_path = None
self.current_title_base_url = None
self.current_chapter_base_url = None
self.current_download_image_number = 1
def init_for_title(self, title_abs_base_path, title_base_url):
"""
Initialize member variables for title and creates folders
"""
assert(self.driver == None)
self.start_driver()
# Load the URL
logging.info("[" + self.get_tid() + " init_for_title]: Loading title url: " + title_base_url)
self.current_title_base_url = title_base_url
self.driver.get(self.current_title_base_url)
time.sleep(SLEEP_SEC)
# Obtain the title
self.extract_title_name()
# Join the base path with the name of the manga
self.download_title_abs_base_path = abspath(title_abs_base_path)
self.download_title_abs_base_path = path_join(self.download_title_abs_base_path, self.metadata.get_title())
if not exists(self.download_title_abs_base_path):
logging.info("[" + self.get_tid() + " init_for_title]: Creating base title folder at: " + self.download_title_abs_base_path)
try:
makedirs(self.download_title_abs_base_path)
except Exception as e:
self.save_screenshot()
raise Exception("Unable to create base title folder at: " + self.download_title_abs_base_path)
else:
assert(exists(self.download_title_abs_base_path))
assert(isdir(self.download_title_abs_base_path))
logging.info("[" + self.get_tid() + " init_for_title]: Base title folder exists at: " + self.download_title_abs_base_path)
# logging.info("[" + self.get_tid() + " init_for_title]: Loading title url: " + title_base_url)
self.current_title_base_url = title_base_url
self.driver.get(self.current_title_base_url)
time.sleep(SLEEP_SEC)
self.terminate_driver()
def reset_for_next_chapter(self):
"""
Resets all member variables for handling the
next chapter and creates folders
"""
self.downloaded_blobs_set.clear()
self.download_chapter_rel_base_path = None
self.current_chapter_base_url = None
self.current_download_image_number = 1
def init_for_chapter(self, chapter_base_rel_path, chapter_url):
"""
Initialize member variables for chapter
"""
self.download_chapter_rel_base_path = chapter_base_rel_path
self.current_chapter_base_url = chapter_url
joined_path = path_join(self.download_title_abs_base_path, self.download_chapter_rel_base_path)
if not exists(joined_path):
logging.info("[" + self.get_tid() + " init_for_chapter]: Creating chapter folder at: " + joined_path)
try:
makedirs(joined_path)
except Exception as e:
self.save_screenshot()
raise Exception("Unable to create chapter folder at: " + joined_path)
else:
assert(exists(joined_path))
assert(isdir(joined_path))
logging.info("[" + self.get_tid() + " init_for_chapter]: Chapter folder exists at: " + joined_path)
self.current_chapter_base_url = chapter_url
self.driver.get(self.current_chapter_base_url)
time.sleep(SLEEP_SEC)
logging.info("[" + self.get_tid() + " init_for_chapter]: (Title: " + self.metadata.get_title() + ", Chapter: " + self.download_chapter_rel_base_path + ") Loading chapter url: " + chapter_url)
def extract_current_page(self):
"""
Gets the current page number
Implemented for:
- MangaDex
Returns
-------------------
current_chapter_page: int
"""
pass
def extract_total_pages(self):
"""
Gets the total number of pages in a chapter
Implemented for:
- MangaDex
Returns
--------------------
total_chapter_pages: int
"""
def extract_single_page(self):
"""
Downloads a single page's image
Implemented for:
- MangaDex
"""
pass
def extract_chapter_images(self):
"""
Downloads a full chapter
Implemented for:
- MangaDex
"""
pass
def create_comic_info(self):
"""
Creates and saves a ComicInfo.xml
Impleented for:
- MangaDex
"""
pass
def create_cbz(self):
"""
Zips the currently operating folder and creates
a cbz in its place then deletes the chapter folder
Implemented for:
- MangaDex
"""
pass
def extract_title_name(self):
"""
Obtain the title name
Implemented for:
- MangaDex
"""
pass
def extract_title_url(self):
"""
Set the title url
"""
self.metadata.set_title_url(self.current_title_base_url)
def extract_description(self):
"""
Obtain the description
Implemented for:
- MangaDex
"""
pass
def extract_categories(self):
"""
Returns the categories for the current title
Implemented for:
- MangaDex
"""
pass
def extract_chapter_numbers(self):
"""
Returns the chapter numbers (and titles) of
the current title
Implemented for:
- MangaDex
"""
pass
def extract_cover(self):
"""
Get the cover image and save it to the
base directory as cover.jpg
Implemented for:
- MangaDex
"""
pass
def save_metadata(self):
"""
Saves metadata to file in the base directory
"""
joined = path_join(self.download_title_abs_base_path, METADATA_FILE_NAME)
with open(joined, "w") as fd:
fd.write(self.metadata.dump())
fd.close()
def extract_metadata(self):
"""
Obtains metadata for the title.
Implementation should wrap other metadata extraction
functions
Implemented for:
- MangaDex
"""
self.extract_title_name()
self.extract_title_url()
self.extract_description()
self.extract_categories()
self.extract_chapter_numbers()
self.extract_cover()
self.save_metadata()
def is_url_valid_source(self, url):
"""
Checks if the URL is actually from the accepted list of
links. For instance if a url is from MangaDex the link
should only go to a valid MangaDex chapter and not one for
J-Novel.
eg. Ignore https://j-novel.club/read/XXXX
eg. Allow https://mangadex.org/chapter/XXXX
"""
if self.source_name == SOURCE_MANGADEX:
if SOURCE_MANGADEX_BASE_URL in url:
return True
else:
return False
else:
assert(0)
return False
def get_update(self):
"""
Checks for new chapters and if required
downloads them
Implemented for:
- MangaDex
"""
pass