rework cli arguments

This commit is contained in:
2025-05-06 20:48:15 +02:00
parent 8e3649afe1
commit 5729096078

167
cli.py
View File

@@ -1,106 +1,119 @@
from src.logic.cli import avail_check, main as cli_main from src.logic.cli import avail_check, search_all
import os import os
import argparse import argparse
from src.logic.utils import move, tag_folder, rename, detect_chapters from src.logic.utils import move, tag_folder, rename, detect_chapters
from komconfig import KomConfig from komconfig import KomConfig
from src.aria import launch_aria2c, kill_aria2c
from src.data.komga import scan_komga
from pathlib import Path
cfg = KomConfig() cfg: KomConfig = KomConfig()
def grabber(): def grabber(args):
nyaa, komga = avail_check() nyaa, komga = avail_check()
print(nyaa, komga) os.system(f"rm -rf {cfg.komgrabber.download_location}")
os.mkdir(cfg.komgrabber.download_location)
if nyaa is True and komga is True: if nyaa is True and komga is True:
cli_main() search_all()
# kill aria2c if args.scan:
os.system("killall aria2c") scan_komga()
if cfg.komgrabber.aria2.kill_after_completion:
# kill aria2c
os.system("killall aria2c")
else: else:
print("No connection established, quitting") print("No connection established, quitting")
def main(run_args=None): def grab_series(series: list[str]):
parser = argparse.ArgumentParser( # nyaa, komga = avail_check()
description="A script to call various functions related to Komga File Management." os.system(f"rm -rf {cfg.komgrabber.download_location}")
) os.mkdir(cfg.komgrabber.download_location)
help_texts = {
"search": "Starts a search for all ongoing series in Komga.",
"move": "Moves the downloaded files from the download path to the Komga library.",
"tag": "Tries to tag all files in the download dir using comictagger.",
"rename": "Renames the files based on the naming scheme [Title] v[number] #[number] to get best results with comictagger.",
"detect_chapters": "Detects the chapters in the downloaded folders and removes them.",
}
search_parser = parser.add_argument_group("Search") def file_operations(args):
search_parser.add_argument( path = cfg.komgrabber.download_location
"-s", if args.path: # type: ignore
"--search", path = Path(args.path) # type: ignore
dest="actions", if args.rename:
action="append_const", rename(path)
const="search", if args.detect_chapters:
help=help_texts["search"], detect_chapters(path)
) if args.tag:
move_parser = parser.add_argument_group("Move") tag_folder(path)
move_parser.add_argument( if args.move:
"-m", move(path)
"--move", if args.scan:
dest="actions", scan_komga()
action="append_const",
const="move",
help=help_texts["move"], def main():
) parser = argparse.ArgumentParser(description="KomGrabber CLI")
tag_parser = parser.add_argument_group("Tag") subparsers = parser.add_subparsers(dest="command", required=True)
# tag subcommand
tag_parser = subparsers.add_parser("search", help="Run search operation")
tag_parser.add_argument( tag_parser.add_argument(
"-t", "-v", "--verbose", action="store_true", help="Enable verbose output"
"--tag",
dest="actions",
action="append_const",
const="tag",
help=help_texts["tag"],
) )
rename_parser = parser.add_argument_group("Rename") tag_parser.add_argument(
rename_parser.add_argument( "--scan",
"-r", action="store_true",
"--rename", help="Scan the library after downloading",
dest="actions",
action="append_const",
const="rename",
help=help_texts["rename"],
)
detect_chapters_parser = parser.add_argument_group("Detect Chapters")
detect_chapters_parser.add_argument(
"-d",
"--detect_chapters",
dest="actions",
action="append_const",
const="detect_chapters",
help=help_texts["detect_chapters"],
) )
parser.add_argument( 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", "-p",
"--path", "--path",
type=str, type=str,
default=cfg.komgrabber.download_location, default=cfg.komgrabber.download_location,
help="Path to use for actions (overwrites default path).", help="Path to use for actions (overwrites default path).",
) )
args = parser.parse_args() 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.",
)
# based on provided arguments, call the corresponding function file_ops.add_argument(
if args.actions is None: "-v", "--verbose", action="store_true", help="Enable verbose output"
parser.print_help() )
return file_ops.add_argument(
for action in args.actions: "--scan",
if action == "search": action="store_true",
grabber() help="Scan the library after downloading",
elif action == "move": )
move(src=args.path, dest=cfg.komga.media_path) file_ops.set_defaults(func=file_operations)
elif action == "tag":
tag_folder(args.path) args = parser.parse_args()
elif action == "rename": args.func(args)
rename(args.path)
elif action == "detect_chapters":
detect_chapters(args.path)
if __name__ == "__main__": if __name__ == "__main__":