2021-12-19 17:20:34 +01:00
|
|
|
import requests
|
|
|
|
import re
|
|
|
|
|
|
|
|
api_url = 'https://api.mangadex.org'
|
|
|
|
|
|
|
|
def get_manga_uuid(manga_url):
|
|
|
|
|
|
|
|
# isolate id from url
|
|
|
|
md_id_regex = re.compile('[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}')
|
|
|
|
# check for new mangadex id
|
|
|
|
if md_id_regex.search(manga_url):
|
|
|
|
uuid = md_id_regex.search(manga_url)[0]
|
|
|
|
else:
|
|
|
|
print('No valid id found')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
# check if the manga exists
|
|
|
|
try:
|
|
|
|
req = requests.get(f'{api_url}/manga/{uuid}')
|
|
|
|
except:
|
|
|
|
print('Error. Maybe the MangaDex API is down?')
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
# check mangadex status
|
|
|
|
response = req.json()['result']
|
|
|
|
if response == 'ok':
|
|
|
|
return uuid
|
|
|
|
else:
|
|
|
|
print('Manga not found')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_manga_title(uuid, lang):
|
|
|
|
|
|
|
|
req = requests.get(f'{api_url}/manga/{uuid}')
|
|
|
|
api_resp = req.json()
|
|
|
|
|
|
|
|
try:
|
|
|
|
title = api_resp['data']['attributes']['title'][lang]
|
|
|
|
except:
|
|
|
|
# search in alt titles
|
|
|
|
try:
|
|
|
|
alt_titles = {}
|
|
|
|
for val in api_resp['data']['attributes']['altTitles']:
|
|
|
|
alt_titles.update(val)
|
|
|
|
title = alt_titles[lang]
|
|
|
|
except: # no title on requested language found
|
|
|
|
print('Chapter in requested language not found.')
|
|
|
|
exit(1)
|
|
|
|
|
2021-12-20 14:48:39 +01:00
|
|
|
return fix_name(title)
|
2021-12-19 17:20:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_manga_chapters(uuid, lang):
|
|
|
|
content_ratings = 'contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic'
|
|
|
|
chap_data_list = []
|
|
|
|
|
|
|
|
req = requests.get(f'{api_url}/manga/{uuid}/feed?limit=0&translatedLanguage[]={lang}&{content_ratings}')
|
|
|
|
try:
|
|
|
|
total = req.json()['total']
|
2021-12-20 15:00:12 +01:00
|
|
|
#print(f'Total chapters: {total}')
|
2021-12-19 17:20:34 +01:00
|
|
|
except:
|
|
|
|
print('Error retrieving the chapters list. Did you specify a valid language code?')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if total == 0:
|
|
|
|
print('No chapters available to download!')
|
|
|
|
exit(0)
|
|
|
|
|
2021-12-20 14:29:40 +01:00
|
|
|
last_chap = ['', '']
|
2021-12-19 17:20:34 +01:00
|
|
|
offset = 0
|
|
|
|
while offset < total: # if more than 500 chapters!
|
|
|
|
req = requests.get(f'{api_url}/manga/{uuid}/feed?order[chapter]=asc&order[volume]=asc&limit=500&translatedLanguage[]={lang}&offset={offset}&{content_ratings}')
|
|
|
|
for chapter in req.json()['data']:
|
|
|
|
chap_num = chapter['attributes']['chapter']
|
2021-12-20 14:29:40 +01:00
|
|
|
chap_vol = chapter['attributes']['volume']
|
2021-12-19 17:20:34 +01:00
|
|
|
chap_uuid = chapter['id']
|
|
|
|
chap_hash = chapter['attributes']['hash']
|
|
|
|
chap_data = chapter['attributes']['data']
|
|
|
|
chap_name = chapter['attributes']['title']
|
|
|
|
if not chap_name == None:
|
2021-12-20 14:48:39 +01:00
|
|
|
chap_name = fix_name(chap_name)
|
2021-12-19 17:20:34 +01:00
|
|
|
# check if the chapter is external (cant download them)
|
|
|
|
chap_external = chapter['attributes']['externalUrl']
|
|
|
|
# name chapter "oneshot" if there is no chapter number
|
|
|
|
if chap_external == None and chap_num == None:
|
2021-12-20 14:29:40 +01:00
|
|
|
# check for duplicates
|
|
|
|
if last_chap[0] == chap_vol and last_chap[1] == chap_num:
|
|
|
|
continue
|
|
|
|
chap_data_list.append([chap_vol, 'Oneshot', chap_uuid, chap_hash, chap_name, chap_data])
|
2021-12-19 17:20:34 +01:00
|
|
|
# else add chapter number
|
|
|
|
elif chap_external == None:
|
2021-12-20 14:29:40 +01:00
|
|
|
# check for duplicates
|
|
|
|
if last_chap[0] == chap_vol and last_chap[1] == chap_num:
|
|
|
|
continue
|
|
|
|
chap_data_list.append([chap_vol, chap_num, chap_uuid, chap_hash, chap_name, chap_data])
|
|
|
|
last_chap = [chap_vol, chap_num]
|
2021-12-19 17:20:34 +01:00
|
|
|
offset += 500
|
|
|
|
|
|
|
|
#chap_list.sort() # sort numerically by chapter #
|
|
|
|
|
|
|
|
return chap_data_list
|
|
|
|
|
|
|
|
|
2021-12-20 14:48:39 +01:00
|
|
|
def fix_name(name):
|
|
|
|
# remove illegal characters
|
|
|
|
name = re.sub('[/<>:"\\|?*!.]', '', name)
|
|
|
|
# remove trailing space
|
|
|
|
name = re.sub('[ \t]+$', '', name)
|
|
|
|
|
|
|
|
return name
|
|
|
|
|