mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-13 18:11:03 +08:00
2.5.7 优化代码,修复全集收集只有第一次运行的bug,完善api功能
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import re
|
||||
|
||||
import uvicorn
|
||||
from uvicorn.config import LOGGING_CONFIG
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from pydantic import BaseModel
|
||||
import logging
|
||||
|
||||
from core import RSSAnalyser
|
||||
from core import DownloadClient
|
||||
from core import RSSAnalyser, DownloadClient, FullSeasonGet
|
||||
from conf import settings, parse
|
||||
from utils import json_config
|
||||
|
||||
@@ -39,7 +39,8 @@ def get_log():
|
||||
|
||||
@app.get("/api/v1/resetRule")
|
||||
def reset_rule():
|
||||
data = {}
|
||||
data = json_config.load(settings.info_path)
|
||||
data["bangumi_info"] = []
|
||||
json_config.save(settings.info_path, data)
|
||||
return "Success"
|
||||
|
||||
@@ -59,41 +60,6 @@ def remove_rule(name: RuleName):
|
||||
return "Not matched"
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
rss_link: str
|
||||
host: str
|
||||
user_name: str
|
||||
password: str
|
||||
download_path: str
|
||||
method: str
|
||||
enable_group_tag: bool
|
||||
not_contain: str
|
||||
debug_mode: bool
|
||||
season_one_tag: bool
|
||||
remove_bad_torrent: bool
|
||||
enable_eps_complete: bool
|
||||
|
||||
|
||||
@app.post("/api/v1/config")
|
||||
async def config(config: Config):
|
||||
data = {
|
||||
"rss_link": config.rss_link,
|
||||
"host": config.host,
|
||||
"user_name": config.user_name,
|
||||
"password": config.password,
|
||||
"download_path": config.download_path,
|
||||
"method": config.method,
|
||||
"enable_group_tag": config.enable_group_tag,
|
||||
"not_contain": config.not_contain,
|
||||
"debug_mode": config.debug_mode,
|
||||
"season_one": config.season_one_tag,
|
||||
"remove_bad_torrent": config.remove_bad_torrent,
|
||||
"enable_eps_complete": config.enable_eps_complete
|
||||
}
|
||||
json_config.save("/config/config.json", data)
|
||||
return "received"
|
||||
|
||||
|
||||
class RSS(BaseModel):
|
||||
link: str
|
||||
|
||||
@@ -103,8 +69,7 @@ async def receive(link: RSS):
|
||||
client = DownloadClient()
|
||||
try:
|
||||
data = RSSAnalyser().rss_to_data(link.link)
|
||||
client.add_collection_feed(link.link, item_path=data["official_title"])
|
||||
client.set_rule(data, link.link)
|
||||
FullSeasonGet().download_collection(data, link.link, client)
|
||||
return data
|
||||
except Exception as e:
|
||||
logger.debug(e)
|
||||
@@ -129,7 +94,7 @@ class AddRule(BaseModel):
|
||||
|
||||
@app.post("/api/v1/addRule")
|
||||
async def add_rule(info: AddRule):
|
||||
return "success"
|
||||
return "Not complete"
|
||||
|
||||
|
||||
def run():
|
||||
@@ -142,6 +107,7 @@ def run():
|
||||
logger.debug("Please copy `const_dev.py` to `const_dev.py` to use custom settings")
|
||||
else:
|
||||
settings.init()
|
||||
LOGGING_CONFIG["formatters"]["default"]["fmt"] = "%(asctime)s %(levelprefix)s %(message)s"
|
||||
uvicorn.run(app, host="0.0.0.0", port=settings.webui_port)
|
||||
|
||||
|
||||
|
||||
@@ -58,14 +58,12 @@ def main_process(bangumi_data, download_client: DownloadClient):
|
||||
if settings.reset_folder:
|
||||
rename.set_folder()
|
||||
rss_analyser = RSSAnalyser()
|
||||
first_run = True
|
||||
while True:
|
||||
times = 0
|
||||
if settings.enable_rss_collector:
|
||||
rss_analyser.run(bangumi_data["bangumi_info"], download_client)
|
||||
if settings.eps_complete and first_run:
|
||||
if settings.eps_complete:
|
||||
FullSeasonGet().eps_complete(bangumi_data["bangumi_info"], download_client)
|
||||
first_run = False
|
||||
logger.info("Running....")
|
||||
save_data_file(bangumi_data)
|
||||
while times < settings.times:
|
||||
@@ -89,7 +87,6 @@ def run():
|
||||
# 初始化
|
||||
setup_logger()
|
||||
show_info()
|
||||
time.sleep(1)
|
||||
download_client = DownloadClient()
|
||||
download_client.init_downloader()
|
||||
if settings.rss_link is None:
|
||||
|
||||
@@ -7,19 +7,18 @@ from network import RequestContent
|
||||
from core import DownloadClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
SEARCH_KEY = ["group", "title_raw", "season_raw", "subtitle", "source", "dpi"]
|
||||
|
||||
|
||||
class FullSeasonGet:
|
||||
def __init__(self):
|
||||
self._get_rss = RequestContent()
|
||||
|
||||
def init_eps_complete_search_str(self, data: dict):
|
||||
search_str_pre = ""
|
||||
for i in [data['group'], data['title_raw'], data['season_raw'], data['subtitle'], data['source'], data['dpi']]:
|
||||
if i is not None:
|
||||
search_str_pre += f" {i}"
|
||||
search_str = re.sub(r"[\W_ ]", "+",
|
||||
search_str_pre.strip())
|
||||
@staticmethod
|
||||
def init_eps_complete_search_str(data: dict):
|
||||
test = [data.get(key).strip() for key in SEARCH_KEY if data.get(key) is not None]
|
||||
search_str_pre = "+".join(test)
|
||||
search_str = re.sub(r"[\W_ ]", "+", search_str_pre)
|
||||
return search_str
|
||||
|
||||
def get_season_torrents(self, data: dict):
|
||||
@@ -27,8 +26,8 @@ class FullSeasonGet:
|
||||
torrents = self._get_rss.get_torrents(f"https://mikanani.me/RSS/Search?searchstr={keyword}")
|
||||
return torrents
|
||||
|
||||
def collect_season_torrents(self, data: dict):
|
||||
torrents = self.get_season_torrents(data)
|
||||
@staticmethod
|
||||
def collect_season_torrents(data: dict, torrents):
|
||||
downloads = []
|
||||
for torrent in torrents:
|
||||
download_info = {
|
||||
@@ -41,15 +40,27 @@ class FullSeasonGet:
|
||||
downloads.append(download_info)
|
||||
return downloads
|
||||
|
||||
def download_eps(self, data, download_client: DownloadClient):
|
||||
logger.info(f"Start collecting {data['official_title']} Season {data['season']}...")
|
||||
torrents = self.get_season_torrents(data)
|
||||
downloads = self.collect_season_torrents(data, torrents)
|
||||
for download in downloads:
|
||||
download_client.add_torrent(download)
|
||||
logger.info("Completed!")
|
||||
data["eps_collect"] = False
|
||||
|
||||
def eps_complete(self, bangumi_info, download_client: DownloadClient):
|
||||
for data in bangumi_info:
|
||||
if data["eps_collect"]:
|
||||
logger.info(f"Start collecting past episodes of {data['official_title']} Season {data['season']}...")
|
||||
downloads = self.collect_season_torrents(data)
|
||||
for download in downloads:
|
||||
download_client.add_torrent(download)
|
||||
logger.info("Completed!")
|
||||
data["eps_collect"] = False
|
||||
self.download_eps(data, download_client)
|
||||
|
||||
def download_collection(self, data, link, download_client: DownloadClient):
|
||||
torrents = self._get_rss.get_torrents(link)
|
||||
downloads = self.collect_season_torrents(data, torrents)
|
||||
logger.info(f"Starting download {data.get('official_title')}")
|
||||
for download in downloads:
|
||||
download_client.add_torrent(download)
|
||||
logger.info("Completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -66,5 +77,4 @@ if __name__ == "__main__":
|
||||
"added": True,
|
||||
"eps_collect": True
|
||||
}
|
||||
torrents = a.collect_season_torrents(data)
|
||||
print(torrents)
|
||||
print(a.init_eps_complete_search_str(data))
|
||||
@@ -41,7 +41,6 @@ class RawParser:
|
||||
name_season = re.sub(".*新番.", "", season_info)
|
||||
else:
|
||||
name_season = re.sub(r"^[^]】]*[]】]", "", season_info).strip()
|
||||
|
||||
season_rule = r"S\d{1,2}|Season \d{1,2}|[第].[季期]"
|
||||
name_season = re.sub(r"[\[\]]", " ", name_season)
|
||||
seasons = re.findall(season_rule, name_season)
|
||||
@@ -105,35 +104,28 @@ class RawParser:
|
||||
if sub is None:
|
||||
return sub
|
||||
# TODO: 这里需要改成更精准的匹配,可能不止 _MP4 ?
|
||||
return sub.replace("_MP4", "")
|
||||
return re.sub(r"_MP4|_MKV", "", sub)
|
||||
|
||||
def process(self, raw_title: str):
|
||||
raw_title = raw_title.strip()
|
||||
|
||||
content_title = self.pre_process(raw_title) # 预处理标题
|
||||
group = self.get_group(content_title) # 翻译组的名字
|
||||
|
||||
match_obj = TITLE_RE.match(content_title) # 处理标题
|
||||
|
||||
season_info, episode_info, other = list(map(
|
||||
lambda x: x.strip(), match_obj.groups()
|
||||
))
|
||||
|
||||
raw_name, season_raw, season = self.season_process(season_info) # 处理 第n季
|
||||
name, name_group = "", ""
|
||||
try:
|
||||
name, name_group = self.name_process(raw_name) # 处理 名字
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 处理 集数
|
||||
raw_episode = EPISODE_RE.search(episode_info)
|
||||
episode = 0
|
||||
if raw_episode is not None:
|
||||
episode = int(raw_episode.group())
|
||||
|
||||
sub, dpi, source = self.find_tags(other) # 剩余信息处理
|
||||
|
||||
return name, season, season_raw, episode, sub, dpi, source, name_group, group
|
||||
|
||||
def analyse(self, raw):
|
||||
@@ -159,4 +151,4 @@ class RawParser:
|
||||
if __name__ == "__main__":
|
||||
test = RawParser()
|
||||
ep = test.analyse("[ANi] Classroom of the Elite S2 - 欢迎来到实力至上主义的教室 第二季 - 01 [1080P][Baha][WEB-DL][AAC AVC][CHT][MP4]")
|
||||
print(ep.title, ep.dpi)
|
||||
print(ep.title, ep.ep_info.number)
|
||||
|
||||
@@ -40,6 +40,7 @@ class DownloadParser:
|
||||
match_obj = re.match(rule, 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_dict.get("folder_name")
|
||||
new_name = re.sub(
|
||||
r"[\[\]]",
|
||||
"",
|
||||
@@ -82,6 +83,7 @@ class DownloadParser:
|
||||
)
|
||||
return new_name
|
||||
|
||||
@staticmethod
|
||||
def rename_none(self, info_dict):
|
||||
return info_dict["name"]
|
||||
|
||||
@@ -104,7 +106,7 @@ class DownloadParser:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
name = "[NC-Raws] 來自深淵 烈日的黃金鄉 - 01 (Baha 1920x1080 AVC AAC MP4) [89D4923F].mp4"
|
||||
name = "[Isekai Meikyuu de Harem wo][01][BIG5][1080P][AT-X].mp4"
|
||||
rename = DownloadParser()
|
||||
new_name = rename.download_rename(name, "Made abyess", 1, ".mp4", "pn")
|
||||
print(new_name)
|
||||
@@ -44,7 +44,7 @@ class TitleParser:
|
||||
"source": episode.source,
|
||||
"subtitle": episode.subtitle,
|
||||
"added": False,
|
||||
"eps_collect": True if settings.eps_complete else False,
|
||||
"eps_collect": True if settings.eps_complete and episode.ep_info.number > 1 else False,
|
||||
}
|
||||
logger.debug(f"RAW:{raw} >> {episode.title}")
|
||||
return data
|
||||
@@ -58,4 +58,5 @@ if __name__ == '__main__':
|
||||
raw = "[Lilith-Raws] 神渣☆偶像 / Kami Kuzu☆Idol - 01 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]"
|
||||
season = int(re.search(r"\d{1,2}", "S02").group())
|
||||
title = T.raw_parser(raw)
|
||||
print(season,title.title)
|
||||
print(season, title.title, title.ep_info.number)
|
||||
print(T.return_dict(raw))
|
||||
|
||||
Reference in New Issue
Block a user