rework cli arguments
This commit is contained in:
163
cli.py
163
cli.py
@@ -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 argparse
|
||||
from src.logic.utils import move, tag_folder, rename, detect_chapters
|
||||
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()
|
||||
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:
|
||||
cli_main()
|
||||
search_all()
|
||||
if args.scan:
|
||||
scan_komga()
|
||||
if cfg.komgrabber.aria2.kill_after_completion:
|
||||
# kill aria2c
|
||||
os.system("killall aria2c")
|
||||
else:
|
||||
print("No connection established, quitting")
|
||||
|
||||
|
||||
def main(run_args=None):
|
||||
parser = argparse.ArgumentParser(
|
||||
description="A script to call various functions related to Komga File Management."
|
||||
)
|
||||
def grab_series(series: list[str]):
|
||||
# nyaa, komga = avail_check()
|
||||
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")
|
||||
search_parser.add_argument(
|
||||
"-s",
|
||||
"--search",
|
||||
dest="actions",
|
||||
action="append_const",
|
||||
const="search",
|
||||
help=help_texts["search"],
|
||||
)
|
||||
move_parser = parser.add_argument_group("Move")
|
||||
move_parser.add_argument(
|
||||
"-m",
|
||||
"--move",
|
||||
dest="actions",
|
||||
action="append_const",
|
||||
const="move",
|
||||
help=help_texts["move"],
|
||||
)
|
||||
tag_parser = parser.add_argument_group("Tag")
|
||||
def file_operations(args):
|
||||
path = cfg.komgrabber.download_location
|
||||
if args.path: # type: ignore
|
||||
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)
|
||||
if args.scan:
|
||||
scan_komga()
|
||||
|
||||
|
||||
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")
|
||||
tag_parser.add_argument(
|
||||
"-t",
|
||||
"--tag",
|
||||
dest="actions",
|
||||
action="append_const",
|
||||
const="tag",
|
||||
help=help_texts["tag"],
|
||||
"-v", "--verbose", action="store_true", help="Enable verbose output"
|
||||
)
|
||||
rename_parser = parser.add_argument_group("Rename")
|
||||
rename_parser.add_argument(
|
||||
"-r",
|
||||
"--rename",
|
||||
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"],
|
||||
tag_parser.add_argument(
|
||||
"--scan",
|
||||
action="store_true",
|
||||
help="Scan the library after downloading",
|
||||
)
|
||||
|
||||
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",
|
||||
"--path",
|
||||
type=str,
|
||||
default=cfg.komgrabber.download_location,
|
||||
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
|
||||
if args.actions is None:
|
||||
parser.print_help()
|
||||
return
|
||||
for action in args.actions:
|
||||
if action == "search":
|
||||
grabber()
|
||||
elif action == "move":
|
||||
move(src=args.path, dest=cfg.komga.media_path)
|
||||
elif action == "tag":
|
||||
tag_folder(args.path)
|
||||
elif action == "rename":
|
||||
rename(args.path)
|
||||
elif action == "detect_chapters":
|
||||
detect_chapters(args.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.set_defaults(func=file_operations)
|
||||
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user