From 3a59838ca3a38955f2ce4fa4d27898ddb23e0488 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Wed, 3 May 2023 15:42:25 +0800 Subject: [PATCH] Add subtitle rename --- .gitmodules | 3 + README.md | 7 +- docs/wiki | 1 + src/module/manager/renamer.py | 133 +++++++++++++------ src/module/network/notification.py | 2 + src/module/parser/analyser/torrent_parser.py | 32 ++++- 6 files changed, 133 insertions(+), 45 deletions(-) create mode 100644 .gitmodules create mode 160000 docs/wiki diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..3d0bec7f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "docs/wiki"] + path = docs/wiki + url = https://github.com/EstrellaXD/Auto_Bangumi.wiki.git diff --git a/README.md b/README.md index 57e13639..f566dc13 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,10 @@ -[主项目地址](https://www.github.com/EstrellaXD/Auto_Bangumi) -/ [项目资源仓库](https://www.github.com/EstrellaXD/ab_resource) -/ [WebUI 仓库](https://github.com/Rewrite0/Auto_Bangumi_WebUI) +[主项目地址](https://www.github.com/EstrellaXD/Auto_Bangumi) +/ [WebUI 仓库](https://github.com/Rewrite0/Auto_Bangumi_WebUI) +/ [Wiki 说明](https://www.github.com/EstrellaXD/Auto_Bangumi/wiki) + ## AutoBangumi 功能说明 diff --git a/docs/wiki b/docs/wiki new file mode 160000 index 00000000..3cd7b588 --- /dev/null +++ b/docs/wiki @@ -0,0 +1 @@ +Subproject commit 3cd7b588d8bae6a530ca5468796dd1bff59b1373 diff --git a/src/module/manager/renamer.py b/src/module/manager/renamer.py index faa5a223..8c49d06b 100644 --- a/src/module/manager/renamer.py +++ b/src/module/manager/renamer.py @@ -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 + ) \ No newline at end of file diff --git a/src/module/network/notification.py b/src/module/network/notification.py index b6c5f991..993c231b 100644 --- a/src/module/network/notification.py +++ b/src/module/network/notification.py @@ -42,6 +42,7 @@ class TelegramNotification: } with RequestContent() as req: resp = req.post_data(self.notification_url, data) + logger.debug(f"Telegram notification: {resp.status_code}") return resp.status_code == 200 @@ -58,4 +59,5 @@ class ServerChanNotification: } with RequestContent() as req: resp = req.post_data(self.notification_url, data) + logger.debug(f"ServerChan notification: {resp.status_code}") return resp.status_code == 200 diff --git a/src/module/parser/analyser/torrent_parser.py b/src/module/parser/analyser/torrent_parser.py index dfcb8418..fdfd75bd 100644 --- a/src/module/parser/analyser/torrent_parser.py +++ b/src/module/parser/analyser/torrent_parser.py @@ -22,6 +22,11 @@ RULES = [ r"(.*)E(\d{1,4})(.*)", ] +SUBTITLE_LANG = { + "zh-tw": ["TC", "CHT", "繁", "zh-tw"], + "zh": ["SC", "CHS", "简", "zh"], +} + def rename_init(name, folder_name, season, suffix) -> DownloadInfo: n = re.split(r"[\[\]()【】()]", name) @@ -87,12 +92,33 @@ def rename_none(info: DownloadInfo): return info.name +def rename_subtitle(info: DownloadInfo): + subtitle_lang = "zh" + for key, value in SUBTITLE_LANG.items(): + for lang in value: + if lang in info.name: + subtitle_lang = key + break + for rule in RULES: + match_obj = re.match(rule, info.file_name, re.I) + if match_obj is not None: + title = re.sub(r"([Ss]|Season )\d{1,3}", "", match_obj.group(1)).strip() + title = title if title != "" else info.folder_name + new_name = re.sub( + r"[\[\]]", + "", + f"{title} S{info.season}E{match_obj.group(2)}.{subtitle_lang}{info.suffix}", + ) + return new_name + + METHODS = { "normal": rename_normal, "pn": rename_pn, "advance": rename_advance, "no_season_pn": rename_no_season_pn, - "none": rename_none + "none": rename_none, + "subtitle": rename_subtitle, } @@ -108,6 +134,6 @@ def torrent_parser( if __name__ == "__main__": - name = "[织梦字幕组][鬼灭之刃 锻刀村篇 鬼滅の刃 刀鍛冶の里編][01集][1080P][AVC][简日双语].mp4" - new_name = torrent_parser(name, "异世界舅舅(2022)", 2, ".mp4", "pn") + name = "Lycoris Recoil S01E08.mp4" + new_name = torrent_parser(name, "Lycoris", 1, ".ass", "subtitle") print(new_name)