Skip to content

Commit e4e3c30

Browse files
committed
filename chapter range fix for single and last
fix repeated range x..last when x = last available chapter
1 parent 2d52e59 commit e4e3c30

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

manga.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def install_dependencies(dependencies_file):
4646
DIRECTORY_KEEP = FILENAME_KEEP | set(['/'])
4747
EXTENSION_KEEP = set('.')
4848

49-
scraper = cloudscraper.create_scraper()
49+
SCRAPER = cloudscraper.create_scraper()
5050

5151
def set_args():
5252
global args
@@ -136,7 +136,7 @@ def download(filename, url, directory='.', extension='png', text='', ok=200):
136136
separation = ' ' * (20 - len(text))
137137
print_colored(f'{text}{separation}- Already exists', Fore.YELLOW)
138138
return False
139-
req = scraper.get(url)
139+
req = SCRAPER.get(url)
140140
if success(req, text, ok, print_ok=bool(text)):
141141
data = req.content
142142
write_file(path, data)
@@ -202,7 +202,7 @@ def parse_chapters_range(chapters, last):
202202
def get_number(number):
203203
return last if number == 'last' else int(number)
204204

205-
ranges = chapters.split(',')
205+
ranges = chapters.replace(' ', '').split(',')
206206
try:
207207
for r in ranges:
208208
split = r.split('..')
@@ -213,10 +213,22 @@ def get_number(number):
213213
except ValueError:
214214
error('Invalid chapters format')
215215

216-
def filename_chapter_range():
217-
min_chapter = min(CHAPTERS)
218-
max_chapter = max(CHAPTERS)
219-
return str(min_chapter) if min_chapter == max_chapter else f'{min_chapter}-{max_chapter}'
216+
def filename_chapter_range(sorted_chapters):
217+
ranges = [] # list[(start, end)]
218+
219+
if len(sorted_chapters) > 0:
220+
start_chapter = sorted_chapters[0]
221+
end_chapter = start_chapter
222+
223+
for chapter in sorted_chapters:
224+
if chapter > end_chapter + 1:
225+
ranges.append((start_chapter, end_chapter))
226+
start_chapter = chapter
227+
end_chapter = chapter
228+
229+
ranges.append((start_chapter, end_chapter))
230+
231+
return ','.join(map(lambda range: f'{range[0]}-{range[1]}' if range[0] != range[1] else str(range[0]), ranges))
220232

221233
def split_rotate_2_pages(rotate):
222234
return str(1 if rotate else 0)
@@ -292,7 +304,7 @@ def online_search():
292304
}
293305

294306
# Alternative Search: https://inmanga.com/OnMangaQuickSearch/Source/QSMangaList.json
295-
search = scraper.post(SEARCH_URL, data=data, headers=headers)
307+
search = SCRAPER.post(SEARCH_URL, data=data, headers=headers)
296308
exit_if_fails(search)
297309

298310
return BeautifulSoup(search.content, 'html.parser').find_all("a", href=True, recursive=False)
@@ -367,7 +379,7 @@ def online_search():
367379
if args.cache:
368380
all_chapters = [int(chapter[0]) for chapter in folders(directory)]
369381
else:
370-
chapters_json = scraper.get(CHAPTERS_WEBSITE + uuid)
382+
chapters_json = SCRAPER.get(CHAPTERS_WEBSITE + uuid)
371383
exit_if_fails(chapters_json)
372384
chapters_full = load_json(chapters_json.content, 'data', 'result')
373385
all_chapters = [int(chapter['Number']) for chapter in chapters_full]
@@ -377,10 +389,11 @@ def online_search():
377389
print_colored(f'Last downloaded chapter: {last}', Fore.YELLOW, Style.BRIGHT);
378390

379391
if args.chapters:
380-
args.chapters = args.chapters.replace(' ', '')
381392
parse_chapters_range(args.chapters, last)
382393
else:
383394
CHAPTERS.update(all_chapters)
395+
396+
CHAPTERS = sorted(CHAPTERS)
384397

385398
if args.cache:
386399
for chapter in CHAPTERS:
@@ -394,7 +407,6 @@ def online_search():
394407
if number in CHAPTERS:
395408
uuids[number] = chapter['Identification']
396409

397-
CHAPTERS = sorted(CHAPTERS)
398410
print_dim(f'{len(CHAPTERS)} chapter{plural(len(CHAPTERS))} will be downloaded - Cancel with Ctrl+C')
399411

400412
if not args.cache:
@@ -410,7 +422,7 @@ def online_search():
410422
url = CHAPTER_PAGES_WEBSITE + uuid
411423

412424
chapter_dir = chapter_directory(manga, chapter)
413-
page = scraper.get(url)
425+
page = SCRAPER.get(url)
414426
if success(page, print_ok=False):
415427
html = BeautifulSoup(page.content, 'html.parser')
416428
pages = html.find(id='PageList').find_all(True, recursive=False)
@@ -427,7 +439,7 @@ def online_search():
427439
extension = f'.{args.format.lower()}'
428440
args.format = args.format.upper()
429441

430-
chapter_range = args.chapters.replace('last', str(last)) if args.chapters else filename_chapter_range()
442+
chapter_range = filename_chapter_range(CHAPTERS)
431443

432444
if args.format != 'PNG':
433445
print_colored(f'Converting to {args.format}...', Fore.BLUE, Style.BRIGHT)
@@ -480,4 +492,6 @@ def online_search():
480492
os.rename(f'{MANGA_DIR}/{chapter}{extension}', path)
481493
print_colored(f'DONE: {os.path.abspath(path)}', Fore.GREEN, Style.BRIGHT)
482494
else:
495+
if len(CHAPTERS) == 1:
496+
directory = os.path.abspath(chapter_directory(manga, CHAPTERS[0]))
483497
print_colored(f'DONE: {directory}', Fore.GREEN, Style.BRIGHT)

0 commit comments

Comments
 (0)