chore: filechanges

This commit is contained in:
2025-03-03 22:25:39 +01:00
parent f7317f5ca7
commit caae57a70b
5 changed files with 51 additions and 187 deletions

View File

@@ -1,16 +0,0 @@
import os
import re
def detect_chapters(src: str = "/home/alexander/Downloads/torrents/manga/"):
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}")

View File

@@ -2,20 +2,22 @@ import sys
import os import os
import time import time
import bencodepy import bencodepy
from .rename import rename from .utils import rename
from aria2p import Client from aria2p import Client
from aria2p import API from aria2p import API
class Download: class Download:
""" Download a file from a url and start the download using aria2""" """Download a file from a url and start the download using aria2"""
def __init__(self, download_location) -> None: def __init__(self, download_location) -> None:
self.download_location=download_location self.download_location = download_location
self.filename=None self.filename = None
self.torrent_file=None self.torrent_file = None
self.progress=0 self.progress = 0
self.canceled=False self.canceled = False
self.aria2_running=self.check_aria2() self.aria2_running = self.check_aria2()
self.api = API( self.api = API(
client=Client( client=Client(
host="http://localhost", host="http://localhost",
@@ -28,72 +30,70 @@ class Download:
if not self.aria2_running: if not self.aria2_running:
print("Aria2 is not running") print("Aria2 is not running")
sys.exit() sys.exit()
def check_aria2(self): def check_aria2(self):
#check if aria2 is running # check if aria2 is running
if os.system("ps -A | grep aria2c") == 0: if os.system("ps -A | grep aria2c") == 0:
return True return True
else: else:
return False return False
def check_progress(self): def check_progress(self):
try: try:
current_progress=self.api.get_downloads()[0].progress current_progress = self.api.get_downloads()[0].progress
except: except:
return self.progress+0.01 return self.progress + 0.01
if current_progress > self.progress: if current_progress > self.progress:
self.progress=current_progress self.progress = current_progress
return current_progress return current_progress
def get_file(self, url, series_name=None): def get_file(self, url, series_name=None):
#get the file name from the url # get the file name from the url
#use wget to download the file to the download location # use wget to download the file to the download location
name=url.split('/')[-1] name = url.split("/")[-1]
dl_url=f'{self.download_location}{name}' dl_url = f"{self.download_location}{name}"
while self.get_filename(dl_url) is None: while self.get_filename(dl_url) is None:
if not os.path.exists(dl_url): if not os.path.exists(dl_url):
os.system(f'wget -P {self.download_location} {url}') os.system(f"wget -P {self.download_location} {url}")
filename = self.get_filename(dl_url) filename = self.get_filename(dl_url)
self.torrent_file=url.split('/')[-1] self.torrent_file = url.split("/")[-1]
self.filename=filename self.filename = filename
return filename return filename
def remove_torrents(self): def remove_torrents(self):
tr_files=[file for file in os.listdir(self.download_location) if ".torrent" in file] tr_files = [
file for file in os.listdir(self.download_location) if ".torrent" in file
]
for file in tr_files: for file in tr_files:
os.remove(f'{self.download_location}{file}') os.remove(f"{self.download_location}{file}")
def add_torrent(self, torr_name): def add_torrent(self, torr_name):
try: try:
self.api.add_torrent(f'{self.download_location}{torr_name}') self.api.add_torrent(f"{self.download_location}{torr_name}")
print("Torrent added") print("Torrent added")
except Exception as e: except Exception as e:
print(f"Error adding torrent: {e}") print(f"Error adding torrent: {e}")
return False return False
def rename_download(self): def rename_download(self):
filename=self.filename.replace(".aria2", "") filename = self.filename.replace(".aria2", "")
foldername=filename.replace(".cbz", "") if ".cbz" in filename else filename foldername = filename.replace(".cbz", "") if ".cbz" in filename else filename
print(f'Filename: {filename}') print(f"Filename: {filename}")
print(f'Foldername: {foldername}') print(f"Foldername: {foldername}")
if not os.path.exists(f'{self.download_location}{foldername}'): if not os.path.exists(f"{self.download_location}{foldername}"):
os.mkdir(f'{self.download_location}{foldername}') os.mkdir(f"{self.download_location}{foldername}")
os.rename(f'{self.download_location}{filename}', f'{self.download_location}{foldername}/{filename}') os.rename(
#rename the file f"{self.download_location}{filename}",
rename(f'{self.download_location}{foldername}') f"{self.download_location}{foldername}/{filename}",
)
# rename the file
rename(f"{self.download_location}{foldername}")
def get_filename(self, torrent_file): def get_filename(self, torrent_file):
try: try:
with open(torrent_file, 'rb') as f: with open(torrent_file, "rb") as f:
torrent = bencodepy.decode(f.read()) torrent = bencodepy.decode(f.read())
#self.filename=torrent[b'info'][b'name'].decode('utf-8') # self.filename=torrent[b'info'][b'name'].decode('utf-8')
return torrent[b'info'][b'name'].decode('utf-8') return torrent[b"info"][b"name"].decode("utf-8")
except FileNotFoundError: except FileNotFoundError:
return None return None

View File

@@ -1,42 +0,0 @@
import os
import shutil
def move(src, dest: str = "/mnt/Media/Manga"):
"""Move the files in a folder to another folder.
Args:
----
- dest (str): the string to the destination folder
"""
# Get the files in the folder
# +move the folders from src to disc, if folder already exists, only move new files
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):
"""Remove empty folders from a directory.
Args:
----
- src (str): the string to 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}")

View File

@@ -1,51 +0,0 @@
# Rename the downloaded files according to the template
# Template: [Name] v[nr] #[nr].ext (e.g. "The Flash v1 #1.cbz")
import os
import re
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
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}")
if __name__ == "__main__":
rename_recursive("/home/alexander/Downloads/torrents/manga/")

View File

@@ -1,27 +0,0 @@
import os
from pathlib import Path
import subprocess
def tag_folder(
folder: Path = Path("/home/alexander/Downloads/torrents/manga/"),
) -> None:
"""Tag the files in a folder according to the template.
Template: [Name] v[nr] #[nr].ext (e.g. "The Flash v1 #1.cbz").
Args:
----
- folder (Path): the string to the folder
"""
# Get the files in the 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',
shell=True,
)