From a01807af11bcdcc5c9a976052f4e14905be247eb Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Tue, 17 May 2022 00:17:09 +0800 Subject: [PATCH] fix bugs --- .idea/Bangumi_Auto_Rename.iml | 2 +- .idea/misc.xml | 2 +- Auto_set_rules/auto_set_rule.py | 42 ++++++++++++++++++++++++- Auto_set_rules/bangumi.json | 43 ++++++++++++++++++-------- Auto_set_rules/collect_bangumi_info.py | 15 ++++++--- Auto_set_rules/test.py | 12 ++++--- Docker/rename_qb.py | 39 +++++++++++++---------- rename_qb.py | 37 ++++++++++++---------- 8 files changed, 134 insertions(+), 58 deletions(-) diff --git a/.idea/Bangumi_Auto_Rename.iml b/.idea/Bangumi_Auto_Rename.iml index d870a4a7..8437fe66 100644 --- a/.idea/Bangumi_Auto_Rename.iml +++ b/.idea/Bangumi_Auto_Rename.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index ab530bfd..dc9ea490 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Auto_set_rules/auto_set_rule.py b/Auto_set_rules/auto_set_rule.py index 8045146c..dbbec8ac 100644 --- a/Auto_set_rules/auto_set_rule.py +++ b/Auto_set_rules/auto_set_rule.py @@ -1 +1,41 @@ -import qbittorrentapi \ No newline at end of file +import qbittorrentapi +import json +import os + +with open("config.json") as f: + info = json.load(f) + +with open("bangumi.json") as f: + bangumi_info = json.load(f) +rss_link = info["rss_link"] +host_ip = "192.168.31.10:8181" +user_name = "admin" +password = "adminadmin" + + +def set_rule(bangumi_name, season): + qb = qbittorrentapi.Client(host=host_ip, username=user_name, password=password) + try: + qb.auth_log_in() + except qbittorrentapi.LoginFailed as e: + print(e) + rule = { + 'enable': False, + 'mustContain': bangumi_name, + 'mustNotContain': '720', + 'useRegx': True, + 'episodeFilter': '', + 'smartFilter': False, + 'previouslyMatchedEpisodes': [], + 'affectedFeeds': [rss_link], + 'ignoreDays': 0, + 'lastMatch': '', + 'addPaused': False, + 'assignedCategory': 'Bangumi', + 'savePath': os.path.join('/downloads', bangumi_name, season) + } + qb.rss_set_rule(rule_name=bangumi_name, rule_def=rule) + +if __name__ == "__main__": + for info in bangumi_info: + set_rule(info["title"], info["season"]) \ No newline at end of file diff --git a/Auto_set_rules/bangumi.json b/Auto_set_rules/bangumi.json index ef179aaf..4c9b023c 100644 --- a/Auto_set_rules/bangumi.json +++ b/Auto_set_rules/bangumi.json @@ -1,41 +1,58 @@ [ { - "title": "Kawaii dake ja Nai Shikimori-san " + "title": "simple", + "season": "" }, { - "title": "Kakkou no Iinazuke " + "title": "Kawaii dake ja Nai Shikimori-san", + "season": "" }, { - "title": "SPYxFAMILY " + "title": "Kakkou no Iinazuke", + "season": "" }, { - "title": "Love Live!虹咲学园 学园偶像同好会 S02 " + "title": "SPYxFAMILY", + "season": "" }, { - "title": "CUE!" + "title": "Love Live!虹咲学园 学园偶像同好会", + "season": "S02" }, { - "title": "Kaguya-sama wa Kokurasetai S03 " + "title": "CUE!", + "season": "" }, { - "title": "Shokei Shoujo no Virgin Road " + "title": "Kaguya-sama wa Kokurasetai", + "season": "S03" }, { - "title": "Kakkou no Iikagen " + "title": "Shokei Shoujo no Virgin Road", + "season": "" }, { - "title": "Summer Time Rendering" + "title": "Kakkou no Iikagen", + "season": "" }, { - "title": "Paripi Koumei " + "title": "Summer Time Rendering", + "season": "" }, { - "title": "Tate no Yuusha no Nariagari S02 " + "title": "Paripi Koumei", + "season": "" }, { - "title": "Shijou Saikyou no Daimaou " + "title": "Tate no Yuusha no Nariagari", + "season": "S02" }, { - "title": "Yuusha, Yamemasu " + "title": "Shijou Saikyou no Daimaou", + "season": "" + }, + { + "title": "Yuusha, Yamemasu", + "season": "" } ] \ No newline at end of file diff --git a/Auto_set_rules/collect_bangumi_info.py b/Auto_set_rules/collect_bangumi_info.py index e36eef37..64464851 100644 --- a/Auto_set_rules/collect_bangumi_info.py +++ b/Auto_set_rules/collect_bangumi_info.py @@ -33,7 +33,7 @@ for a in item: matchObj = re.match(rule, name, re.I) if matchObj is not None: new_name = re.sub(r'\[|\]', '', f'{matchObj.group(1)}') - new_name = re.split(r'/', new_name)[-1].lstrip() + new_name = re.split(r'/', new_name)[-1].strip() if new_name not in bangumi_title: bangumi_title.append(new_name) @@ -44,13 +44,20 @@ had_data = [] for data in bangumi_info: had_data.append(data["title"]) -season_rules = r'S\d' +season_rules = r'(.*)(S.\d)' for title in bangumi_title: + a = re.match(season_rules, title, re.I) + if a is not None: + title = a.group(1).strip() + season = a.group(2).strip() + else: + season = '' if title not in had_data: bangumi_info.append({ - "title": title + "title": title, + "season": season }) - print(f"add {title}") + print(f"add {title} {season}") with open("bangumi.json", 'w', encoding='utf8') as f: diff --git a/Auto_set_rules/test.py b/Auto_set_rules/test.py index 0447a00c..49a9767d 100644 --- a/Auto_set_rules/test.py +++ b/Auto_set_rules/test.py @@ -1,6 +1,10 @@ import re -name = "Tate no Yuusha no Nariagari S02 " +name = "Tate no Yuusha no Nariagari S02" -season_rules = r'S(\d{0,3}|\d{1,3}\.\d{1,2})' -a = re.match(season_rules, name) -print(a) \ No newline at end of file +season_rules = r'(.*)(S.\d)' +a = re.match(season_rules, name, re.I) +if a is not None: + print(a.group(1)) + print(a.group(2)) +else: + print(name) \ No newline at end of file diff --git a/Docker/rename_qb.py b/Docker/rename_qb.py index 9b887fd3..d0cae982 100644 --- a/Docker/rename_qb.py +++ b/Docker/rename_qb.py @@ -1,4 +1,3 @@ -import argparse import re import io import sys @@ -12,6 +11,7 @@ password = environ['PASSWORD'] method = environ['METHOD'] delay_time = environ['TIME'] + # Episode Regular Expression Matching Rules episode_rules = [r'(.*)\[(\d{1,3}|\d{1,3}\.\d{1,2})(?:v\d{1,2})?(?:END)?\](.*)', r'(.*)\[E(\d{1,3}|\d{1,3}\.\d{1,2})(?:v\d{1,2})?(?:END)?\](.*)', @@ -23,10 +23,6 @@ episode_rules = [r'(.*)\[(\d{1,3}|\d{1,3}\.\d{1,2})(?:v\d{1,2})?(?:END)?\](.*)', # Suffixs of files we are going to rename suffixs = ['mp4', 'mkv', 'avi', 'mov', 'flv', 'rmvb', 'ass', 'idx'] sys.stdout = io.TextIOWrapper(buffer=sys.stdout.buffer, encoding='utf8') -parser = argparse.ArgumentParser(description='Regular Expression Match') -parser.add_argument('--hash', default='', - help='The torrent Hash value.') -args = parser.parse_args() class QbittorrentRename: @@ -47,6 +43,7 @@ class QbittorrentRename: def rename_normal(self, idx): self.name = self.recent_info[idx].name + self.hash = self.recent_info[idx].hash file_name = self.name for rule in episode_rules: matchObj = re.match(rule, file_name, re.I) @@ -55,6 +52,7 @@ class QbittorrentRename: def rename_pn(self, idx): self.name = self.recent_info[idx].name + self.hash = self.recent_info[idx].hash n = re.split(r'\[|\]', self.name) file_name = self.name.replace(f'[{n[1]}]', '') for rule in episode_rules: @@ -62,36 +60,43 @@ class QbittorrentRename: if matchObj is not None: self.new_name = re.sub(r'\[|\]', '', f'{matchObj.group(1)} E{matchObj.group(2)}.{n[-1]}') - def rename_hash(self, torrent_hash): - self.hash = torrent_hash + def rename(self): try: self.qbt_client.torrents_rename_file(torrent_hash=self.hash, old_path=self.name, new_path=self.new_name) - self.count += 1 print(f'{self.name} >> {self.new_name}') + self.count += 1 except: return + def clear_info(self): + self.name = None + self.hash = None + self.new_name = None + + def print_result(self): + sys.stdout.write(f"-----已完成对{self.torrent_count}个文件的检查,已对其中{self.count}个文件进行重命名-----" + '\n') + sys.stdout.write("------------------------完成------------------------" + '\n') + sys.stdout.flush() + def rename_app(self): - if self.method not in ['pn', 'normal', 'hash']: + if self.method not in ['pn', 'normal']: print('error method') elif self.method == 'normal': for i in range(0, self.torrent_count + 1): try: self.rename_normal(i) + self.rename() + self.clear_info() except: - sys.stdout.write(f"-----已完成对{i + 1}个文件的检查,已对其中{self.count}个文件进行重命名-----" + '\n') - sys.stdout.write("------------------------完成------------------------" + '\n') - sys.stdout.flush() + self.print_result() elif self.method == 'pn': for i in range(0, self.torrent_count + 1): try: self.rename_pn(i) + self.rename() + self.clear_info() except: - sys.stdout.write(f"-----已完成对{i + 1}个文件的检查,已对其中{self.count}个文件进行重命名-----" + '\n') - sys.stdout.write("------------------------完成------------------------" + '\n') - sys.stdout.flush() - elif self.method == 'hash': - self.rename_hash(args.hash) + self.print_result() if __name__ == "__main__": diff --git a/rename_qb.py b/rename_qb.py index 183a4783..b6044793 100644 --- a/rename_qb.py +++ b/rename_qb.py @@ -1,4 +1,3 @@ -import argparse import re import io import sys @@ -25,10 +24,6 @@ episode_rules = [r'(.*)\[(\d{1,3}|\d{1,3}\.\d{1,2})(?:v\d{1,2})?(?:END)?\](.*)', # Suffixs of files we are going to rename suffixs = ['mp4', 'mkv', 'avi', 'mov', 'flv', 'rmvb', 'ass', 'idx'] sys.stdout = io.TextIOWrapper(buffer=sys.stdout.buffer, encoding='utf8') -parser = argparse.ArgumentParser(description='Regular Expression Match') -parser.add_argument('--hash', default='', - help='The torrent Hash value.') -args = parser.parse_args() class QbittorrentRename: @@ -49,6 +44,7 @@ class QbittorrentRename: def rename_normal(self, idx): self.name = self.recent_info[idx].name + self.hash = self.recent_info[idx].hash file_name = self.name for rule in episode_rules: matchObj = re.match(rule, file_name, re.I) @@ -57,6 +53,7 @@ class QbittorrentRename: def rename_pn(self, idx): self.name = self.recent_info[idx].name + self.hash = self.recent_info[idx].hash n = re.split(r'\[|\]', self.name) file_name = self.name.replace(f'[{n[1]}]', '') for rule in episode_rules: @@ -64,36 +61,42 @@ class QbittorrentRename: if matchObj is not None: self.new_name = re.sub(r'\[|\]', '', f'{matchObj.group(1)} E{matchObj.group(2)}.{n[-1]}') - def rename_hash(self, torrent_hash): - self.hash = torrent_hash + def rename(self): try: self.qbt_client.torrents_rename_file(torrent_hash=self.hash, old_path=self.name, new_path=self.new_name) - self.count += 1 print(f'{self.name} >> {self.new_name}') + self.count += 1 except: return + def clear_info(self): + self.name = None + self.hash = None + self.new_name = None + + def print_result(self): + print(f"-----已完成对{self.torrent_count}个文件的检查,已对其中{self.count}个文件进行重命名-----") + print("------------------------完成------------------------") + def rename_app(self): - if self.method not in ['pn', 'normal', 'hash']: + if self.method not in ['pn', 'normal']: print('error method') elif self.method == 'normal': for i in range(0, self.torrent_count + 1): try: self.rename_normal(i) + self.rename() + self.clear_info() except: - print(f"-----已完成对{i + 1}个文件的检查,已对其中{self.count}个文件进行重命名-----") - print("------------------------完成------------------------") - quit() + self.print_result() elif self.method == 'pn': for i in range(0, self.torrent_count + 1): try: self.rename_pn(i) + self.rename() + self.clear_info() except: - print(f"-----已完成对{i + 1}个文件的检查,已对其中{self.count}个文件进行重命名-----") - print("------------------------完成------------------------") - quit() - elif self.method == 'hash': - self.rename_hash(args.hash) + self.print_result() if __name__ == "__main__":