mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-12-01 00:38:13 +08:00
优化代码
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
import sys
|
||||
import time
|
||||
|
||||
from docker_main import EnvInfo
|
||||
import qbittorrentapi
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
class SetRule:
|
||||
def __init__(self, config):
|
||||
with open("/config/bangumi.json") as f:
|
||||
def __init__(self):
|
||||
with open(EnvInfo.info_path) 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.rss_link = EnvInfo.rss_link
|
||||
self.host_ip = EnvInfo.host_ip
|
||||
self.user_name = EnvInfo.user_name
|
||||
self.password = EnvInfo.password
|
||||
self.download_path = EnvInfo.download_path
|
||||
self.qb = qbittorrentapi.Client(host=self.host_ip, username=self.user_name, password=self.password)
|
||||
try:
|
||||
self.qb.auth_log_in()
|
||||
@@ -42,15 +41,15 @@ class SetRule:
|
||||
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")
|
||||
sys.stdout.write(f"[{EnvInfo.time_show_obj}] Successes adding RSS Feed." + "\n")
|
||||
except ConnectionError:
|
||||
sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] Error with adding RSS Feed." + "\n")
|
||||
sys.stdout.write(f"[{EnvInfo.time_show_obj}] 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.write(f"[{EnvInfo.time_show_obj}] 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.write(f"[{EnvInfo.time_show_obj}] Finished." + "\n")
|
||||
sys.stdout.flush()
|
||||
|
||||
@@ -5,21 +5,34 @@ import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
import re
|
||||
from docker_main import EnvInfo
|
||||
|
||||
|
||||
class MatchRule:
|
||||
split_rule = r"\[|\]|\【|\】|\★|\(|\)|\(|\)"
|
||||
last_rule = r"(.*)( \-)"
|
||||
sub_title = r"[^\x00-\xff]{1,}| \d{1,2}|\·"
|
||||
match_rule = r"(S\d{1,2}(.*))"
|
||||
season_match = r"(.*)(Season \d{1,2}|S\d{1,2}|第.*季|第.*期)"
|
||||
|
||||
|
||||
class CollectRSS:
|
||||
def __init__(self, config, info):
|
||||
def __init__(self):
|
||||
self.bangumi_list = []
|
||||
with open("/app/rule.json") as f:
|
||||
self.rules = json.load(f)
|
||||
rss = requests.get(config["rss_link"], 'utf-8')
|
||||
try:
|
||||
self.rules = requests.get(EnvInfo.rule_url).json()
|
||||
with open(EnvInfo.rule_path, 'w') as f:
|
||||
json.dump(self.rules, f, indent=4, separators=(',', ': '), ensure_ascii=False)
|
||||
except ConnectionError:
|
||||
with open(EnvInfo.rule_path) as f:
|
||||
self.rules = json.load(f)
|
||||
rss = requests.get(EnvInfo.rss_link, 'utf-8')
|
||||
soup = BeautifulSoup(rss.text, 'xml')
|
||||
self.items = soup.find_all('item')
|
||||
self.info = info
|
||||
with open(EnvInfo.info_path) as f:
|
||||
self.info = json.load(f)
|
||||
|
||||
def get_info_list(self):
|
||||
split_rule = r"\[|\]|\【|\】|\★|\(|\)|\(|\)"
|
||||
last_rule = r"(.*)( \-)"
|
||||
for item in self.items:
|
||||
name = item.title.string
|
||||
exit_flag = False
|
||||
@@ -27,7 +40,7 @@ class CollectRSS:
|
||||
for group in rule["group_name"]:
|
||||
if re.search(group, name):
|
||||
exit_flag = True
|
||||
n = re.split(split_rule, name)
|
||||
n = re.split(MatchRule.split_rule, name)
|
||||
while '' in n:
|
||||
n.remove('')
|
||||
while ' ' in n:
|
||||
@@ -36,7 +49,7 @@ class CollectRSS:
|
||||
bangumi_title = n[rule['name_position']].strip()
|
||||
except IndexError:
|
||||
continue
|
||||
sub_title = re.sub(r"[^\x00-\xff]{1,}| \d{1,2}|\·","",bangumi_title)
|
||||
sub_title = re.sub(MatchRule.sub_title, "", bangumi_title)
|
||||
b = re.split(r"\/|\_", sub_title)
|
||||
while '' in b:
|
||||
b.remove('')
|
||||
@@ -44,10 +57,10 @@ class CollectRSS:
|
||||
if pre_name != '':
|
||||
bangumi_title = pre_name
|
||||
for i in range(2):
|
||||
match_obj = re.match(last_rule, bangumi_title, re.I)
|
||||
match_obj = re.match(MatchRule.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)
|
||||
match_obj = re.match(MatchRule.match_rule, 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:
|
||||
@@ -56,17 +69,14 @@ class CollectRSS:
|
||||
if exit_flag:
|
||||
break
|
||||
if not exit_flag:
|
||||
print(f"[{time.strftime('%Y-%m-%d %X')}] ERROR Not match with {name}")
|
||||
print(f"[{EnvInfo.time_show_obj}] 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)
|
||||
match_title_season = re.match(MatchRule.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)
|
||||
@@ -78,11 +88,11 @@ class CollectRSS:
|
||||
"title": json_title,
|
||||
"season": json_season
|
||||
})
|
||||
sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}] add {json_title} {json_season}" + "\n")
|
||||
sys.stdout.write(f"[{EnvInfo.time_show_obj}] add {json_title} {json_season}" + "\n")
|
||||
sys.stdout.flush()
|
||||
with open("/config/bangumi.json", 'w', encoding='utf8') as f:
|
||||
with open(EnvInfo.info_path, '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()
|
||||
self.put_info_json()
|
||||
36
AutoBangumi/app/docker_main.py
Normal file
36
AutoBangumi/app/docker_main.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import os
|
||||
import time
|
||||
from collect_bangumi_info import CollectRSS
|
||||
from auto_set_rule import SetRule
|
||||
from rename_qb import qBittorrentRename
|
||||
import json
|
||||
|
||||
|
||||
class EnvInfo:
|
||||
info_path = "/config/bangumi.json"
|
||||
host_ip = os.environ["HOST"]
|
||||
sleep_time = float(os.environ["TIME"])
|
||||
user_name = os.environ["USER"]
|
||||
password = os.environ["PASSWORD"]
|
||||
rss_link = os.environ["RSS"]
|
||||
download_path = os.environ["DOWNLOAD_PATH"]
|
||||
method = os.environ["METHOD"]
|
||||
rule_url = "https://raw.githubusercontent.com/EstrellaXD/Bangumi_Auto_Collector/main/AutoBangumi2.0/app/rule.json"
|
||||
rule_path = "/app/rule.json"
|
||||
time_show_obj = time.strftime('%Y-%m-%d %X')
|
||||
|
||||
|
||||
def create_data_file():
|
||||
if not os.path.exists(EnvInfo.info_path):
|
||||
bangumi_info = []
|
||||
with open(EnvInfo.info_path, "w") as i:
|
||||
json.dump(bangumi_info, i, indent=4, separators=(',', ': '), ensure_ascii=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_data_file()
|
||||
while True:
|
||||
CollectRSS().run()
|
||||
SetRule().run()
|
||||
qBittorrentRename().run()
|
||||
time.sleep(EnvInfo.sleep_time)
|
||||
@@ -1,14 +1,15 @@
|
||||
import re
|
||||
import io
|
||||
import sys
|
||||
import qbittorrentapi
|
||||
from os import environ
|
||||
import time
|
||||
from docker_main import EnvInfo
|
||||
|
||||
|
||||
class QbittorrentRename:
|
||||
def __init__(self, config):
|
||||
self.qbt_client = qbittorrentapi.Client(host=config['host_ip'], username=config['user_name'], password=config['password'])
|
||||
class qBittorrentRename:
|
||||
def __init__(self):
|
||||
self.qbt_client = qbittorrentapi.Client(host=EnvInfo.host_ip,
|
||||
username=EnvInfo.user_name,
|
||||
password=EnvInfo.password)
|
||||
try:
|
||||
self.qbt_client.auth_log_in()
|
||||
except qbittorrentapi.LoginFailed as e:
|
||||
@@ -21,7 +22,6 @@ class QbittorrentRename:
|
||||
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)?\](.*)',
|
||||
@@ -66,15 +66,15 @@ class QbittorrentRename:
|
||||
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.write(f"[{EnvInfo.time_show_obj}] 已完成对{self.torrent_count}个文件的检查" + '\n')
|
||||
sys.stdout.write(f"[{EnvInfo.time_show_obj}] 已对其中{self.count}个文件进行重命名" + '\n')
|
||||
sys.stdout.write(f"[{EnvInfo.time_show_obj}] 完成" + '\n')
|
||||
sys.stdout.flush()
|
||||
|
||||
def run(self):
|
||||
if self.method not in ['pn', 'normal']:
|
||||
if EnvInfo.method not in ['pn', 'normal']:
|
||||
print('error method')
|
||||
elif self.method == 'normal':
|
||||
elif EnvInfo.method == 'normal':
|
||||
for i in range(0, self.torrent_count + 1):
|
||||
try:
|
||||
self.rename_normal(i)
|
||||
@@ -82,7 +82,7 @@ class QbittorrentRename:
|
||||
self.clear_info()
|
||||
except:
|
||||
self.print_result()
|
||||
elif self.method == 'pn':
|
||||
elif EnvInfo.method == 'pn':
|
||||
for i in range(0, self.torrent_count + 1):
|
||||
try:
|
||||
self.rename_pn(i)
|
||||
@@ -41,7 +41,9 @@
|
||||
"夏沐字幕组",
|
||||
"幻樱",
|
||||
"动漫国字幕组",
|
||||
"SweetSub&圆环记录攻略组"
|
||||
"SweetSub&圆环记录攻略组",
|
||||
"动漫萌",
|
||||
"极影字幕社"
|
||||
],
|
||||
"name_position": 2
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import os
|
||||
import time
|
||||
from collect_bangumi_info import CollectRSS
|
||||
from auto_set_rule import SetRule
|
||||
from rename_qb import QbittorrentRename
|
||||
import json
|
||||
|
||||
|
||||
config_path = "/config/config.json"
|
||||
info_path = "/config/bangumi.json"
|
||||
host_ip = os.environ["HOST"]
|
||||
sleep_time = os.environ["TIME"]
|
||||
user_name = os.environ["USER"]
|
||||
password = os.environ["PASSWORD"]
|
||||
rss_link = os.environ["RSS"]
|
||||
download_path = os.environ["DOWNLOAD_PATH"]
|
||||
method = os.environ["METHOD"]
|
||||
|
||||
|
||||
def create_config():
|
||||
if not os.path.exists(config_path):
|
||||
config = {
|
||||
"host_ip": host_ip,
|
||||
"user_name": user_name,
|
||||
"password": password,
|
||||
"method": method,
|
||||
"rss_link": rss_link,
|
||||
"download_path": download_path
|
||||
}
|
||||
SetRule(config).add_rss_feed()
|
||||
with open(config_path, "w") as c:
|
||||
json.dump(config, c, indent=4, separators=(',', ': '), ensure_ascii=False)
|
||||
if not os.path.exists(info_path):
|
||||
bangumi_info = []
|
||||
with open(info_path, "w") as i:
|
||||
json.dump(bangumi_info, i, indent=4, separators=(',', ': '), ensure_ascii=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_config()
|
||||
with open(config_path) as f:
|
||||
config = json.load(f)
|
||||
while True:
|
||||
with open(info_path) as f:
|
||||
info = json.load(f)
|
||||
CollectRSS(config, info).run()
|
||||
SetRule(config).run()
|
||||
QbittorrentRename(config).run()
|
||||
time.sleep(float(sleep_time))
|
||||
10
test/test.py
10
test/test.py
@@ -1,15 +1,11 @@
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
|
||||
url = "https://raw.githubusercontent.com/EstrellaXD/Bangumi_Auto_Collector/main/AutoBangumi2.0/app/rule.json"
|
||||
try:
|
||||
r = requests.get(url)
|
||||
except ConnectionError:
|
||||
print("e")
|
||||
|
||||
print(r.text)
|
||||
rules = requests.get("https://raw.githubusercontent.com/EstrellaXD/Bangumi_Auto_Collector/main/AutoBangumi2.0/app/rule.json").json()
|
||||
print(rules)
|
||||
@@ -1,4 +1,4 @@
|
||||
import time
|
||||
import sys
|
||||
from AutoBangumi.app.collect_bangumi_info import CollectRSS
|
||||
|
||||
sys.stdout.write(f"[{time.strftime('%Y-%m-%d %X')}]")
|
||||
|
||||
cr = CollectRSS(config=)
|
||||
Reference in New Issue
Block a user