From 5c6bd86b6985b47ebefc45a10eeb35cfc9312000 Mon Sep 17 00:00:00 2001 From: Karey Higuera Date: Tue, 5 Jul 2022 11:03:20 -0400 Subject: [PATCH] add option for naming folders with chapter name --- README.md | 26 +++++++++++++++++++++++++- src/options.py | 3 +++ src/webtoon_downloader.py | 12 ++++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c3efe9d..6b104d7 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,30 @@ For example, downloading Tower of God, Chapter 150 would result in the following │... ``` +* In order to use chapter names instead of chapter numbers for separate folders, pass the ```--chapter-name``` argument along with ```--separate```. + ```ps + $ python webtoon_downloader.py [url] --separate --chapter-name + ``` +For example, downloading Tower of God, Chapter 150 to 152 with this option would result in the following: + ```ps + Tower_of_God + │[Season 2] Ep. 70 + │--150_001.jpg + │--150_002.jpg + │--150_003.jpg + │... + │[Season 2] Ep. 71 + │--151_001.jpg + │--151_002.jpg + │--151_003.jpg + │... + │[Season 2] Ep. 72 + │--152_001.jpg + │--152_002.jpg + │--152_003.jpg + │... + ``` + For more details on positional arguments, please use the ```-h ``` or ```--help``` argument: ```console py webtoon_downloader.py --help @@ -188,4 +212,4 @@ Project Link: [https://github.com/Zehina/Webtoon-Downloader](https://github.com/ [license-shield]: https://img.shields.io/github/license/Zehina/repo.svg?style=for-the-badge [license-url]: https://github.com/Zehina/Webtoon-Downloader/blob/master/LICENSE.txt [linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555 -[linkedin-url]: https://linkedin.com/in/Zehina \ No newline at end of file +[linkedin-url]: https://linkedin.com/in/Zehina diff --git a/src/options.py b/src/options.py index 6cd3f19..9df503f 100644 --- a/src/options.py +++ b/src/options.py @@ -50,6 +50,9 @@ def initialize(self): self.parser.add_argument('--separate', required=False, help='download each chapter in seperate folders', action='store_true', default=False) + self.parser.add_argument('--chapter-name', required=False, + help='use the chapter title for the separate folder names', + action='store_true', default=False) self.parser.add_argument('--readme', '-r', help=('displays readme file content for ' 'more help details'), required=False, action='store_true') self.parser._positionals.title = "commands" diff --git a/src/webtoon_downloader.py b/src/webtoon_downloader.py index fac4af7..1351a77 100644 --- a/src/webtoon_downloader.py +++ b/src/webtoon_downloader.py @@ -330,7 +330,7 @@ def download_chapter(chapter_download_task_id: int, session: requests.Session, v log.info(f'Chapter {chapter_info.chapter_number} download complete with a total of {len(img_urls)} pages [green]✓') progress.remove_task(chapter_download_task_id) -def download_webtoon(series_url: str, start_chapter: int, end_chapter: int, dest: str, images_format: str='jpg', download_latest_chapter=False, separate_chapters=False): +def download_webtoon(series_url: str, start_chapter: int, end_chapter: int, dest: str, images_format: str='jpg', download_latest_chapter=False, separate_chapters=False, use_chapter_name=False): """ downloads all chapters starting from start_chapter until end_chapter, inclusive. stores the downloaded chapter into the dest path. @@ -357,6 +357,10 @@ def download_webtoon(series_url: str, start_chapter: int, end_chapter: int, dest separate_chapters: bool separate downloaded chapters in their own folder under the dest path if true, else stores all images in the dest folder. + + use_chapter_name: bool + when downloading chapters in their own folders, use the chapter name instead + of the chapter number """ session = requests.session() session.cookies.set("needGDPR", "FALSE", domain=".webtoons.com") @@ -398,7 +402,7 @@ def download_webtoon(series_url: str, start_chapter: int, end_chapter: int, dest with ThreadPoolExecutor(max_workers=4) as pool: chapter_download_futures = set() for chapter_info in itertools.islice(chapters_to_download, n_concurrent_chapters_download): - chapter_dest = os.path.join(dest, f"{chapter_info.chapter_number:0{zeros}d}") if separate_chapters else dest + chapter_dest = os.path.join(dest, f"{chapter_info.title}" if use_chapter_name else f"{chapter_info.chapter_number:0{zeros}d}") if separate_chapters else dest chapter_download_task = progress.add_task(f"[plum2]Chapter {chapter_info.chapter_number}.", type='Pages', type_color='grey85', number_format='>02d', start=False, rendered_total='??') chapter_download_futures.add( pool.submit(download_chapter, chapter_download_task, session, viewer_url, chapter_info, chapter_dest, zeros, images_format) @@ -417,7 +421,7 @@ def download_webtoon(series_url: str, start_chapter: int, end_chapter: int, dest # Scheduling the next set of futures. for chapter_info in itertools.islice(chapters_to_download, len(done)): - chapter_dest = os.path.join(dest, f"{chapter_info.chapter_number:0{zeros}d}") if separate_chapters else dest + chapter_dest = os.path.join(dest, f"{chapter_info.title}" if use_chapter_name else f"{chapter_info.chapter_number:0{zeros}d}") if separate_chapters else dest chapter_download_task = progress.add_task(f"[plum2]Chapter {chapter_info.chapter_number}.", type='Pages', type_color='grey85', number_format='>02d', start=False, rendered_total='??') chapter_download_futures.add( pool.submit(download_chapter, chapter_download_task, session, viewer_url, chapter_info, chapter_dest, zeros, images_format) @@ -442,7 +446,7 @@ def main(): return series_url = args.url separate = args.seperate or args.separate - download_webtoon(series_url, args.start, args.end, args.dest, args.images_format, args.latest, separate) + download_webtoon(series_url, args.start, args.end, args.dest, args.images_format, args.latest, separate, args.chapter_name) if(__name__ == '__main__'): main()