Skip to content

Commit

Permalink
Revert "update renamer"
Browse files Browse the repository at this point in the history
This reverts commit b7c65a8.
  • Loading branch information
shininome committed Mar 19, 2024
1 parent b7c65a8 commit 1464b9d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 64 deletions.
5 changes: 3 additions & 2 deletions backend/src/module/downloader/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def __init__(self):
pass

@staticmethod
def check_files(files_name: list[str]):
def check_files(info):
media_list = []
subtitle_list = []
for file_name in files_name:
for f in info.files:
file_name = f.name
suffix = Path(file_name).suffix
if suffix.lower() in [".mp4", ".mkv"]:
media_list.append(file_name)
Expand Down
85 changes: 36 additions & 49 deletions backend/src/module/manager/renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

from module.conf import settings
from module.downloader import DownloadClient
from module.downloader.path import TorrentPath
from module.models import EpisodeFile, Notification, SubtitleFile
from module.parser import TitleParser
from module.downloader.path import TorrentPath

logger = logging.getLogger(__name__)


class Renamer:
class Renamer(TorrentPath):
def __init__(self):
self._path_parser = TorrentPath()
super().__init__()
self._parser = TitleParser()
self._check_pool = {}

Expand All @@ -28,24 +28,26 @@ def print_result(torrent_count, rename_count):
def gen_path(
file_info: EpisodeFile | SubtitleFile, bangumi_name: str, method: str
) -> str:
season = f"{file_info.season:02d}"
episode = f"{file_info.episode:02d}"
method_dict = {
"none": file_info.media_path,
"subtitle_none": file_info.media_path,
"pn": f"{file_info.title} S{season}E{episode}{file_info.suffix}",
"advance": f"{bangumi_name} S{season}E{episode}{file_info.suffix}",
"normal": file_info.media_path,
}
# TODO 这两个方法后面还是要分开,接受的参数都不一样,缺少language参数
if "subtitle" in method:
method_dict = {
"subtitle_pn": f"{file_info.title} S{season}E{episode}.{file_info.language}{file_info.suffix}",
"subtitle_advance": f"{bangumi_name} S{season}E{episode}.{file_info.language}{file_info.suffix}",
}
if method == "normal":
season = f"0{file_info.season}" if file_info.season < 10 else file_info.season
episode = (
f"0{file_info.episode}" if file_info.episode < 10 else file_info.episode
)
if method == "none" or method == "subtitle_none":
return file_info.media_path
elif method == "pn":
return f"{file_info.title} S{season}E{episode}{file_info.suffix}"
elif method == "advance":
return f"{bangumi_name} S{season}E{episode}{file_info.suffix}"
elif method == "normal":
logger.warning("[Renamer] Normal rename method is deprecated.")
return method_dict.get(method, method_dict.get("none"))
return file_info.media_path
elif method == "subtitle_pn":
return f"{file_info.title} S{season}E{episode}.{file_info.language}{file_info.suffix}"
elif method == "subtitle_advance":
return f"{bangumi_name} S{season}E{episode}.{file_info.language}{file_info.suffix}"
else:
logger.error(f"[Renamer] Unknown rename method: {method}")
return file_info.media_path

