second running version
This commit is contained in:
parent
4dae1d037f
commit
7bcc46ebff
102
main.py
102
main.py
@ -1,17 +1,17 @@
|
|||||||
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import traceback
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from collections import defaultdict
|
||||||
from aniworld.models import Anime, Episode
|
from aniworld.models import Anime, Episode
|
||||||
from aniworld.common import get_season_episode_count, get_movie_episode_count
|
from aniworld.common import get_season_episode_count, get_movie_episode_count
|
||||||
from aniworld.search import search_anime
|
from aniworld.search import search_anime
|
||||||
from Loader import download
|
from Loader import download
|
||||||
|
|
||||||
# Configure logging
|
# Configure basic logging to the console
|
||||||
logging.basicConfig(
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
filename="loader.log",
|
|
||||||
level=logging.DEBUG,
|
|
||||||
format="%(asctime)s - %(levelname)s - %(message)s"
|
|
||||||
)
|
|
||||||
|
|
||||||
class MatchNotFoundError(Exception):
|
class MatchNotFoundError(Exception):
|
||||||
"""Custom exception raised when the pattern match is not found."""
|
"""Custom exception raised when the pattern match is not found."""
|
||||||
@ -20,18 +20,23 @@ class MatchNotFoundError(Exception):
|
|||||||
class Loader:
|
class Loader:
|
||||||
def __init__(self, basePath: str):
|
def __init__(self, basePath: str):
|
||||||
self.directory = basePath
|
self.directory = basePath
|
||||||
logging.info(f"Initialized Loader with base path: {self.directory}")
|
logging.warning(f"Initialized Loader with base path: {self.directory}")
|
||||||
|
|
||||||
def __find_mp4_files(self):
|
def __find_mp4_files(self):
|
||||||
logging.info("Scanning for .mp4 files")
|
logging.warning("Scanning for .mp4 files")
|
||||||
for root, dirs, files in os.walk(self.directory):
|
|
||||||
mp4_files = [file for file in files if file.endswith('.mp4')]
|
for root_folder_name in os.listdir(self.directory):
|
||||||
|
|
||||||
|
folder_data = defaultdict(list) # Dictionary to store MP4 files per folder
|
||||||
|
folder = os.path.join(self.directory, root_folder_name)
|
||||||
|
|
||||||
|
# First pass: Scan all folders and collect MP4 file data
|
||||||
|
for root, dirs, files in os.walk(folder):
|
||||||
|
mp4_files = [file for file in files if file.endswith('.mp4')]
|
||||||
|
if mp4_files:
|
||||||
|
folder_data[root_folder_name].extend(mp4_files)
|
||||||
|
yield root_folder_name, folder_data[root_folder_name]
|
||||||
|
|
||||||
if mp4_files:
|
|
||||||
relative_path = os.path.relpath(root, self.directory)
|
|
||||||
root_folder_name = relative_path.split(os.sep)[0]
|
|
||||||
logging.debug(f"Found {len(mp4_files)} .mp4 files in {root_folder_name}")
|
|
||||||
yield root_folder_name, mp4_files
|
|
||||||
for dir in self.__find_empty_folders():
|
for dir in self.__find_empty_folders():
|
||||||
logging.debug(f"Found no .mp4 files in {dir}")
|
logging.debug(f"Found no .mp4 files in {dir}")
|
||||||
yield dir, []
|
yield dir, []
|
||||||
@ -62,14 +67,17 @@ class Loader:
|
|||||||
logging.debug(f"Key found for folder '{folder_name}': {key}")
|
logging.debug(f"Key found for folder '{folder_name}': {key}")
|
||||||
return key
|
return key
|
||||||
else:
|
else:
|
||||||
key = search_anime(self.__remove_year(folder_name))
|
key = search_anime(self.__remove_year(folder_name), True)
|
||||||
with open(key_file, 'w') as file:
|
if (len(key) >= 1):
|
||||||
file.write(key)
|
key = key[0]['link']
|
||||||
logging.info(f"Generated new key for folder '{folder_name}': {key}")
|
with open(key_file, 'w') as file:
|
||||||
return key
|
file.write(key)
|
||||||
|
logging.warning(f"Generated new key for folder '{folder_name}': {key}")
|
||||||
|
return key
|
||||||
|
raise Exception()
|
||||||
|
|
||||||
def __GetEpisodeAndSeason(self, filename: str):
|
def __GetEpisodeAndSeason(self, filename: str):
|
||||||
pattern = r'S(\d{2})E(\d{2})'
|
pattern = r'S(\d+)E(\d+)'
|
||||||
match = re.search(pattern, filename)
|
match = re.search(pattern, filename)
|
||||||
if match:
|
if match:
|
||||||
season = match.group(1)
|
season = match.group(1)
|
||||||
@ -104,36 +112,42 @@ class Loader:
|
|||||||
if missing_episodes:
|
if missing_episodes:
|
||||||
yield season, missing_episodes
|
yield season, missing_episodes
|
||||||
|
|
||||||
|
|
||||||
def LoadMissing(self):
|
def LoadMissing(self):
|
||||||
logging.info("Starting process to load missing episodes")
|
logging.warning("Starting process to load missing episodes")
|
||||||
result = self.__find_mp4_files()
|
result = self.__find_mp4_files()
|
||||||
|
|
||||||
for folder, mp4_files in result:
|
def download_episode(folder, season, episode, key):
|
||||||
|
"""Helper function to download an individual episode."""
|
||||||
try:
|
try:
|
||||||
key = self.__check_and_generate_key(folder)
|
folder_path = os.path.join(self.directory, folder, f"Season {season}")
|
||||||
missings = self.__GetMissingEpisodesAndSeason(key, mp4_files)
|
anime = Anime(
|
||||||
|
episode_list=[Episode(slug=key, season=season, episode=episode)],
|
||||||
for season, missing_episodes in missings:
|
language="German Dub",
|
||||||
folder_path = os.path.join(self.directory, folder, f"Season {season}")
|
output_directory=folder_path
|
||||||
|
)
|
||||||
for episode in missing_episodes:
|
logging.warning(f"Downloading episode {episode} of season {season} for anime {key}")
|
||||||
anime = Anime(
|
download(anime)
|
||||||
episode_list=[
|
|
||||||
Episode(slug=key, season=season, episode=episode)
|
|
||||||
],
|
|
||||||
language="German Dub",
|
|
||||||
output_directory=folder_path
|
|
||||||
)
|
|
||||||
logging.info(f"Downloading episode {episode} of season {season} for anime {key}")
|
|
||||||
download(anime)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error processing folder '{folder}': {e}")
|
logging.error(f"Error downloading episode {episode} of season {season} for anime {key}: {e}")
|
||||||
continue
|
|
||||||
|
# Using ThreadPoolExecutor to run downloads in parallel
|
||||||
|
with ThreadPoolExecutor(max_workers=5) as executor: # Adjust number of workers as needed
|
||||||
|
for folder, mp4_files in result:
|
||||||
|
try:
|
||||||
|
key = self.__check_and_generate_key(folder)
|
||||||
|
missings = self.__GetMissingEpisodesAndSeason(key, mp4_files)
|
||||||
|
|
||||||
|
for season, missing_episodes in missings:
|
||||||
|
for episode in missing_episodes:
|
||||||
|
executor.submit(download_episode, folder, season, episode, key)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Error processing folder '{folder}': {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
continue
|
||||||
|
|
||||||
# Read the base directory from an environment variable
|
# Read the base directory from an environment variable
|
||||||
#directory_to_search = os.getenv("ANIME_DIRECTORY", "\\\\sshfs.r\\ubuntu@192.168.178.43\\media\\serien\\Serien")
|
directory_to_search = os.getenv("ANIME_DIRECTORY", "\\\\sshfs.r\\ubuntu@192.168.178.43\\media\\serien\\Serien")
|
||||||
directory_to_search = os.getenv("ANIME_DIRECTORY", "D:\sss")
|
#directory_to_search = os.getenv("ANIME_DIRECTORY", "D:\sss")
|
||||||
|
|
||||||
loader = Loader(directory_to_search)
|
loader = Loader(directory_to_search)
|
||||||
loader.LoadMissing()
|
loader.LoadMissing()
|
||||||
Loading…
x
Reference in New Issue
Block a user