2020-01-16 19:25:19 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from tqdm import tqdm
|
|
|
|
import requests
|
|
|
|
import shutil
|
|
|
|
import simplejson.errors
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
2020-02-26 00:37:11 +01:00
|
|
|
raise Exception("sys.argv[1]")
|
|
|
|
|
2020-01-16 19:25:19 +01:00
|
|
|
|
|
|
|
def parse_chapter_api(data):
|
2020-02-26 00:37:11 +01:00
|
|
|
if not data["server"].startswith("http"):
|
|
|
|
data["server"] = "https://mangadex.org/data/"
|
|
|
|
base_url = data["server"] + data["hash"] + "/"
|
|
|
|
return list(map(lambda page: base_url + page, data["page_array"]))
|
|
|
|
|
2020-01-16 19:25:19 +01:00
|
|
|
|
|
|
|
for chapter in tqdm(sys.argv[1:]):
|
|
|
|
chapter_api_response = requests.get(
|
2020-02-26 00:37:11 +01:00
|
|
|
f"https://mangadex.org/api/?id={chapter}&type=chapter"
|
2020-01-16 19:25:19 +01:00
|
|
|
)
|
|
|
|
|
2020-02-26 00:37:11 +01:00
|
|
|
chapter_number = chapter_api_response.json()["chapter"]
|
2020-01-16 19:25:19 +01:00
|
|
|
|
|
|
|
os.makedirs(chapter_number, exist_ok=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
for image in tqdm(parse_chapter_api(chapter_api_response.json())):
|
|
|
|
r = requests.get(image, stream=True)
|
2020-02-26 00:37:11 +01:00
|
|
|
with open(chapter_number + "/" + image.split("/")[-1], "wb") as f:
|
2020-01-16 19:25:19 +01:00
|
|
|
r.raw.decode_content = True
|
|
|
|
shutil.copyfileobj(r.raw, f)
|
|
|
|
except simplejson.errors.JSONDecodeError as e:
|
|
|
|
if chapter_api_response.status_code != 200:
|
2020-02-26 00:37:11 +01:00
|
|
|
raise Exception(
|
|
|
|
f"API request failed with HTTP status code {chapter_api_response.status_code}"
|
|
|
|
) from None
|
2020-01-16 19:25:19 +01:00
|
|
|
else:
|
|
|
|
raise e
|