mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-30 13:31:05 +08:00
Add subtitle rename
This commit is contained in:
@@ -14,9 +14,9 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class Renamer:
|
||||
def __init__(self, download_client: DownloadClient, settings: Config):
|
||||
self.client = download_client
|
||||
self._client = download_client
|
||||
self._renamer = TitleParser()
|
||||
self.notification = PostNotification()
|
||||
self._notification = PostNotification()
|
||||
self.settings = settings
|
||||
|
||||
@staticmethod
|
||||
@@ -26,49 +26,46 @@ class Renamer:
|
||||
logger.debug(f"Checked {torrent_count} files")
|
||||
|
||||
def get_torrent_info(self, category="Bangumi"):
|
||||
recent_info = self.client.get_torrent_info(category=category)
|
||||
recent_info = self._client.get_torrent_info(category=category)
|
||||
torrent_count = len(recent_info)
|
||||
return recent_info, torrent_count
|
||||
|
||||
@staticmethod
|
||||
def check_files(info, suffix_type: str = "media"):
|
||||
if suffix_type == "subtitle":
|
||||
suffix_list = [".ass", ".srt"]
|
||||
else:
|
||||
suffix_list = [".mp4", ".mkv"]
|
||||
file_list = []
|
||||
def check_files(info):
|
||||
media_list = []
|
||||
subtitle_list = []
|
||||
for f in info.files:
|
||||
file_name = f.name
|
||||
suffix = os.path.splitext(file_name)[-1]
|
||||
if suffix.lower() in suffix_list:
|
||||
file_list.append(file_name)
|
||||
return file_list
|
||||
if suffix.lower() in [".mp4", ".mkv"]:
|
||||
media_list.append(file_name)
|
||||
elif suffix.lower() in [".ass", ".srt"]:
|
||||
subtitle_list.append(file_name)
|
||||
return media_list, subtitle_list
|
||||
|
||||
def rename_file(self, info, media_path: str, settings: Config):
|
||||
def rename_file(self, info, media_path: str, rename_method: str, bangumi_name: str, season: int, remove_bad_torrents: bool):
|
||||
torrent_name = info.name
|
||||
suffix = os.path.splitext(media_path)[-1]
|
||||
compare_name = media_path.split(os.path.sep)[-1]
|
||||
bangumi_name, season = self.get_season_info(info.save_path, settings)
|
||||
new_path = self._renamer.torrent_parser(
|
||||
torrent_name=torrent_name,
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
suffix=suffix,
|
||||
method=settings.bangumi_manage.rename_method
|
||||
method=rename_method
|
||||
)
|
||||
if compare_name != new_path:
|
||||
try:
|
||||
self.client.rename_torrent_file(_hash=info.hash, old_path=media_path, new_path=new_path)
|
||||
self.notification.send_msg(bangumi_name, "update")
|
||||
self._client.rename_torrent_file(_hash=info.hash, old_path=media_path, new_path=new_path)
|
||||
self._notification.send_msg(bangumi_name, "最新剧集已经更新,已自动重命名。")
|
||||
except Exception as e:
|
||||
logger.warning(f"{torrent_name} rename failed")
|
||||
logger.warning(f"Season name: {bangumi_name}, Season: {season}, Suffix: {suffix}")
|
||||
logger.debug(e)
|
||||
# Delete bad torrent
|
||||
self.delete_bad_torrent(info, settings)
|
||||
self.delete_bad_torrent(info, remove_bad_torrents)
|
||||
|
||||
def rename_collection(self, info, media_list: list[str], settings: Config):
|
||||
bangumi_name, season = self.get_season_info(info.save_path, settings)
|
||||
def rename_collection(self, info, media_list: list[str],bangumi_name: str, season: int, remove_bad_torrents: bool):
|
||||
_hash = info.hash
|
||||
for media_path in media_list:
|
||||
path_len = len(media_path.split(os.path.sep))
|
||||
@@ -80,41 +77,53 @@ class Renamer:
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
suffix=suffix,
|
||||
method=settings.bangumi_manage.rename_method
|
||||
method="pn"
|
||||
)
|
||||
if torrent_name != new_name:
|
||||
try:
|
||||
self.client.rename_torrent_file(_hash=_hash, old_path=media_path, new_path=new_name)
|
||||
self._client.rename_torrent_file(_hash=_hash, old_path=media_path, new_path=new_name)
|
||||
except Exception as e:
|
||||
logger.warning(f"{torrent_name} rename failed")
|
||||
logger.warning(f"Bangumi name: {bangumi_name}, Season: {season}, Suffix: {suffix}")
|
||||
logger.debug(e)
|
||||
# Delete bad torrent.
|
||||
self.delete_bad_torrent(info, settings)
|
||||
self.client.set_category(category="BangumiCollection", hashes=_hash)
|
||||
self.delete_bad_torrent(info, remove_bad_torrents)
|
||||
self._client.set_category(category="BangumiCollection", hashes=_hash)
|
||||
|
||||
def rename_subtitles(self, subtitle_list: list[str], _hash):
|
||||
def rename_subtitles(
|
||||
self,
|
||||
subtitle_list: list[str],
|
||||
bangumi_name: str,
|
||||
season: int,
|
||||
_hash
|
||||
):
|
||||
for subtitle_path in subtitle_list:
|
||||
suffix = os.path.splitext(subtitle_path)[-1]
|
||||
old_name = subtitle_path.split(os.path.sep)[-1]
|
||||
new_name = self._renamer.torrent_parser(old_name, suffix)
|
||||
new_name = self._renamer.torrent_parser(
|
||||
method="subtitle",
|
||||
torrent_name=old_name,
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
suffix=suffix
|
||||
)
|
||||
if old_name != new_name:
|
||||
try:
|
||||
self.client.rename_torrent_file(_hash=_hash, old_path=subtitle_path, new_path=new_name)
|
||||
self._client.rename_torrent_file(_hash=_hash, old_path=subtitle_path, new_path=new_name)
|
||||
except Exception as e:
|
||||
logger.warning(f"{old_name} rename failed")
|
||||
logger.warning(f"Suffix: {suffix}")
|
||||
logger.debug(e)
|
||||
|
||||
def delete_bad_torrent(self, info, settings: Config):
|
||||
if settings.bangumi_manage.remove_bad_torrent:
|
||||
self.client.delete_torrent(info.hash)
|
||||
def delete_bad_torrent(self, info, remove_bad_torrent: bool):
|
||||
if remove_bad_torrent:
|
||||
self._client.delete_torrent(info.hash)
|
||||
logger.info(f"{info.name} have been deleted.")
|
||||
|
||||
@staticmethod
|
||||
def get_season_info(save_path: str, settings: Config):
|
||||
def get_season_info(save_path: str, download_path: str):
|
||||
# Remove default save path
|
||||
save_path = save_path.replace(settings.downloader.path, "")
|
||||
save_path = save_path.replace(download_path, "")
|
||||
# Check windows or linux path
|
||||
path_parts = PurePath(save_path).parts \
|
||||
if PurePath(save_path).name != save_path \
|
||||
@@ -135,17 +144,63 @@ class Renamer:
|
||||
|
||||
def rename(self):
|
||||
# Get torrent info
|
||||
download_path = self.settings.downloader.path
|
||||
rename_method = self.settings.bangumi_manage.rename_method
|
||||
remove_bad_torrents = self.settings.bangumi_manage.remove_bad_torrent
|
||||
recent_info, torrent_count = self.get_torrent_info()
|
||||
rename_count = 0
|
||||
for info in recent_info:
|
||||
media_list = self.check_files(info)
|
||||
media_list, subtitle_list = self.check_files(info)
|
||||
bangumi_name, season = self.get_season_info(info.save_path, download_path)
|
||||
if len(media_list) == 1:
|
||||
self.rename_file(info, media_list[0], self.settings)
|
||||
rename_count += 1
|
||||
# TODO: Rename subtitles
|
||||
self.rename_file(
|
||||
info=info,
|
||||
media_path=media_list[0],
|
||||
rename_method=rename_method,
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
remove_bad_torrents=remove_bad_torrents
|
||||
)
|
||||
if len(subtitle_list) > 0:
|
||||
self.rename_subtitles(
|
||||
subtitle_list=subtitle_list,
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
_hash=info.hash
|
||||
)
|
||||
elif len(media_list) > 1:
|
||||
logger.info("Start rename collection")
|
||||
self.rename_collection(info, media_list, self.settings)
|
||||
rename_count += len(media_list)
|
||||
self.rename_collection(
|
||||
info,
|
||||
media_list,
|
||||
bangumi_name,
|
||||
season,
|
||||
remove_bad_torrents
|
||||
)
|
||||
if len(subtitle_list) > 0:
|
||||
self.rename_subtitles(
|
||||
subtitle_list=subtitle_list,
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
_hash=info.hash
|
||||
)
|
||||
else:
|
||||
logger.warning(f"{info.name} has no media file")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from module.conf import settings, setup_logger
|
||||
setup_logger()
|
||||
client = DownloadClient(settings)
|
||||
renamer = Renamer(client, settings)
|
||||
info, _ = renamer.get_torrent_info(category="BangumiCollection")
|
||||
for i in info:
|
||||
_hash = i.hash
|
||||
_, subtitle_list = renamer.check_files(i)
|
||||
print(_hash)
|
||||
bangumi_name, season = renamer.get_season_info(i.save_path, settings.downloader.path)
|
||||
renamer.rename_subtitles(
|
||||
subtitle_list,
|
||||
bangumi_name=bangumi_name,
|
||||
season=season,
|
||||
_hash=_hash
|
||||
)
|
||||
Reference in New Issue
Block a user