async def rename_file(
self,
Expand Down Expand Up @@ -147,20 +149,14 @@ async def rename(self, client: DownloadClient) -> list[Notification]:
torrents_info = await client.get_torrent_info()
renamed_info: list[Notification] = []
for info in torrents_info:
files_info = await client.get_torrent_files(info["hash"])
files_name = [
file["name"] for file in files_info
] # TODO: 兼容性过差,需要download提供接口后重写该部分
media_list, subtitle_list = self._path_parser.check_files(files_name)
bangumi_name, season = self._path_parser._path_to_bangumi(
info.get("save_path")
)
media_list, subtitle_list = await client.check_files(info)
bangumi_name, season = await client._path_to_bangumi(info.save_path)
kwargs = {
"torrent_name": info["name"],
"torrent_name": info.name,
"bangumi_name": bangumi_name,
"method": rename_method,
"season": season,
"_hash": info["hash"],
"_hash": info.hash,
"client": client,
}
# Rename single media file
Expand All @@ -183,20 +179,18 @@ async def rename(self, client: DownloadClient) -> list[Notification]:
logger.debug("[Renamer] Rename process finished.")
return renamed_info

async def compare_ep_version(
self, torrent_name: str, torrent_hash: str, client: DownloadClient
):
async def compare_ep_version(self, torrent_name: str, torrent_hash: str, client: DownloadClient):
if re.search(r"v\d.", torrent_name):
pass
else:
await client.delete_torrent(hashes=torrent_hash)

@staticmethod
async def _rename_file_internal(
original_path: str,
new_path: str,
_hash: str,
client: DownloadClient,
original_path: str,
new_path: str,
_hash: str,
client: DownloadClient,
) -> bool:
if original_path != new_path:
renamed = await client.rename_torrent_file(
Expand All @@ -211,16 +205,9 @@ async def _rename_file_internal(


if __name__ == "__main__":
import asyncio

async def test():
from module.conf import setup_logger
from module.downloader import DownloadClient

settings.log.debug_enable = True
setup_logger()
async with DownloadClient() as qb:
renamer = Renamer()
await renamer.rename(qb)
from module.conf import setup_logger

asyncio.run(test())
settings.log.debug_enable = True
setup_logger()
with Renamer() as renamer:
renamer.rename()
26 changes: 13 additions & 13 deletions backend/src/module/parser/title_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import logging
from abc import abstractmethod

from module.conf import settings
from module.models import Bangumi
from module.models.bangumi import Episode
from module.parser import analyser

from module.parser.analyser import (
OpenAIParser,
mikan_parser,
raw_parser,
tmdb_parser,
torrent_parser,
)

logger = logging.getLogger(__name__)

Expand All @@ -22,15 +26,13 @@ def torrent_parser(
file_type: str = "media",
):
try:
return analyser.torrent_parser(
torrent_path, torrent_name, season, file_type
)
return torrent_parser(torrent_path, torrent_name, season, file_type)
except Exception as e:
logger.warning(f"Cannot parse {torrent_path} with error {e}")

@staticmethod
def tmdb_parser(title: str, season: int, language: str):
tmdb_info = analyser.tmdb_parser(title, language)
tmdb_info = tmdb_parser(title, language)
if tmdb_info:
logger.debug(f"TMDB Matched, official title is {tmdb_info.title}")
tmdb_season = tmdb_info.last_season if tmdb_info.last_season else season
Expand All @@ -42,9 +44,7 @@ def tmdb_parser(title: str, season: int, language: str):

@staticmethod
def tmdb_poster_parser(bangumi: Bangumi):
tmdb_info = analyser.tmdb_parser(
bangumi.official_title, settings.rss_parser.language
)
tmdb_info = tmdb_parser(bangumi.official_title, settings.rss_parser.language)
if tmdb_info:
logger.debug(f"TMDB Matched, official title is {tmdb_info.title}")
bangumi.poster_link = tmdb_info.poster_link
Expand All @@ -61,11 +61,11 @@ def raw_parser(raw: str) -> Bangumi | None:
# use OpenAI ChatGPT to parse raw title and get structured data
if settings.experimental_openai.enable:
kwargs = settings.experimental_openai.dict(exclude={"enable"})
gpt = analyser.OpenAIParser(**kwargs)
gpt = OpenAIParser(**kwargs)
episode_dict = gpt.parse(raw, asdict=True)
episode = Episode(**episode_dict)
else:
episode = analyser.raw_parser(raw)
episode = raw_parser(raw)

titles = {
"zh": episode.title_zh,
Expand Down Expand Up @@ -105,4 +105,4 @@ def raw_parser(raw: str) -> Bangumi | None:

@staticmethod
def mikan_parser(homepage: str) -> tuple[str, str]:
return analyser.mikan_parser(homepage)
return mikan_parser(homepage)

0 comments on commit 1464b9d

Please sign in to comment.