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,98 +2,98 @@ import sys
import os
import time
import bencodepy
from .rename import rename
from .utils import rename
from aria2p import Client
from aria2p import API
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:
self.download_location=download_location
self.filename=None
self.torrent_file=None
self.progress=0
self.canceled=False
self.aria2_running=self.check_aria2()
self.download_location = download_location
self.filename = None
self.torrent_file = None
self.progress = 0
self.canceled = False
self.aria2_running = self.check_aria2()
self.api = API(
client=Client(
host="http://localhost",
port=6800,
secret="",
timeout=60,
)
)
)
self.api.set_global_options({"dir": self.download_location})
if not self.aria2_running:
print("Aria2 is not running")
sys.exit()
def check_aria2(self):
#check if aria2 is running
# check if aria2 is running
if os.system("ps -A | grep aria2c") == 0:
return True
else:
return False
return False
def check_progress(self):
try:
current_progress=self.api.get_downloads()[0].progress
current_progress = self.api.get_downloads()[0].progress
except:
return self.progress+0.01
return self.progress + 0.01
if current_progress > self.progress:
self.progress=current_progress
self.progress = current_progress
return current_progress
def get_file(self, url, series_name=None):
#get the file name from the url
#use wget to download the file to the download location
name=url.split('/')[-1]
dl_url=f'{self.download_location}{name}'
# get the file name from the url
# use wget to download the file to the download location
name = url.split("/")[-1]
dl_url = f"{self.download_location}{name}"
while self.get_filename(dl_url) is None:
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)
self.torrent_file=url.split('/')[-1]
self.filename=filename
return filename
self.torrent_file = url.split("/")[-1]
self.filename = filename
return filename
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:
os.remove(f'{self.download_location}{file}')
os.remove(f"{self.download_location}{file}")
def add_torrent(self, torr_name):
try:
self.api.add_torrent(f'{self.download_location}{torr_name}')
self.api.add_torrent(f"{self.download_location}{torr_name}")
print("Torrent added")
except Exception as e:
print(f"Error adding torrent: {e}")
return False
def rename_download(self):
filename=self.filename.replace(".aria2", "")
foldername=filename.replace(".cbz", "") if ".cbz" in filename else filename
print(f'Filename: {filename}')
print(f'Foldername: {foldername}')
if not os.path.exists(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}')
#rename the file
rename(f'{self.download_location}{foldername}')
filename = self.filename.replace(".aria2", "")
foldername = filename.replace(".cbz", "") if ".cbz" in filename else filename
print(f"Filename: {filename}")
print(f"Foldername: {foldername}")
if not os.path.exists(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}",
)
# rename the file
rename(f"{self.download_location}{foldername}")
def get_filename(self, torrent_file):
try:
with open(torrent_file, 'rb') as f:
with open(torrent_file, "rb") as f:
torrent = bencodepy.decode(f.read())
#self.filename=torrent[b'info'][b'name'].decode('utf-8')
return 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")
except FileNotFoundError:
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,
)