Add fetch-all method.
This commit is contained in:
		| @@ -9,10 +9,14 @@ from .scans import Chapter, Manga, ScanFetcher | ||||
| _MangaT = TypeVar("_MangaT", bound=Manga) | ||||
| _ChapterT = TypeVar("_ChapterT", bound=Chapter) | ||||
|  | ||||
| LOGGER = logging.getLogger(__package__) | ||||
|  | ||||
|  | ||||
| def list_mangas(fetcher: ScanFetcher[_MangaT, _ChapterT]): | ||||
|     for manga in fetcher.list_mangas(): | ||||
|         print(manga) | ||||
|     mangas = fetcher.list_mangas() | ||||
|     print("Found {} mangas:".format(len(mangas))) | ||||
|     for manga in sorted(mangas, key=lambda m: m.name): | ||||
|         print(manga.name) | ||||
|  | ||||
|  | ||||
| def list_chapters(fetcher: ScanFetcher[_MangaT, _ChapterT], name: str): | ||||
| @@ -28,7 +32,10 @@ def list_chapters(fetcher: ScanFetcher[_MangaT, _ChapterT], name: str): | ||||
|  | ||||
|  | ||||
| def fetch_chapters( | ||||
|     fetcher: ScanFetcher[_MangaT, _ChapterT], name: str, folder: Path, ignore: set[str] | ||||
|     fetcher: ScanFetcher[_MangaT, _ChapterT], | ||||
|     name: str, | ||||
|     folder: Path, | ||||
|     ignore: set[str] = set(), | ||||
| ): | ||||
|     manga = fetcher.find_manga(name) | ||||
|     assert manga is not None | ||||
| @@ -61,10 +68,25 @@ def fetch_chapters( | ||||
|  | ||||
|  | ||||
| def parse_ignore(value: str) -> set[str]: | ||||
|     """ | ||||
|     Parse a set of ignore numbers. | ||||
|  | ||||
|     The given string is first split around ',' to obtain parts, then each part | ||||
|     is converted as follows: | ||||
|         - if the part is of the form x-y, with x and y two integers, numbers from the | ||||
|           range [x, y] (both end included) are added to the ignore set, | ||||
|         - otherwise, the part itself is added to the ignore set. | ||||
|  | ||||
|     Args: | ||||
|         value: Value to parse. | ||||
|  | ||||
|     Returns: | ||||
|         The set of numbers to ignore. | ||||
|     """ | ||||
|     ignore: set[str] = set() | ||||
|  | ||||
|     for part in value.split(","): | ||||
|         part = part.strip() | ||||
|         part = part.strip().replace(" ", "") | ||||
|         try: | ||||
|             start, end = (int(p) for p in part.split("-")) | ||||
|             for number in range(start, end + 1): | ||||
| @@ -82,11 +104,17 @@ def main() -> None: | ||||
|     ) | ||||
|     sub_parsers = parser.add_subparsers(dest="command") | ||||
|  | ||||
|     fetch_parser = sub_parsers.add_parser("fetch") | ||||
|     fetch_parent_parser = argparse.ArgumentParser(add_help=False) | ||||
|     fetch_parent_parser.add_argument( | ||||
|         "-o", "--output", type=Path, required=False, default=None | ||||
|     ) | ||||
|  | ||||
|     fetch_parser = sub_parsers.add_parser("fetch", parents=[fetch_parent_parser]) | ||||
|     fetch_parser.add_argument("-i", "--ignore", type=str, required=False, default="") | ||||
|     fetch_parser.add_argument("-o", "--output", type=Path, required=False, default=None) | ||||
|     fetch_parser.add_argument("manga", type=str) | ||||
|  | ||||
|     sub_parsers.add_parser("fetch-all", parents=[fetch_parent_parser]) | ||||
|  | ||||
|     sub_parsers.add_parser("list-mangas") | ||||
|  | ||||
|     list_chapters_parser = sub_parsers.add_parser("list-chapters") | ||||
| @@ -102,8 +130,11 @@ def main() -> None: | ||||
|         case "lelscans": | ||||
|             fetcher = LelScansFetcher() | ||||
|  | ||||
|     command: Literal["fetch", "list-mangas", "list-chapters"] = args.command | ||||
|     command: Literal["fetch", "fetch-all", "list-mangas", "list-chapters"] = ( | ||||
|         args.command | ||||
|     ) | ||||
|  | ||||
|     folder: Path | ||||
|     match command: | ||||
|         case "list-mangas": | ||||
|             list_mangas(fetcher) | ||||
| @@ -112,13 +143,18 @@ def main() -> None: | ||||
|             list_chapters(fetcher, manga) | ||||
|         case "fetch": | ||||
|             manga = args.manga | ||||
|             folder: Path | None = args.output | ||||
|             folder = args.output or Path("scans", manga) | ||||
|             ignore: str = args.ignore | ||||
|             if folder is None: | ||||
|                 folder = Path("scans", manga) | ||||
|             folder.mkdir(exist_ok=True) | ||||
|  | ||||
|             fetch_chapters(fetcher, manga, folder, parse_ignore(ignore)) | ||||
|         case "fetch-all": | ||||
|             folder = args.output or Path("scans") | ||||
|             for path in folder.iterdir(): | ||||
|                 print(f"Retrieving scan for {path.name}... ") | ||||
|                 if not path.is_dir(): | ||||
|                     continue | ||||
|                 fetch_chapters(fetcher, path.stem, path) | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|   | ||||
		Reference in New Issue
	
	Block a user