Skip to content

Commit

Permalink
refactor: rename module little refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
EstrellaXD committed Jan 4, 2024
1 parent 258c922 commit cf9a3ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions backend/src/module/core/aiocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from module.models import Bangumi, RSSItem, Torrent



rss_item_pool = []
torrent_pool: list[tuple[Bangumi, list[Torrent]]] = []

Expand Down
78 changes: 50 additions & 28 deletions backend/src/module/manager/renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from module.downloader import DownloadClient
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):
super().__init__()
self._parser = TitleParser()
self._check_pool = {}

Expand Down Expand Up @@ -65,16 +67,18 @@ async def rename_file(
)
if ep:
new_path = self.gen_path(ep, bangumi_name, method=method)
if media_path != new_path:
if new_path not in self.check_pool.keys():
if await client.rename_torrent_file(
_hash=_hash, old_path=media_path, new_path=new_path
):
return Notification(
official_title=bangumi_name,
season=ep.season,
episode=ep.episode,
)
success = await self._rename_file_internal(
original_path=media_path,
new_path=new_path,
_hash=_hash,
client=client,
)
if success:
return Notification(
official_title=bangumi_name,
season=ep.season,
episode=ep.episode,
)
else:
logger.warning(f"[Renamer] {media_path} parse failed")
if settings.bangumi_manage.remove_bad_torrent:
Expand All @@ -92,23 +96,21 @@ async def rename_collection(
**kwargs,
):
for media_path in media_list:
if client.is_ep(media_path):
if self.is_ep(media_path):
ep = self._parser.torrent_parser(
torrent_path=media_path,
season=season,
)
if ep:
new_path = self.gen_path(ep, bangumi_name, method=method)
if media_path != new_path:
renamed = await client.rename_torrent_file(
_hash=_hash, old_path=media_path, new_path=new_path
)
if not renamed:
logger.warning(f"[Renamer] {media_path} rename failed")
# Delete bad torrent.
if settings.bangumi_manage.remove_bad_torrent:
await client.delete_torrent(_hash)
break
success = await self._rename_file_internal(
original_path=media_path,
new_path=new_path,
_hash=_hash,
client=client,
)
if not success:
break

async def rename_subtitles(
self,
Expand All @@ -131,12 +133,14 @@ async def rename_subtitles(
)
if sub:
new_path = self.gen_path(sub, bangumi_name, method=method)
if subtitle_path != new_path:
renamed = await client.rename_torrent_file(
_hash=_hash, old_path=subtitle_path, new_path=new_path
)
if not renamed:
logger.warning(f"[Renamer] {subtitle_path} rename failed")
success = await self._rename_file_internal(
original_path=subtitle_path,
new_path=new_path,
_hash=_hash,
client=client,
)
if not success:
break

async def rename(self, client: DownloadClient) -> list[Notification]:
# Get torrent info
Expand Down Expand Up @@ -181,6 +185,24 @@ async def compare_ep_version(self, torrent_name: str, torrent_hash: str, client:
else:
await client.delete_torrent(hashes=torrent_hash)

@staticmethod
async def _rename_file_internal(
original_path: str,
new_path: str,
_hash: str,
client: DownloadClient,
) -> bool:
if original_path != new_path:
renamed = await client.rename_torrent_file(
_hash=_hash, old_path=original_path, new_path=new_path
)
if not renamed:
logger.warning(f"[Renamer] {original_path} rename failed")
if settings.bangumi_manage.remove_bad_torrent:
await client.delete_torrent(_hash)
return False
return True


if __name__ == "__main__":
from module.conf import setup_logger
Expand Down

0 comments on commit cf9a3ab

Please sign in to comment.