import argparse import os import shutil from pathlib import Path from komconfig import KomConfig from src.data.komga import scan_komga from src.logic.search import avail_check, search_all, search_requested, search_series from src.logic.utils import detect_chapters, move, rename, tag_folder cfg: KomConfig = KomConfig() def grabber(args): nyaa, komga = avail_check() # os.system(f"rm -rf {cfg.komgrabber.download_location}") # os.mkdir(cfg.komgrabber.download_location) if not os.path.exists(cfg.komgrabber.tag_location): os.mkdir(cfg.komgrabber.tag_location) if not os.path.exists(cfg.komgrabber.tag_location): os.mkdir(cfg.komgrabber.tag_location) if nyaa is True and komga is True: if args.request: search_requested() if args.library is None: libraries = cfg.komga.libraries if not libraries: print("No libraries found in Komga, please check your configuration") return for library in libraries: if not library.id: print( f"Library {library} has no id, please check your configuration" ) continue search_all(library, args.all) else: library = cfg.komga.getLibraryByName(args.library) search_all(library, args.all) if ( cfg.komgrabber.downloader == "aria2" and cfg.komgrabber.downloader_settings.kill_after_completion ): # kill aria2c os.system("killall aria2c") else: print("No connection established, quitting") def grab_series(args): # nyaa, komga = avail_check() # if not args.series: print("No series provided to tag") return series = [series.strip() for series in args.series] library = cfg.komga.getLibraryByName(args.library) if not library: print(f"Library {args.library} not found, please check your configuration") return search_series(library, series) def file_operations(args): path = Path(args.path) # type: ignore if args.rename: rename(path) if args.detect_chapters: detect_chapters(path) if args.tag: tag_folder(path) if args.move: move(path, args.library) if args.scan: scan_komga() # remove all folders and files in path for folder in os.listdir(path): folder_path = os.path.join(path, folder) if os.path.isfile(folder_path): os.remove(folder_path) elif os.path.isdir(folder_path): shutil.rmtree(folder_path) # os.rmdir(path) def main(): parser = argparse.ArgumentParser(description="KomGrabber CLI") subparsers = parser.add_subparsers(dest="command", required=True) # tag subcommand tag_parser = subparsers.add_parser( "search", help="Run search operation. After the search is completed, the library will be scanned to detect new or updated series.", ) tag_parser.add_argument( "library", nargs="?", # makes it optional default=None, # or "" if you prefer an empty string metavar="[library]", # nicer usage display help="Library to search in (e.g. 'manga', 'anime', leave empty for all)", ) tag_parser.add_argument( "-v", "--verbose", action="store_true", help="Enable verbose output" ) tag_parser.add_argument( "-a", "--all", action="store_true", default=False, help="Search for all series in the database", ) tag_parser.add_argument( "-r", "--request", action="store_true", default=False, help="Search for the requested series in the database", ) tag_parser.set_defaults(func=grabber) # tag-series subcommand file_ops = subparsers.add_parser("file", help="Run file related operations") file_ops.add_argument( "-p", "--path", type=str, default=cfg.komgrabber.tag_location, help="Path to use for actions (overwrites default path).", ) file_ops.add_argument( "-t", "--tag", default=True, action="store_true", help="Tag the downloaded files with the series name.", ) file_ops.add_argument( "-r", "--rename", default=True, action="store_true", help="Rename the downloaded files to match the series name.", ) file_ops.add_argument( "-d", "--detect-chapters", action="store_true", default=True, help="Detect chapters in the downloaded files.", ) file_ops.add_argument( "-m", "--move", action="store_true", default=True, help="Move the downloaded files to the specified path.", ) file_ops.add_argument( "-v", "--verbose", action="store_true", help="Enable verbose output" ) file_ops.add_argument( "--scan", action="store_true", help="Scan the library after downloading", ) file_ops.add_argument( "--library", type=str, help="Specify the library to use for operations", default="Manga", ) file_ops.set_defaults(func=file_operations) series_tagger = subparsers.add_parser( "search-series", help="Search series in the library" ) series_tagger.add_argument( "series", type=str, nargs="+", help="Series to search (e.g. 'One Piece', 'Naruto')", ) series_tagger.add_argument( "--library", type=str, help="Library to use for tagging", default="Manga" ) series_tagger.set_defaults(func=grab_series) args = parser.parse_args() args.func(args) if __name__ == "__main__": main()