From a444ff0bf21585a761918892330bd7690885fd4b Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Thu, 19 May 2022 22:14:27 +0800 Subject: [PATCH] fix bug --- Windows/app/auto_set_rule.py | 56 +++++++++++++++++ Windows/app/collect_bangumi_info.py | 87 +++++++++++++++++++++++++++ Windows/app/rename_qb.py | 93 +++++++++++++++++++++++++++++ Windows/config/bangumi.json | 58 ++++++++++++++++++ Windows/config/config.json | 8 +++ Windows/config/rule.json | 48 +++++++++++++++ Windows/win_main.py | 20 +++++++ 7 files changed, 370 insertions(+) create mode 100644 Windows/app/auto_set_rule.py create mode 100644 Windows/app/collect_bangumi_info.py create mode 100644 Windows/app/rename_qb.py create mode 100644 Windows/config/bangumi.json create mode 100644 Windows/config/config.json create mode 100644 Windows/config/rule.json create mode 100644 Windows/win_main.py diff --git a/Windows/app/auto_set_rule.py b/Windows/app/auto_set_rule.py new file mode 100644 index 00000000..6bc63f8e --- /dev/null +++ b/Windows/app/auto_set_rule.py @@ -0,0 +1,56 @@ +import sys +import time + +import qbittorrentapi +import json +import os + + +class SetRule: + def __init__(self, config): + with open("../config/bangumi.json") as f: + self.bangumi_info = json.load(f) + self.rss_link = config["rss_link"] + self.host_ip = config["host_ip"] + self.user_name = config["user_name"] + self.password = config["password"] + self.download_path = config["download_path"] + self.qb = qbittorrentapi.Client(host=self.host_ip, username=self.user_name, password=self.password) + try: + self.qb.auth_log_in() + except qbittorrentapi.LoginFailed as e: + print(e) + + def set_rule(self, bangumi_name, season): + rule = { + 'enable': True, + 'mustContain': bangumi_name, + 'mustNotContain': '720', + 'useRegx': True, + 'episodeFilter': '', + 'smartFilter': False, + 'previouslyMatchedEpisodes': [], + 'affectedFeeds': [self.rss_link], + 'ignoreDays': 0, + 'lastMatch': '', + 'addPaused': False, + 'assignedCategory': 'Bangumi', + 'savePath': str(os.path.join(self.download_path, bangumi_name, season)) + } + self.qb.rss_set_rule(rule_name=bangumi_name, rule_def=rule) + + def add_rss_feed(self): + try: + self.qb.rss_add_feed(self.rss_link) + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] Successes adding RSS Feed." + "\n") + except ConnectionError: + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] Error with adding RSS Feed." + "\n") + + def run(self): + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] Start adding rules." + "\n") + sys.stdout.flush() + for info in self.bangumi_info: + self.set_rule(info["title"], info["season"]) + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] Finished." + "\n") + sys.stdout.flush() + diff --git a/Windows/app/collect_bangumi_info.py b/Windows/app/collect_bangumi_info.py new file mode 100644 index 00000000..dbcba356 --- /dev/null +++ b/Windows/app/collect_bangumi_info.py @@ -0,0 +1,87 @@ +# -*- coding: UTF-8 -*- +import sys +import time +import requests +from bs4 import BeautifulSoup +import json +import re + + +class CollectRSS: + def __init__(self, config, info, rules): + self.bangumi_list = [] + self.rules = rules + rss = requests.get(config["rss_link"], 'utf-8') + soup = BeautifulSoup(rss.text, 'xml') + self.items = soup.find_all('item') + self.info = info + + def get_info_list(self): + split_rule = r"\[|\]|\【|\】|\★|\(|\)|\(|\)" + last_rule = r"(.*)( \-)" + for item in self.items: + name = item.title.string + exit_flag = False + for rule in self.rules: + for group in rule["group_name"]: + if re.search(group, name): + exit_flag = True + n = re.split(split_rule, name) + while '' in n: + n.remove('') + while ' ' in n: + n.remove(' ') + try: + bangumi_title = n[rule['name_position']].strip() + except IndexError: + continue + sub_title = re.sub(r"[^\x00-\xff]{1,}| \d{1,2}|\·","",bangumi_title) + b = re.split(r"\/|\_", sub_title) + while '' in b: + b.remove('') + pre_name = max(b, key=len, default='').strip() + if pre_name != '': + bangumi_title = pre_name + for i in range(2): + match_obj = re.match(last_rule, bangumi_title, re.I) + if match_obj is not None: + bangumi_title = match_obj.group(1).strip() + match_obj = re.match(r"(S\d{1,2}(.*))", bangumi_title, re.I) + if match_obj is not None: + bangumi_title = match_obj.group(2).strip() + if bangumi_title not in self.bangumi_list: + self.bangumi_list.append(bangumi_title) + break + if exit_flag: + break + if not exit_flag: + print(f"[{time.strftime('%Y-%m-%d %X')}] ERROR Not match with {name}") + + def put_info_json(self): + season_match = r"(.*)(Season \d{1,2}|S\d{1,2}|第.*季)" + + had_data = [] + for data in self.info: + had_data.append(data["title"]) + + for title in self.bangumi_list: + match_title_season = re.match(season_match, title, re.I) + if match_title_season is not None: + json_title = match_title_season.group(1).strip() + json_season = match_title_season.group(2) + else: + json_season = '' + json_title = title + if json_title not in had_data: + self.info.append({ + "title": json_title, + "season": json_season + }) + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] add {json_title} {json_season}" + "\n") + sys.stdout.flush() + with open("../config/bangumi.json", 'w', encoding='utf8') as f: + json.dump(self.info, f, indent=4, separators=(',', ': '), ensure_ascii=False) + + def run(self): + self.get_info_list() + self.put_info_json() \ No newline at end of file diff --git a/Windows/app/rename_qb.py b/Windows/app/rename_qb.py new file mode 100644 index 00000000..700c9469 --- /dev/null +++ b/Windows/app/rename_qb.py @@ -0,0 +1,93 @@ +import re +import sys +import qbittorrentapi +import time + + +class QbittorrentRename: + def __init__(self, config): + self.qbt_client = qbittorrentapi.Client(host=config['host_ip'], username=config['user_name'], password=config['password']) + try: + self.qbt_client.auth_log_in() + except qbittorrentapi.LoginFailed as e: + print(e) + self.recent_info = self.qbt_client.torrents_info(status_filter='completed') + self.hash = None + self.name = None + self.new_name = None + self.path_name = None + self.count = 0 + self.rename_count = 0 + self.torrent_count = len(self.recent_info) + self.method = config['method'] + self.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)?\](.*)', + r'(.*)\[第(\d*\.*\d*)话(?:END)?\](.*)', + r'(.*)\[第(\d*\.*\d*)話(?:END)?\](.*)', + r'(.*)第(\d*\.*\d*)话(?:END)?(.*)', + r'(.*)第(\d*\.*\d*)話(?:END)?(.*)', + r'(.*)- (\d{1,3}|\d{1,3}\.\d{1,2})(?:v\d{1,2})?(?:END)? (.*)'] + + def rename_normal(self, idx): + self.name = self.recent_info[idx].name + self.hash = self.recent_info[idx].hash + self.path_name = self.recent_info[idx].content_path.split("/")[-1] + file_name = self.name + for rule in self.rules: + matchObj = re.match(rule, file_name, re.I) + if matchObj is not None: + self.new_name = f'{matchObj.group(1).strip()} E{matchObj.group(2)}{matchObj.group(3)}' + + def rename_pn(self, idx): + self.name = self.recent_info[idx].name + self.hash = self.recent_info[idx].hash + self.path_name = self.recent_info[idx].content_path.split("/")[-1] + n = re.split(r'\[|\]', self.name) + file_name = self.name.replace(f'[{n[1]}]', '') + for rule in self.rules: + matchObj = re.match(rule, file_name, re.I) + if matchObj is not None: + self.new_name = re.sub(r'\[|\]', '', f'{matchObj.group(1).strip()} E{matchObj.group(2)}{n[-1]}') + + def rename(self): + if self.path_name != self.new_name: + self.qbt_client.torrents_rename_file(torrent_hash=self.hash, old_path=self.path_name, new_path=self.new_name) + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] {self.path_name} >> {self.new_name}") + self.count += 1 + else: + return + + def clear_info(self): + self.name = None + self.hash = None + self.new_name = None + self.path_name = None + + def print_result(self): + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] 已完成对{self.torrent_count}个文件的检查" + '\n') + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] 已对其中{self.count}个文件进行重命名" + '\n') + sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] 完成" + '\n') + sys.stdout.flush() + + def run(self): + 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: + 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: + self.print_result() + + + diff --git a/Windows/config/bangumi.json b/Windows/config/bangumi.json new file mode 100644 index 00000000..4c9b023c --- /dev/null +++ b/Windows/config/bangumi.json @@ -0,0 +1,58 @@ +[ + { + "title": "simple", + "season": "" + }, + { + "title": "Kawaii dake ja Nai Shikimori-san", + "season": "" + }, + { + "title": "Kakkou no Iinazuke", + "season": "" + }, + { + "title": "SPYxFAMILY", + "season": "" + }, + { + "title": "Love Live!虹咲学园 学园偶像同好会", + "season": "S02" + }, + { + "title": "CUE!", + "season": "" + }, + { + "title": "Kaguya-sama wa Kokurasetai", + "season": "S03" + }, + { + "title": "Shokei Shoujo no Virgin Road", + "season": "" + }, + { + "title": "Kakkou no Iikagen", + "season": "" + }, + { + "title": "Summer Time Rendering", + "season": "" + }, + { + "title": "Paripi Koumei", + "season": "" + }, + { + "title": "Tate no Yuusha no Nariagari", + "season": "S02" + }, + { + "title": "Shijou Saikyou no Daimaou", + "season": "" + }, + { + "title": "Yuusha, Yamemasu", + "season": "" + } +] \ No newline at end of file diff --git a/Windows/config/config.json b/Windows/config/config.json new file mode 100644 index 00000000..73fb7cfb --- /dev/null +++ b/Windows/config/config.json @@ -0,0 +1,8 @@ +{ + "host_ip": "192.168.31.10:8181", + "user_name": "admin", + "password": "adminadmin", + "method": "pn", + "rss_link": "https://mikanani.me/RSS/MyBangumi?token=qTxKo48gH1SrFNy8X%2fCfQUoeElNsgKNWFNzNieKwBH8%3d", + "download_path": "/downloads/Bangumi" +} \ No newline at end of file diff --git a/Windows/config/rule.json b/Windows/config/rule.json new file mode 100644 index 00000000..e5ead51b --- /dev/null +++ b/Windows/config/rule.json @@ -0,0 +1,48 @@ +[ + { + "group_name": [ + "Lilith-Raws", + "NC-Raws", + "Skymoon-Raws", + "天月搬运组", + "LoliHouse", + "猎户不鸽发布组", + "NaN-Raws", + "猎户随缘发布组", + "桜都字幕组", + "澄空学园&雪飘工作室", + "千夏字幕组", + "IET字幕组", + "离谱Sub", + "酷漫404", + "星空字幕组", + "轻之国度字幕组", + "枫叶字幕组", + "雪飘工作室", + "豌豆字幕组", + "云光字幕组", + "悠哈璃羽字幕社", + "桜都字幕组", + "ANi", + "❀拨雪寻春❀", + "极彩字幕组", + "悠哈璃羽字幕社", + "漫猫字幕组", + "MingY" + ], + "name_position": 1 + }, + { + "group_name": [ + "喵萌奶茶屋", + "爱恋字母社", + "诸神字幕组", + "驯兽师联盟", + "夏沐字幕组", + "幻樱", + "动漫国字幕组", + "SweetSub&圆环记录攻略组" + ], + "name_position": 2 + } +] \ No newline at end of file diff --git a/Windows/win_main.py b/Windows/win_main.py new file mode 100644 index 00000000..6fd5ca4a --- /dev/null +++ b/Windows/win_main.py @@ -0,0 +1,20 @@ +import os +import time +from app.collect_bangumi_info import CollectRSS +from app.auto_set_rule import SetRule +from app.rename_qb import QbittorrentRename +import json + + +if __name__ == "__main__": + with open("config/config.json") as f: + config = json.load(f) + while True: + with open("config/bangumi.json") as f: + info = json.load(f) + with open("../config/rule.json") as f: + rule = json.load(f) + CollectRSS(config, info, rule).run() + SetRule(config).run() + QbittorrentRename(config).run() + time.sleep(float(1800))