-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovers.py
58 lines (47 loc) · 1.81 KB
/
covers.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
import requests, os, sys
# Parse args
if len(sys.argv) == 1:
print("Usage: python covers.py [manga_id] [locale]")
exit()
manga = sys.argv[1]
print("Manga ID: " + manga)
locale = None
if len(sys.argv) == 3:
locale = sys.argv[2].lower()
print("Locale: " + locale)
else:
print("No locale specified, downloading covers for all locales")
# Initialize variables
offset = 0
total = 100
# Start
while offset<total:
# Make the API request
url = f'https://api.mangadex.org/cover?limit=100&manga[]={manga}&order[volume]=asc&offset={offset}{"&locales[]=" + locale if locale is not None else ""}'
headers = {'accept': 'application/json'}
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code != 200:
print(f"Error: {response.status_code}")
exit()
# Loop through the covers and download the ones that match the criteria
resJson = response.json()
offset = resJson['offset']+resJson['limit']
total = resJson['total']
covers = resJson['data']
for cover in covers:
if cover['type'] != 'cover_art':
continue
attributes = cover['attributes']
ext = ' [' + attributes['locale'].upper() + '].' + attributes['fileName'].split('.')[-1]
volume = str(attributes['volume']).zfill(2)
file_name = f"Volume {volume}{ext}"
url_file_name = f"https://mangadex.org/covers/{manga}/{attributes['fileName']}"
# Create the manga directory if it doesn't exist
if not os.path.exists(manga):
os.makedirs(manga)
# Download the cover
response = requests.get(url_file_name)
with open(os.path.join(manga, file_name), 'wb') as f:
f.write(response.content)
print(f"Downloaded {file_name}")