chore: move various small functions into utils module
This commit is contained in:
5
cli.py
5
cli.py
@@ -1,10 +1,7 @@
|
||||
from src.logic.cli import avail_check, main as cli_main
|
||||
import os
|
||||
import argparse
|
||||
from src.logic.rename import rename
|
||||
from src.logic.tag import tag_folder
|
||||
from src.logic.move import move
|
||||
from src.logic.detect_chapters import detect_chapters
|
||||
from src.logic.utils import move, tag_folder, rename, detect_chapters
|
||||
from komconfig import KomConfig
|
||||
|
||||
cfg = KomConfig()
|
||||
|
||||
152
src/logic/utils.py
Normal file
152
src/logic/utils.py
Normal file
@@ -0,0 +1,152 @@
|
||||
import os
|
||||
import re
|
||||
from komconfig import KomConfig
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
config = KomConfig()
|
||||
|
||||
|
||||
def rename(folder: str = "/home/alexander/Downloads/torrents/manga/") -> None:
|
||||
"""Rename the files in a folder according to the template.
|
||||
Template: [Name] v[nr] #[nr].ext (e.g. "The Flash v1 #1.cbz").
|
||||
|
||||
Args:
|
||||
----
|
||||
- folder (str): the string to the folder
|
||||
"""
|
||||
# Get the files in the folder
|
||||
if "~" in folder:
|
||||
folder = os.path.expanduser(folder)
|
||||
files = os.listdir(folder)
|
||||
print(files)
|
||||
for file in files:
|
||||
if os.path.isdir(f"{folder}/{file}"):
|
||||
rename(f"{folder}/{file}")
|
||||
if not file.endswith(".cbz"):
|
||||
print(f"Skipping {file}, not a cbz file")
|
||||
continue
|
||||
ext = file.split(".")[-1]
|
||||
|
||||
match = re.search(r"v\d{2,4}", file)
|
||||
if match:
|
||||
split_start = match.start()
|
||||
split_end = match.end()
|
||||
# Split the filename between split_start and split_end
|
||||
volume = file[split_start:split_end]
|
||||
# Split the filename at the split index, but keep the "v" and digits in the title
|
||||
title = file[:split_start].strip()
|
||||
# add the volume number to the title as a suffix #nr
|
||||
title = f"{title} {volume} #{volume.replace('v', '')}"
|
||||
print(title)
|
||||
# rename the file
|
||||
os.rename(f"{folder}/{file}", f"{folder}/{title}.{ext}")
|
||||
|
||||
|
||||
# rename the folder
|
||||
def rename_recursive(folder: str) -> None:
|
||||
# get all directories in the folder and apply the rename function to them
|
||||
for root, dirs, _files in os.walk(folder):
|
||||
for dir in dirs:
|
||||
rename(f"{root}/{dir}")
|
||||
|
||||
|
||||
def tag_folder(folder: Path = Path(config.komgrabber.download_location)) -> None:
|
||||
"""
|
||||
Recursively tags all the .cbz files in the folder using ComicTagger
|
||||
|
||||
Parameters
|
||||
----------
|
||||
folder : Path, optional
|
||||
The path that will be used to tag, by default Path(config.komgrabber.download_location)
|
||||
"""
|
||||
# Get the files in the folder
|
||||
if "~" in str(folder):
|
||||
folder = os.path.expanduser(folder)
|
||||
files = os.listdir(folder)
|
||||
for file in files:
|
||||
if os.path.isdir(f"{folder}/{file}"):
|
||||
tag_folder(f"{folder}/{file}")
|
||||
if not file.endswith(".cbz"):
|
||||
continue
|
||||
print("Trying to tag file", file)
|
||||
subprocess.call(
|
||||
f'comictagger -s -t cr -f -o "{folder}/{file}" --nosummary --overwrite {"-i" if config.komgrabber.tag_interactive else ""}',
|
||||
shell=True,
|
||||
)
|
||||
|
||||
|
||||
def move(src, dest: Path = Path(config.komga.media_path)) -> None:
|
||||
"""
|
||||
Moves the files from the source folder to the destination folder.
|
||||
If the folder already exists in the destination, only move the new files.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
src : str
|
||||
The source folder
|
||||
dest : Path, optional
|
||||
The destination folder used by Komga, by default Path(config.komga.media_path)
|
||||
"""
|
||||
# Get the files in the folder
|
||||
# +move the folders from src to disc, if folder already exists, only move new files
|
||||
if "~" in src:
|
||||
src = os.path.expanduser(src)
|
||||
folders = os.listdir(src)
|
||||
for folder in folders:
|
||||
if not os.path.exists(f"{dest}/{folder}"):
|
||||
print(f"Moving {folder} to {dest}")
|
||||
shutil.move(f"{src}/{folder}", dest)
|
||||
else:
|
||||
files = os.listdir(f"{src}/{folder}")
|
||||
for file in files:
|
||||
if not os.path.exists(f"{dest}/{folder}/{file}"):
|
||||
print(f"Moving {file} to {dest}/{folder}")
|
||||
shutil.move(f"{src}/{folder}/{file}", f"{dest}/{folder}")
|
||||
# Remove empty folders
|
||||
remove_empty_folders(src)
|
||||
|
||||
|
||||
def remove_empty_folders(src):
|
||||
"""
|
||||
Recursively removes empty folders in the source folder
|
||||
|
||||
Parameters
|
||||
----------
|
||||
src : Path, str
|
||||
The source folder
|
||||
"""
|
||||
folders = os.listdir(src)
|
||||
for folder in folders:
|
||||
if os.path.isfile(f"{src}/{folder}"):
|
||||
continue
|
||||
if not os.listdir(f"{src}/{folder}"):
|
||||
print(f"Removing {folder}")
|
||||
os.rmdir(f"{src}/{folder}")
|
||||
else:
|
||||
remove_empty_folders(f"{src}/{folder}")
|
||||
|
||||
|
||||
def detect_chapters(src: Path = Path(config.komgrabber.download_location)) -> None:
|
||||
"""
|
||||
Detects and deletes any non-volume file in the source folder
|
||||
|
||||
Parameters
|
||||
----------
|
||||
src : Path, optional
|
||||
The Path to be checked, by default Path(config.komgrabber.download_location)
|
||||
"""
|
||||
if "~" in src:
|
||||
src = os.path.expanduser(src)
|
||||
for folder in os.listdir(src):
|
||||
if os.path.isdir(f"{src}/{folder}"):
|
||||
files = os.listdir(f"{src}/{folder}")
|
||||
for file in files:
|
||||
# check for regex "v(d) #(d)" in the file name
|
||||
regex = re.compile(r"^.* v(\d+) #(\d+(?:-\d+)?)\.cbz$")
|
||||
if regex.search(file):
|
||||
print(f"File {file} is a Volume")
|
||||
else:
|
||||
print(f"Deleting chapter {file}")
|
||||
os.remove(f"{src}/{folder}/{file}")
|
||||
Reference in New Issue
Block a user