From 0a819d32bd2a9ce54a68af533f85b2c9cb884209 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Wed, 15 Mar 2023 20:39:14 +0800 Subject: [PATCH] Complete notification.py --- src/module/api.py | 1 + src/module/app.py | 2 +- src/module/models/bangumi.py | 19 ++++++++++++ src/module/network/notification.py | 40 ++++++++++++------------ src/module/network/request_contents.py | 10 ++++-- src/module/network/request_url.py | 24 +++++++++++++-- src/module/parser/fuzz_match.py | 42 -------------------------- src/module/parser/openai.py | 17 +++++++++++ src/module/parser/title_parser.py | 7 +++-- src/run.sh | 2 +- 10 files changed, 93 insertions(+), 71 deletions(-) delete mode 100644 src/module/parser/fuzz_match.py create mode 100644 src/module/parser/openai.py diff --git a/src/module/api.py b/src/module/api.py index 1d6fdb83..00cccbde 100644 --- a/src/module/api.py +++ b/src/module/api.py @@ -20,6 +20,7 @@ app.mount("/assets", StaticFiles(directory="templates/assets"), name="assets") templates = Jinja2Templates(directory="templates") +# HTML Response @app.get("/", response_class=HTMLResponse) def index(request: Request): context = {"request": request} diff --git a/src/module/app.py b/src/module/app.py index 6904f384..dd720977 100644 --- a/src/module/app.py +++ b/src/module/app.py @@ -71,7 +71,7 @@ def main_process(bangumi_data, download_client: DownloadClient): if settings.bangumi_manage.enable: rename.run() times += 1 - time.sleep(settings.program.sleep_time/settings.program.times) + time.sleep(settings.program.sleep_time / settings.program.times) def run(): diff --git a/src/module/models/bangumi.py b/src/module/models/bangumi.py index cb66fc45..f8b8d6b2 100644 --- a/src/module/models/bangumi.py +++ b/src/module/models/bangumi.py @@ -26,3 +26,22 @@ class Episode: group: str resolution: str source: str + + +@dataclass +class SeasonInfo(dict): + official_title: str + title_raw: str + season: int + season_raw: str + group: str + filter: list | None + offset: int | None + dpi: str + source: str + subtitle: str + added: bool + eps_collect: bool + + + diff --git a/src/module/network/notification.py b/src/module/network/notification.py index 50860b90..c6dca0ba 100644 --- a/src/module/network/notification.py +++ b/src/module/network/notification.py @@ -1,7 +1,6 @@ import logging -import requests - +from .request_contents import RequestContent from module.conf import settings @@ -12,11 +11,23 @@ class PostNotification: def __init__(self): self.token = settings.notification_token self.notification_url = lambda message: f"https://api.pushbullet.com/v2/{self.token}/{message}" + self.client = self.getClient() - def ifttt_post(self, message): - url = self.notification_url(message) - response = requests.get(url) - return response.status_code == 200 + @staticmethod + def getClient(): + if settings.notification.type.lower() == "telegram": + return TelegramNotification() + elif settings.notification.type.lower() == "serverchan": + return ServerChanNotification() + else: + return None + + def send_msg(self, title: str, desp: str) -> bool: + if not settings.notification.enable: + return False + if self.client is None: + return False + return self.client.send_msg(title, desp) class TelegramNotification: @@ -35,7 +46,6 @@ class TelegramNotification: class ServerChanNotification: """Server酱推送""" - def __init__(self): self.token = settings.notification.token self.notification_url = f"https://sctapi.ftqq.com/{self.token}.send" @@ -47,16 +57,6 @@ class ServerChanNotification: "title": title, "desp": desp, } - try: - resp = requests.post(self.notification_url, json=data, timeout=3) - resp.raise_for_status() - except requests.RequestException as e: - logging.error("[ServerChanNotification] send fail, error: %s" % e) - return False - return True - - -if __name__ == '__main__': - name = "勇者、辞职不干了" - notification = ServerChanNotification() - notification.send_msg(f"《{name[:10]}》缓存成功", f"[Auto Bangumi]《{name}》缓存成功") + with RequestContent() as req: + resp = req.post_data(self.notification_url, data) + return resp.status_code == 200 diff --git a/src/module/network/request_contents.py b/src/module/network/request_contents.py index 497c89c1..078d8dca 100644 --- a/src/module/network/request_contents.py +++ b/src/module/network/request_contents.py @@ -1,12 +1,12 @@ -from dataclasses import dataclass +import re +from dataclasses import dataclass from bs4 import BeautifulSoup from .request_url import RequestURL from module.conf import settings -import re FILTER = "|".join(settings.rss_parser.filter) @@ -41,3 +41,9 @@ class RequestContent(RequestURL): # API JSON def get_json(self, _url) -> dict: return self.get_url(_url).json() + + def post_json(self, _url, data: dict) -> dict: + return self.post_url(_url, data).json() + + def post_data(self, _url, data: dict) -> dict: + return self.post_url(_url, data) diff --git a/src/module/network/request_url.py b/src/module/network/request_url.py index 90be4c1a..44b53b64 100644 --- a/src/module/network/request_url.py +++ b/src/module/network/request_url.py @@ -18,8 +18,8 @@ class RequestURL: } def get_url(self, url): - times = 0 - while times < 5: + try_time = 0 + while try_time < 5: try: req = self.session.get(url=url, headers=self.header) req.raise_for_status() @@ -29,7 +29,25 @@ class RequestURL: logger.debug(e) logger.warning("ERROR with Connection.Please check DNS/Connection settings") time.sleep(5) - times += 1 + try_time += 1 + except Exception as e: + logger.debug(f"URL: {url}") + logger.debug(e) + break + + def post_url(self, url: str, data: dict): + try_time = 0 + while try_time < 5: + try: + req = self.session.post(url=url, headers=self.header, data=data) + req.raise_for_status() + return req + except requests.RequestException as e: + logger.debug(f"URL: {url}") + logger.debug(e) + logger.warning("ERROR with Connection.Please check DNS/Connection settings") + time.sleep(5) + try_time += 1 except Exception as e: logger.debug(f"URL: {url}") logger.debug(e) diff --git a/src/module/parser/fuzz_match.py b/src/module/parser/fuzz_match.py deleted file mode 100644 index 15b761d6..00000000 --- a/src/module/parser/fuzz_match.py +++ /dev/null @@ -1,42 +0,0 @@ -from thefuzz import fuzz -import logging -from module.utils import json_config -from module.conf import settings - -logger = logging.getLogger(__name__) - - -class FuzzMatch: - def __init__(self): - try: - anidb_data = json_config.get(settings.anidb_url) - json_config.save(settings.anidb_path, anidb_data) - except Exception as e: - logger.debug(e) - logger.info(f"Fail to get anidb data, reading local data") - anidb_data = json_config.load(settings.anidb_path) - self.match_data = anidb_data - - @staticmethod - def match(title_raw, info: dict): - compare_value = [] - for tag in ["main", "en", "ja", "zh-Hans", "zh-Hant"]: - if info[tag] is not None: - a = fuzz.token_sort_ratio(title_raw.lower(), info[tag].lower()) - compare_value.append(a) - for compare in info["other"]: - a = fuzz.token_sort_ratio(title_raw.lower(), compare.lower()) - compare_value.append(a) - return max(compare_value) - - def find_max_name(self, title_raw): - max_value = 0 - max_info = None - for info in self.match_data: - a = self.match(title_raw, info) - if a > max_value: - max_value = a - max_info = info - return max_value, max_info["main"] - # logger.debug(max(value)) - diff --git a/src/module/parser/openai.py b/src/module/parser/openai.py new file mode 100644 index 00000000..cd34c8b2 --- /dev/null +++ b/src/module/parser/openai.py @@ -0,0 +1,17 @@ +from module.network import RequestContent +from module.conf import settings + + +API_URL = "https://openai.estrella.cloud" + + +class OpenAIParser: + def __init__(self): + pass + + def parse(self, title: str) -> dict: + prompt = { + "prompt": "This is a test prompt", + } + with RequestContent() as req: + req.get_json(API_URL, params=prompt) diff --git a/src/module/parser/title_parser.py b/src/module/parser/title_parser.py index 0cfdaad3..72404619 100644 --- a/src/module/parser/title_parser.py +++ b/src/module/parser/title_parser.py @@ -3,6 +3,7 @@ import logging from .analyser import RawParser, DownloadParser, TMDBMatcher from module.conf import settings +from module.models import SeasonInfo logger = logging.getLogger(__name__) LANGUAGE = settings.rss_parser.language @@ -38,7 +39,7 @@ class TitleParser: official_title = official_title if official_title else title return official_title, tmdb_season - def return_dict(self, _raw: str): + def return_dict(self, _raw: str) -> dict: try: episode = self.raw_parser(_raw) title_search = episode.title_zh if episode.title_zh else episode.title_en @@ -59,8 +60,10 @@ class TitleParser: "subtitle": episode.sub, "added": False, "eps_collect": True if episode.episode > 1 else False, + "offset": 0, + "filter": settings.rss_parser.filter } logger.debug(f"RAW:{_raw} >> {episode.title_en}") return data except Exception as e: - logger.debug(e) \ No newline at end of file + logger.debug(e) diff --git a/src/run.sh b/src/run.sh index 39000cb9..bf18b90f 100755 --- a/src/run.sh +++ b/src/run.sh @@ -2,7 +2,7 @@ # Check old version if [ -f /config/bangumi.json ]; then - mv /config/bangumi.json /data/bangumi.json + mv /config/bangumi.json /app/data/bangumi.json fi