mangapy 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mangapy/__init__.py +3 -0
- mangapy/capabilities.py +9 -0
- mangapy/chapter_archiver.py +229 -0
- mangapy/cli.py +186 -0
- mangapy/download_manager.py +215 -0
- mangapy/fanfox.py +362 -0
- mangapy/mangadex.py +289 -0
- mangapy/mangarepository.py +61 -0
- mangapy/providers.py +36 -0
- mangapy/utils.py +12 -0
- mangapy-3.0.1.dist-info/METADATA +154 -0
- mangapy-3.0.1.dist-info/RECORD +16 -0
- mangapy-3.0.1.dist-info/WHEEL +5 -0
- mangapy-3.0.1.dist-info/entry_points.txt +2 -0
- mangapy-3.0.1.dist-info/licenses/LICENSE +21 -0
- mangapy-3.0.1.dist-info/top_level.txt +1 -0
mangapy/__init__.py
ADDED
mangapy/capabilities.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass(frozen=True)
|
|
5
|
+
class ProviderCapabilities:
|
|
6
|
+
max_parallel_chapters: int = 1
|
|
7
|
+
max_parallel_pages: int = 1
|
|
8
|
+
supports_batch_download: bool = False
|
|
9
|
+
rate_limit: float | None = None # requests per second, if enforced by caller
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import random
|
|
4
|
+
import re
|
|
5
|
+
import sys
|
|
6
|
+
import requests
|
|
7
|
+
import shutil
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from functools import partial
|
|
12
|
+
from tqdm import tqdm
|
|
13
|
+
from PIL import Image
|
|
14
|
+
from urllib.parse import urlparse
|
|
15
|
+
from mangapy.mangarepository import Chapter, Page
|
|
16
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
tqdm.set_lock(threading.RLock())
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ChapterArchiver(object):
|
|
24
|
+
def __init__(self, path: str, max_workers=1, retry_enabled: bool = True, show_progress: bool = True):
|
|
25
|
+
self.max_workers = max_workers
|
|
26
|
+
self.path = Path(path).expanduser()
|
|
27
|
+
self.retry_enabled = retry_enabled
|
|
28
|
+
self.show_progress = show_progress
|
|
29
|
+
self._session_local = threading.local()
|
|
30
|
+
self._pdf_lock_guard = threading.Lock()
|
|
31
|
+
self._pdf_locks = {}
|
|
32
|
+
|
|
33
|
+
def archive(self, chapter: Chapter, pdf: bool, headers: Mapping[str, str | bytes | None] | None):
|
|
34
|
+
chapter_id = getattr(chapter, "chapter_id", None)
|
|
35
|
+
if chapter.number is not None:
|
|
36
|
+
isChapterNumberAnInt = isinstance(chapter.number, int) or chapter.number.is_integer()
|
|
37
|
+
chapter_name = str(int(chapter.number)) if isChapterNumberAnInt else str(chapter.number)
|
|
38
|
+
else:
|
|
39
|
+
chapter_name = chapter_id if chapter_id is not None else "unknown"
|
|
40
|
+
|
|
41
|
+
if pdf:
|
|
42
|
+
pdf_path = self.path.joinpath('pdf')
|
|
43
|
+
pdf_path.mkdir(parents=True, exist_ok=True)
|
|
44
|
+
chapter_pdf_file_path = pdf_path.joinpath(chapter_name + '.pdf')
|
|
45
|
+
lock = self._get_pdf_lock(chapter_pdf_file_path)
|
|
46
|
+
with lock:
|
|
47
|
+
if os.path.isfile(chapter_pdf_file_path):
|
|
48
|
+
print("⏺ {0} already downloaded and it will be skipped.".format(chapter_name))
|
|
49
|
+
return # early exit if the file is already on disk
|
|
50
|
+
self._download_chapter_images(chapter, chapter_name, headers, pdf=True)
|
|
51
|
+
chapter_images_path = self.path.joinpath('.images', chapter_name)
|
|
52
|
+
self._create_chapter_pdf(chapter_images_path, chapter_pdf_file_path)
|
|
53
|
+
shutil.rmtree(chapter_images_path, ignore_errors=True)
|
|
54
|
+
else:
|
|
55
|
+
self._download_chapter_images(chapter, chapter_name, headers, pdf=False)
|
|
56
|
+
|
|
57
|
+
def _fetch_image(self, url: str, headers: Mapping[str, str | bytes | None] | None):
|
|
58
|
+
session = self._get_session()
|
|
59
|
+
if not self.retry_enabled:
|
|
60
|
+
try:
|
|
61
|
+
response = session.get(url, headers=headers, timeout=(10, 30))
|
|
62
|
+
except requests.RequestException as exc:
|
|
63
|
+
logging.error("Failed to download image %s: %s", url, exc)
|
|
64
|
+
return None
|
|
65
|
+
if response.status_code != 200:
|
|
66
|
+
return None
|
|
67
|
+
return response.content
|
|
68
|
+
|
|
69
|
+
last_error = None
|
|
70
|
+
for attempt in range(3):
|
|
71
|
+
try:
|
|
72
|
+
response = session.get(url, headers=headers, timeout=(10, 30))
|
|
73
|
+
except requests.RequestException as exc:
|
|
74
|
+
last_error = exc
|
|
75
|
+
time.sleep(_retry_delay(attempt))
|
|
76
|
+
continue
|
|
77
|
+
|
|
78
|
+
if response.status_code == 200:
|
|
79
|
+
return response.content
|
|
80
|
+
if response.status_code == 429 or response.status_code >= 500:
|
|
81
|
+
time.sleep(_retry_delay(attempt, response))
|
|
82
|
+
continue
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
if last_error:
|
|
86
|
+
logging.error("Failed to download image %s: %s", url, last_error)
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
def _save_image(self, image_path: Path, headers: Mapping[str, str | bytes | None] | None, page: Page):
|
|
90
|
+
file_name = str(page.number)
|
|
91
|
+
image_url = page.url
|
|
92
|
+
file_ext = urlparse(image_url).path.split('.')[-1]
|
|
93
|
+
file_path = image_path.joinpath(file_name + '.' + file_ext)
|
|
94
|
+
if os.path.isfile(file_path):
|
|
95
|
+
return # early exit if the file is already on disk
|
|
96
|
+
|
|
97
|
+
if image_url.startswith('//'):
|
|
98
|
+
image_url = 'https:' + image_url
|
|
99
|
+
|
|
100
|
+
data = self._fetch_image(image_url, headers=headers)
|
|
101
|
+
|
|
102
|
+
if data is None:
|
|
103
|
+
logging.error("Can't download page {0}".format(file_name))
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
with open(file_path, "wb") as output:
|
|
107
|
+
output.write(data)
|
|
108
|
+
|
|
109
|
+
def _create_chapter_pdf(self, chapter_images_path: Path, pdf_path: Path):
|
|
110
|
+
file_list = [path for path in chapter_images_path.glob('**/*') if path.is_file()]
|
|
111
|
+
chapter_images_path = list(map(lambda path: str(path.absolute()), file_list))
|
|
112
|
+
images_path = natural_sort(chapter_images_path)
|
|
113
|
+
|
|
114
|
+
tmp_path = pdf_path.with_name(pdf_path.name + ".tmp")
|
|
115
|
+
if _try_img2pdf(images_path, tmp_path):
|
|
116
|
+
tmp_path.replace(pdf_path)
|
|
117
|
+
return
|
|
118
|
+
|
|
119
|
+
images = []
|
|
120
|
+
for path in images_path:
|
|
121
|
+
image = Image.open(path)
|
|
122
|
+
image.load()
|
|
123
|
+
if image.mode == 'RGBA':
|
|
124
|
+
image = image.convert('RGB')
|
|
125
|
+
images.append(image)
|
|
126
|
+
|
|
127
|
+
images_count = len(images)
|
|
128
|
+
if images_count <= 0:
|
|
129
|
+
return
|
|
130
|
+
first_image = images.pop(0)
|
|
131
|
+
try:
|
|
132
|
+
if images_count == 1:
|
|
133
|
+
first_image.save(tmp_path, "PDF", resolution=100.0, save_all=True)
|
|
134
|
+
else:
|
|
135
|
+
first_image.save(tmp_path, "PDF", resolution=100.0, save_all=True, append_images=images)
|
|
136
|
+
tmp_path.replace(pdf_path)
|
|
137
|
+
finally:
|
|
138
|
+
first_image.close()
|
|
139
|
+
for image in images:
|
|
140
|
+
image.close()
|
|
141
|
+
if tmp_path.exists():
|
|
142
|
+
try:
|
|
143
|
+
tmp_path.unlink()
|
|
144
|
+
except OSError:
|
|
145
|
+
pass
|
|
146
|
+
|
|
147
|
+
def _get_session(self) -> requests.Session:
|
|
148
|
+
session = getattr(self._session_local, "session", None)
|
|
149
|
+
if session is None:
|
|
150
|
+
session = requests.Session()
|
|
151
|
+
self._session_local.session = session
|
|
152
|
+
return session
|
|
153
|
+
|
|
154
|
+
def _get_pdf_lock(self, pdf_path: Path) -> threading.Lock:
|
|
155
|
+
with self._pdf_lock_guard:
|
|
156
|
+
lock = self._pdf_locks.get(pdf_path)
|
|
157
|
+
if lock is None:
|
|
158
|
+
lock = threading.Lock()
|
|
159
|
+
self._pdf_locks[pdf_path] = lock
|
|
160
|
+
return lock
|
|
161
|
+
|
|
162
|
+
def _download_chapter_images(self, chapter: Chapter, chapter_name: str, headers, pdf: bool):
|
|
163
|
+
external_url = getattr(chapter, "external_url", None)
|
|
164
|
+
if external_url:
|
|
165
|
+
print(f"⛔️ {chapter_name} is hosted externally and has no pages on this provider: {external_url}")
|
|
166
|
+
return
|
|
167
|
+
pages_count = getattr(chapter, "pages_count", None)
|
|
168
|
+
if pages_count == 0:
|
|
169
|
+
print(f"⛔️ {chapter_name} has no pages available on this provider.")
|
|
170
|
+
return
|
|
171
|
+
|
|
172
|
+
images_path = self.path.joinpath('.images' if pdf else 'images')
|
|
173
|
+
chapter_images_path = images_path.joinpath(chapter_name)
|
|
174
|
+
chapter_images_path.mkdir(parents=True, exist_ok=True)
|
|
175
|
+
pages = chapter.pages()
|
|
176
|
+
if pages is None or not len(pages):
|
|
177
|
+
print("🆘 {0} doesn't have any pages and it will be skipped.".format(chapter_name))
|
|
178
|
+
return
|
|
179
|
+
|
|
180
|
+
description = ('Chapter {0}'.format(chapter_name))
|
|
181
|
+
func = partial(self._save_image, chapter_images_path, headers) # currying
|
|
182
|
+
|
|
183
|
+
disable_progress = (not self.show_progress) or (not sys.stderr.isatty())
|
|
184
|
+
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
|
185
|
+
for _ in tqdm(
|
|
186
|
+
executor.map(func, pages),
|
|
187
|
+
total=len(pages),
|
|
188
|
+
desc=description,
|
|
189
|
+
unit='pages',
|
|
190
|
+
ncols=100,
|
|
191
|
+
disable=disable_progress,
|
|
192
|
+
):
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def natural_sort(list):
|
|
197
|
+
def convert(text):
|
|
198
|
+
return int(text) if text.isdigit() else text.lower()
|
|
199
|
+
|
|
200
|
+
def alphanum_key(key):
|
|
201
|
+
return [convert(c) for c in re.split('([0-9]+)', key)]
|
|
202
|
+
|
|
203
|
+
return sorted(list, key=alphanum_key)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _retry_delay(attempt: int, response: requests.Response | None = None) -> float:
|
|
207
|
+
if response is not None:
|
|
208
|
+
retry_after = response.headers.get("Retry-After")
|
|
209
|
+
if retry_after and retry_after.isdigit():
|
|
210
|
+
return float(retry_after)
|
|
211
|
+
base = min(2 ** attempt, 5)
|
|
212
|
+
return base + random.uniform(0, 0.2)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _try_img2pdf(image_paths: list[str], tmp_path: Path) -> bool:
|
|
216
|
+
if not image_paths:
|
|
217
|
+
return False
|
|
218
|
+
try:
|
|
219
|
+
import img2pdf
|
|
220
|
+
except Exception:
|
|
221
|
+
return False
|
|
222
|
+
|
|
223
|
+
try:
|
|
224
|
+
with open(tmp_path, "wb") as output:
|
|
225
|
+
output.write(img2pdf.convert(image_paths, dpi=100))
|
|
226
|
+
return True
|
|
227
|
+
except Exception as exc:
|
|
228
|
+
logging.warning("img2pdf failed, falling back to PIL: %s", exc)
|
|
229
|
+
return False
|
mangapy/cli.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import yaml
|
|
3
|
+
import json
|
|
4
|
+
import importlib.metadata
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from mangapy.download_manager import DownloadManager, DownloadRequest
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
version = importlib.metadata.version('mangapy')
|
|
12
|
+
except importlib.metadata.PackageNotFoundError:
|
|
13
|
+
version = '0.0.0'
|
|
14
|
+
default_path_to_download_folder = str(os.path.join(Path.home(), "Downloads", "mangapy"))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def cmd_parse():
|
|
18
|
+
"""Returns parsed arguments from command line"""
|
|
19
|
+
parser = argparse.ArgumentParser()
|
|
20
|
+
|
|
21
|
+
subparsers = parser.add_subparsers(help='Download modes.', dest='mode')
|
|
22
|
+
|
|
23
|
+
yaml_parser = subparsers.add_parser("yaml")
|
|
24
|
+
args_parser = subparsers.add_parser("title")
|
|
25
|
+
|
|
26
|
+
yaml_parser.add_argument('yaml_file', type=str, help="Path to the .yaml file")
|
|
27
|
+
|
|
28
|
+
args_parser.add_argument('manga_title', type=str, help="manga title to download")
|
|
29
|
+
args_parser.add_argument('-s', '--source', type=str, help="manga source")
|
|
30
|
+
args_parser.add_argument('-o', '--out', type=str, default=default_path_to_download_folder, help='download directory')
|
|
31
|
+
args_parser.add_argument('-d', '--debug', action='store_true', help="set log to DEBUG level")
|
|
32
|
+
args_parser.add_argument('--pdf', action='store_true', help="create a pdf for each chapter")
|
|
33
|
+
args_parser.add_argument('--no-retry', action='store_true', help="disable network retries")
|
|
34
|
+
args_parser.add_argument('--no-progress', action='store_true', help="disable progress output")
|
|
35
|
+
|
|
36
|
+
args_parser.add_argument('-p', '--proxy', type=json.loads, help="use a proxy to download the chapters")
|
|
37
|
+
group = args_parser.add_mutually_exclusive_group()
|
|
38
|
+
group.add_argument('-a', '--all', action='store_true', help="download all chapters available")
|
|
39
|
+
group.add_argument('-c', '--chapter', type=str, help="chapter(s) number to download")
|
|
40
|
+
|
|
41
|
+
parser.add_argument('-v', '--version',
|
|
42
|
+
action='version',
|
|
43
|
+
version='{0} {1}'.format(parser.prog, version),
|
|
44
|
+
help="show program version and exit")
|
|
45
|
+
|
|
46
|
+
args = parser.parse_args()
|
|
47
|
+
return args
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def main():
|
|
51
|
+
try:
|
|
52
|
+
args = cmd_parse()
|
|
53
|
+
if args.mode == 'title':
|
|
54
|
+
main_title(args)
|
|
55
|
+
elif args.mode == 'yaml':
|
|
56
|
+
main_yaml(args)
|
|
57
|
+
except KeyboardInterrupt:
|
|
58
|
+
print("\n⛔️ Download canceled by user.")
|
|
59
|
+
sys.exit(130)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def main_yaml(args: argparse.Namespace):
|
|
63
|
+
yaml_file = args.yaml_file.strip()
|
|
64
|
+
|
|
65
|
+
try:
|
|
66
|
+
with open(yaml_file, 'r') as f:
|
|
67
|
+
dictionary = yaml.load(f, Loader=yaml.FullLoader)
|
|
68
|
+
output = dictionary.get('output', default_path_to_download_folder)
|
|
69
|
+
|
|
70
|
+
proxy = None
|
|
71
|
+
if 'proxy' in dictionary.keys() and dictionary['proxy']:
|
|
72
|
+
proxy_info = dictionary['proxy']
|
|
73
|
+
if _is_valid_proxy(proxy_info):
|
|
74
|
+
print('Setting proxy')
|
|
75
|
+
proxy = dictionary['proxy']
|
|
76
|
+
else:
|
|
77
|
+
print('The proxy is not in the right format and it will not be used.')
|
|
78
|
+
|
|
79
|
+
debug_log = bool(dictionary.get('debug', False))
|
|
80
|
+
no_retry = bool(dictionary.get('no_retry', False))
|
|
81
|
+
no_progress = bool(dictionary.get('no_progress', False))
|
|
82
|
+
downloads = _normalize_yaml_downloads(dictionary)
|
|
83
|
+
manager = DownloadManager()
|
|
84
|
+
for entry in downloads:
|
|
85
|
+
title = entry.get('title')
|
|
86
|
+
if not title:
|
|
87
|
+
continue
|
|
88
|
+
request = DownloadRequest(
|
|
89
|
+
title=title.strip(),
|
|
90
|
+
source=_normalize_source(entry.get('source', 'fanfox')),
|
|
91
|
+
output=str(entry.get('output', output)).strip(),
|
|
92
|
+
pdf=bool(entry.get('pdf', False)),
|
|
93
|
+
proxy=entry.get('proxy', proxy),
|
|
94
|
+
no_retry=bool(entry.get('no_retry', no_retry)),
|
|
95
|
+
no_progress=bool(entry.get('no_progress', no_progress)),
|
|
96
|
+
enable_debug_log=bool(entry.get('debug', debug_log)),
|
|
97
|
+
download_all_chapters=bool(entry.get('download_all_chapters', False)),
|
|
98
|
+
download_last_chapter=bool(entry.get('download_last_chapter', False)),
|
|
99
|
+
download_single_chapter=entry.get('download_single_chapter'),
|
|
100
|
+
download_chapters=entry.get('download_chapters'),
|
|
101
|
+
options=_extract_options(entry),
|
|
102
|
+
)
|
|
103
|
+
manager.download(request)
|
|
104
|
+
except Exception as error:
|
|
105
|
+
print(error)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def main_title(args: argparse.Namespace):
|
|
109
|
+
source = _normalize_source(args.source) if args.source else 'fanfox'
|
|
110
|
+
proxy = None
|
|
111
|
+
if args.proxy:
|
|
112
|
+
if _is_valid_proxy(args.proxy):
|
|
113
|
+
print('Setting proxy')
|
|
114
|
+
proxy = args.proxy
|
|
115
|
+
else:
|
|
116
|
+
print('The proxy is not in the right format and it will not be used.')
|
|
117
|
+
|
|
118
|
+
request = DownloadRequest(
|
|
119
|
+
title=args.manga_title.strip(),
|
|
120
|
+
source=source,
|
|
121
|
+
output=args.out.strip(),
|
|
122
|
+
pdf=args.pdf or False,
|
|
123
|
+
proxy=proxy,
|
|
124
|
+
no_retry=bool(getattr(args, "no_retry", False)),
|
|
125
|
+
no_progress=bool(getattr(args, "no_progress", False)),
|
|
126
|
+
enable_debug_log=args.debug,
|
|
127
|
+
download_all_chapters=bool(args.all),
|
|
128
|
+
download_single_chapter=_parse_single_chapter(args.chapter),
|
|
129
|
+
download_chapters=_parse_chapter_range(args.chapter),
|
|
130
|
+
options=None,
|
|
131
|
+
)
|
|
132
|
+
DownloadManager().download(request)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _parse_chapter_range(value: str | None) -> str | None:
|
|
136
|
+
if not value:
|
|
137
|
+
return None
|
|
138
|
+
parts = value.split('-')
|
|
139
|
+
if len(parts) == 2:
|
|
140
|
+
return value
|
|
141
|
+
return None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _parse_single_chapter(value: str | None) -> str | None:
|
|
145
|
+
if not value:
|
|
146
|
+
return None
|
|
147
|
+
parts = value.split('-')
|
|
148
|
+
if len(parts) == 2:
|
|
149
|
+
return None
|
|
150
|
+
return value.strip()
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _is_valid_proxy(proxy_info: dict) -> bool:
|
|
154
|
+
return 'http' in proxy_info.keys() and 'https' in proxy_info.keys()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _normalize_source(source: str) -> str:
|
|
158
|
+
return source.strip().lower()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _normalize_yaml_downloads(dictionary: dict) -> list[dict]:
|
|
162
|
+
if 'downloads' in dictionary and isinstance(dictionary['downloads'], list):
|
|
163
|
+
return list(dictionary['downloads'])
|
|
164
|
+
|
|
165
|
+
downloads = []
|
|
166
|
+
for key, value in dictionary.items():
|
|
167
|
+
if key in {'debug', 'output', 'proxy'}:
|
|
168
|
+
continue
|
|
169
|
+
if isinstance(value, list):
|
|
170
|
+
for entry in value:
|
|
171
|
+
entry_with_source = dict(entry)
|
|
172
|
+
entry_with_source.setdefault('source', key)
|
|
173
|
+
downloads.append(entry_with_source)
|
|
174
|
+
return downloads
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _extract_options(entry: dict) -> dict | None:
|
|
178
|
+
options = {}
|
|
179
|
+
for key in ("translated_language", "content_rating", "data_saver"):
|
|
180
|
+
if key in entry:
|
|
181
|
+
options[key] = entry.get(key)
|
|
182
|
+
return options or None
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
if __name__ == '__main__':
|
|
186
|
+
main()
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Iterable, List
|
|
7
|
+
|
|
8
|
+
from mangapy.chapter_archiver import ChapterArchiver
|
|
9
|
+
from mangapy.mangarepository import Chapter, Manga
|
|
10
|
+
from mangapy.providers import get_repository
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class DownloadRequest:
|
|
15
|
+
title: str
|
|
16
|
+
source: str
|
|
17
|
+
output: str
|
|
18
|
+
pdf: bool = False
|
|
19
|
+
proxy: dict | None = None
|
|
20
|
+
no_retry: bool = False
|
|
21
|
+
no_progress: bool = False
|
|
22
|
+
enable_debug_log: bool = False
|
|
23
|
+
download_all_chapters: bool = False
|
|
24
|
+
download_last_chapter: bool = False
|
|
25
|
+
download_single_chapter: str | None = None
|
|
26
|
+
download_chapters: str | None = None
|
|
27
|
+
options: dict | None = None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class DownloadManager:
|
|
31
|
+
def download(self, request: DownloadRequest) -> None:
|
|
32
|
+
if request.enable_debug_log:
|
|
33
|
+
logging.getLogger("mangapy").setLevel(logging.DEBUG)
|
|
34
|
+
else:
|
|
35
|
+
logging.getLogger("mangapy").setLevel(logging.ERROR)
|
|
36
|
+
|
|
37
|
+
repository = get_repository(request.source)
|
|
38
|
+
if request.proxy:
|
|
39
|
+
repository.proxies = request.proxy
|
|
40
|
+
if hasattr(repository, "no_retry"):
|
|
41
|
+
repository.no_retry = request.no_retry
|
|
42
|
+
|
|
43
|
+
print(f"🔎 Searching for {request.title} in {request.source}...")
|
|
44
|
+
try:
|
|
45
|
+
manga = repository.search(request.title, options=request.options)
|
|
46
|
+
except Exception as exc:
|
|
47
|
+
logging.error(str(exc))
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
if manga is None or len(manga.chapters) <= 0:
|
|
51
|
+
if manga is None:
|
|
52
|
+
print(f"❌ Manga {request.title} doesn't exist.")
|
|
53
|
+
return
|
|
54
|
+
if request.source == "mangadex":
|
|
55
|
+
options = request.options or {}
|
|
56
|
+
languages = _normalize_option_list(options.get("translated_language"), ["en"])
|
|
57
|
+
ratings = _normalize_option_list(options.get("content_rating"), ["safe", "suggestive", "erotica"])
|
|
58
|
+
language_display = ", ".join(languages) if languages else "none"
|
|
59
|
+
rating_display = ", ".join(ratings) if ratings else "none"
|
|
60
|
+
print(
|
|
61
|
+
f"❌ {manga.title} found, but no chapters matched the requested language(s): {language_display}."
|
|
62
|
+
)
|
|
63
|
+
print(
|
|
64
|
+
"ℹ️ Try a different language via YAML (translated_language) or adjust content_rating: "
|
|
65
|
+
f"{rating_display}."
|
|
66
|
+
)
|
|
67
|
+
return
|
|
68
|
+
print(f"❌ Manga {request.title} has no chapters available.")
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
print(f"✅ {manga.title} found")
|
|
72
|
+
|
|
73
|
+
chapters = _select_chapters(manga, request)
|
|
74
|
+
if not chapters:
|
|
75
|
+
logging.error("❌ Chapter selection is empty.")
|
|
76
|
+
return
|
|
77
|
+
if _all_chapters_external_or_empty(chapters):
|
|
78
|
+
print("⛔️ Title found, but all selected chapters are external links or have no hosted pages.")
|
|
79
|
+
return
|
|
80
|
+
|
|
81
|
+
directory = os.path.join(request.output, request.source, manga.subdirectory)
|
|
82
|
+
headers = repository.image_request_headers()
|
|
83
|
+
max_parallel_pages = repository.capabilities.max_parallel_pages
|
|
84
|
+
|
|
85
|
+
print("⬇️ Download started.")
|
|
86
|
+
if repository.capabilities.max_parallel_chapters > 1 and len(chapters) > 1:
|
|
87
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
88
|
+
|
|
89
|
+
with ThreadPoolExecutor(max_workers=repository.capabilities.max_parallel_chapters) as executor:
|
|
90
|
+
list(
|
|
91
|
+
executor.map(
|
|
92
|
+
lambda ch: _archive_chapter(
|
|
93
|
+
directory,
|
|
94
|
+
max_parallel_pages,
|
|
95
|
+
ch,
|
|
96
|
+
request.pdf,
|
|
97
|
+
headers,
|
|
98
|
+
retry_enabled=not request.no_retry,
|
|
99
|
+
show_progress=not request.no_progress,
|
|
100
|
+
),
|
|
101
|
+
chapters,
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
else:
|
|
105
|
+
archiver = ChapterArchiver(
|
|
106
|
+
directory,
|
|
107
|
+
max_workers=max_parallel_pages,
|
|
108
|
+
retry_enabled=not request.no_retry,
|
|
109
|
+
show_progress=not request.no_progress,
|
|
110
|
+
)
|
|
111
|
+
for chapter in chapters:
|
|
112
|
+
_archive_with_archiver(archiver, chapter, request.pdf, headers)
|
|
113
|
+
|
|
114
|
+
print("🎉 Download finished.")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _archive_chapter(
|
|
118
|
+
directory: str,
|
|
119
|
+
max_parallel_pages: int,
|
|
120
|
+
chapter: Chapter,
|
|
121
|
+
pdf: bool,
|
|
122
|
+
headers,
|
|
123
|
+
retry_enabled: bool,
|
|
124
|
+
show_progress: bool,
|
|
125
|
+
):
|
|
126
|
+
archiver = ChapterArchiver(
|
|
127
|
+
directory,
|
|
128
|
+
max_workers=max_parallel_pages,
|
|
129
|
+
retry_enabled=retry_enabled,
|
|
130
|
+
show_progress=show_progress,
|
|
131
|
+
)
|
|
132
|
+
_archive_with_archiver(archiver, chapter, pdf, headers)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _archive_with_archiver(archiver: ChapterArchiver, chapter: Chapter, pdf: bool, headers):
|
|
136
|
+
try:
|
|
137
|
+
archiver.archive(chapter, pdf, headers)
|
|
138
|
+
except Exception as exc:
|
|
139
|
+
logging.error(str(exc))
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _select_chapters(manga: Manga, request: DownloadRequest) -> List[Chapter]:
|
|
143
|
+
if request.download_all_chapters:
|
|
144
|
+
return list(manga.chapters)
|
|
145
|
+
|
|
146
|
+
if request.download_single_chapter is not None:
|
|
147
|
+
value = request.download_single_chapter.strip()
|
|
148
|
+
number = _parse_number(value)
|
|
149
|
+
for chapter in manga.chapters:
|
|
150
|
+
if number is not None and chapter.number == number:
|
|
151
|
+
return [chapter]
|
|
152
|
+
if chapter.chapter_id == value:
|
|
153
|
+
return [chapter]
|
|
154
|
+
logging.error("❌ Chapter doesn't exist.")
|
|
155
|
+
return []
|
|
156
|
+
|
|
157
|
+
if request.download_chapters is not None:
|
|
158
|
+
try:
|
|
159
|
+
begin, end = _parse_range(request.download_chapters)
|
|
160
|
+
except ValueError as exc:
|
|
161
|
+
logging.error(str(exc))
|
|
162
|
+
return []
|
|
163
|
+
if begin is None:
|
|
164
|
+
logging.error("❌ Invalid chapter range.")
|
|
165
|
+
return []
|
|
166
|
+
selected: List[Chapter] = []
|
|
167
|
+
for chapter in manga.chapters:
|
|
168
|
+
if chapter.number is None:
|
|
169
|
+
continue
|
|
170
|
+
if chapter.number < begin:
|
|
171
|
+
continue
|
|
172
|
+
if end is not None and chapter.number > end:
|
|
173
|
+
continue
|
|
174
|
+
selected.append(chapter)
|
|
175
|
+
return selected
|
|
176
|
+
|
|
177
|
+
return [manga.last_chapter]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _parse_range(value: str) -> tuple[float | None, float | None]:
|
|
181
|
+
parts = value.split("-")
|
|
182
|
+
if len(parts) != 2:
|
|
183
|
+
return None, None
|
|
184
|
+
begin = _parse_number(parts[0])
|
|
185
|
+
end = _parse_number(parts[1]) if parts[1] else None
|
|
186
|
+
if begin is not None and end is not None and begin > end:
|
|
187
|
+
raise ValueError("invalid chapter interval, the end should be bigger than start")
|
|
188
|
+
return begin, end
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _parse_number(value: str) -> float | None:
|
|
192
|
+
try:
|
|
193
|
+
return float(value.strip())
|
|
194
|
+
except ValueError:
|
|
195
|
+
return None
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _normalize_option_list(value, default: list[str]) -> list[str]:
|
|
199
|
+
if value is None:
|
|
200
|
+
return list(default)
|
|
201
|
+
if isinstance(value, list):
|
|
202
|
+
return [str(item) for item in value if item]
|
|
203
|
+
return [str(value)]
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _all_chapters_external_or_empty(chapters: Iterable[Chapter]) -> bool:
|
|
207
|
+
for chapter in chapters:
|
|
208
|
+
external_url = getattr(chapter, "external_url", None)
|
|
209
|
+
if external_url:
|
|
210
|
+
continue
|
|
211
|
+
pages_count = getattr(chapter, "pages_count", None)
|
|
212
|
+
if pages_count == 0:
|
|
213
|
+
continue
|
|
214
|
+
return False
|
|
215
|
+
return True
|