From 6f438be19ae5971eb0e8858300d118f1d9c34897 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Tue, 25 Apr 2023 14:46:45 +0800 Subject: [PATCH] Add RSS Link refresh --- src/main.py | 24 ++++++++++++++++++++++++ src/module/ab_decorator/__init__.py | 1 + src/module/app.py | 3 ++- src/module/conf/__init__.py | 6 +++++- src/module/conf/const.py | 17 +++++++---------- src/module/conf/log.py | 2 +- src/module/core/api_func.py | 12 ++++++++---- src/module/core/download_client.py | 11 +++++++++-- src/module/downloader/__init__.py | 10 ++++++++-- src/module/downloader/qb_downloader.py | 4 +++- src/module/models/config.py | 10 ++++------ src/module/rss/rss_analyser.py | 3 ++- src/module/setID.sh | 10 ---------- 13 files changed, 74 insertions(+), 39 deletions(-) delete mode 100644 src/module/setID.sh diff --git a/src/main.py b/src/main.py index b5e1971f..3caa7a1c 100644 --- a/src/main.py +++ b/src/main.py @@ -22,6 +22,8 @@ main_process = multiprocessing.Process(target=app.run) @router.get("/api/v1/restart", tags=["program"]) async def restart(): global main_process + if not main_process.is_alive(): + return {"status": "failed", "reason": "Already stopped"} logger.info("Restarting...") os.kill(main_process.pid, signal.SIGTERM) main_process = multiprocessing.Process(target=app.run) @@ -30,6 +32,28 @@ async def restart(): return {"status": "success"} +@router.get("/api/v1/stop", tags=["program"]) +async def stop(): + global main_process + if not main_process.is_alive(): + return {"status": "failed", "reason": "Already stopped"} + logger.info("Stopping...") + os.kill(main_process.pid, signal.SIGTERM) + logger.info("Stopped") + return {"status": "success"} + + +@router.get("/api/v1/start", tags=["program"]) +async def start(): + global main_process + if main_process.is_alive(): + return {"status": "failed", "reason": "Already started"} + logger.info("Starting...") + main_process.start() + logger.info("Started") + return {"status": "success"} + + if VERSION != "DEV_VERSION": router.mount("/assets", StaticFiles(directory="templates/assets"), name="assets") templates = Jinja2Templates(directory="templates") diff --git a/src/module/ab_decorator/__init__.py b/src/module/ab_decorator/__init__.py index d504865e..78a18e93 100644 --- a/src/module/ab_decorator/__init__.py +++ b/src/module/ab_decorator/__init__.py @@ -12,6 +12,7 @@ def qb_connect_failed_wait(func): return func(*args, **kwargs) except Exception as e: logger.debug(f"URL: {args[0]}") + logger.warning(e) logger.warning("Cannot connect to qBittorrent. Wait 5 min and retry...") time.sleep(300) times += 1 diff --git a/src/module/app.py b/src/module/app.py index aced1330..9e731ac8 100644 --- a/src/module/app.py +++ b/src/module/app.py @@ -77,8 +77,9 @@ def run(): setup_logger() show_info() download_client = DownloadClient() + download_client.auth() download_client.init_downloader() - if settings.rss_parser.token is None: + if settings.rss_parser.token in ["", "token", None]: logger.error("Please set your RSS token in config file.") quit() download_client.rss_feed() diff --git a/src/module/conf/__init__.py b/src/module/conf/__init__.py index 1bec68d2..c716bbd8 100644 --- a/src/module/conf/__init__.py +++ b/src/module/conf/__init__.py @@ -4,4 +4,8 @@ from .config import settings, VERSION TMDB_API = "32b19d6a05b512190a056fa4e747cbbc" DATA_PATH = "data/data.json" -RSS_LINK = f"https://{settings.rss_parser.custom_url}/RSS/MyBangumi?token={settings.rss_parser.token}" + + +class RSSLink(str): + def __new__(cls): + return f"https://{settings.rss_parser.custom_url}/RSS/MyBangumi?token={settings.rss_parser.token}" \ No newline at end of file diff --git a/src/module/conf/const.py b/src/module/conf/const.py index 175a1e4b..bfe6c3cf 100644 --- a/src/module/conf/const.py +++ b/src/module/conf/const.py @@ -31,11 +31,8 @@ DEFAULT_SETTINGS = { "group_tag": False, "remove_bad_torrent": False }, - "debug": { - "enable": False, - "level": "info", - "file": "bangumi.log", - "dev_debug": False + "log": { + "debug_enable": False, }, "proxy": { "enable": False, @@ -56,9 +53,9 @@ DEFAULT_SETTINGS = { ENV_TO_ATTR = { "program": { - "AB_INTERVAL_TIME": ("sleep_time", int), - "AB_RENAME_FREQ": ("times", int), - "AB_WEBUI_PORT": ("webui_port", int), + "AB_INTERVAL_TIME": ("sleep_time", lambda e: int(e)), + "AB_RENAME_FREQ": ("times", lambda e: int(e)), + "AB_WEBUI_PORT": ("webui_port", lambda e: int(e)), }, "downloader": { "AB_DOWNLOADER_HOST": "host", @@ -80,8 +77,8 @@ ENV_TO_ATTR = { "AB_EP_COMPLETE": ("eps_complete", lambda e: e.lower() in ("true", "1", "t")), "AB_REMOVE_BAD_BT": ("remove_bad_torrent", lambda e: e.lower() in ("true", "1", "t")), }, - "debug": { - "AB_DEBUG_MODE": ("enable", lambda e: e.lower() in ("true", "1", "t")), + "log": { + "AB_DEBUG_MODE": ("debug_enable", lambda e: e.lower() in ("true", "1", "t")), }, } diff --git a/src/module/conf/log.py b/src/module/conf/log.py index b8516603..475b475c 100644 --- a/src/module/conf/log.py +++ b/src/module/conf/log.py @@ -6,7 +6,7 @@ LOG_PATH = "data/log.txt" def setup_logger(): - level = logging.DEBUG if settings.debug.enable else logging.INFO + level = logging.DEBUG if settings.log.debug_enable else logging.INFO logging.addLevelName(logging.DEBUG, 'DEBUG:') logging.addLevelName(logging.INFO, 'INFO:') logging.addLevelName(logging.WARNING, 'WARNING:') diff --git a/src/module/core/api_func.py b/src/module/core/api_func.py index 449faf4a..fae3b2d4 100644 --- a/src/module/core/api_func.py +++ b/src/module/core/api_func.py @@ -17,7 +17,7 @@ logger = logging.getLogger(__name__) class APIProcess: def __init__(self): self._rss_analyser = RSSAnalyser() - self._download_client = DownloadClient() + self._client = DownloadClient() self._full_season_get = FullSeasonGet() def link_process(self, link): @@ -25,15 +25,19 @@ class APIProcess: @api_failed def download_collection(self, link): + if not self._client.authed: + self._client.auth() data = self.link_process(link) - self._full_season_get.download_collection(data, link, self._download_client) + self._full_season_get.download_collection(data, link, self._client) return data @api_failed def add_subscribe(self, link): + if not self._client.authed: + self._client.auth() data = self.link_process(link) - self._download_client.add_rss_feed(link, data.get("official_title")) - self._download_client.set_rule(data, link) + self._client.add_rss_feed(link, data.get("official_title")) + self._client.set_rule(data, link) return data @staticmethod diff --git a/src/module/core/download_client.py b/src/module/core/download_client.py index 236f2cc9..f103e6ef 100644 --- a/src/module/core/download_client.py +++ b/src/module/core/download_client.py @@ -4,7 +4,9 @@ import os from module.downloader import getClient -from module.conf import settings, RSS_LINK +from module.conf import settings, RSSLink + +RSS_LINK = RSSLink() logger = logging.getLogger(__name__) @@ -12,10 +14,15 @@ logger = logging.getLogger(__name__) class DownloadClient: def __init__(self): self.client = getClient() + self.authed = False def auth(self): host, username, password = settings.downloader.host, settings.downloader.username, settings.downloader.password - self.client.auth(host, username, password) + try: + self.client.auth(host, username, password) + self.authed = True + except Exception as e: + logger.error(f"Can't login {host} by {username}, {e}") def init_downloader(self): prefs = { diff --git a/src/module/downloader/__init__.py b/src/module/downloader/__init__.py index 527d6a40..a7612e6c 100644 --- a/src/module/downloader/__init__.py +++ b/src/module/downloader/__init__.py @@ -1,5 +1,11 @@ +from module.conf import settings + + def getClient(): # TODO 多下载器支持 # 从 settings 里读取下载器名称,然后返回对应 Client - from .qb_downloader import QbDownloader - return QbDownloader() + if settings.downloader.type == "qbittorrent": + from .qb_downloader import QbDownloader + return QbDownloader() + else: + raise Exception(f"Unsupported downloader type: {settings.downloader.type}") diff --git a/src/module/downloader/qb_downloader.py b/src/module/downloader/qb_downloader.py index 64e8b643..568ee107 100644 --- a/src/module/downloader/qb_downloader.py +++ b/src/module/downloader/qb_downloader.py @@ -13,7 +13,6 @@ logger = logging.getLogger(__name__) class QbDownloader: - @qb_connect_failed_wait def __init__(self): self._client: Client | None = None @@ -101,3 +100,6 @@ class QbDownloader: def set_category(self, _hash, category): self._client.torrents_set_category(category, hashes=_hash) + + def check_connection(self): + return self._client.app_version() diff --git a/src/module/models/config.py b/src/module/models/config.py index b2d0a62b..9f341df4 100644 --- a/src/module/models/config.py +++ b/src/module/models/config.py @@ -17,6 +17,7 @@ class Downloader(BaseModel): path: str = Field("/downloads/Bangumi", description="Downloader path") ssl: bool = Field(False, description="Downloader ssl") + class RSSParser(BaseModel): enable: bool = Field(True, description="Enable RSS parser") type: str = Field("mikan", description="RSS parser type") @@ -35,11 +36,8 @@ class BangumiManage(BaseModel): remove_bad_torrent: bool = Field(False, description="Remove bad torrent") -class Debug(BaseModel): - enable: bool = Field(False, description="Enable debug") - level: str = Field("debug", description="Debug level") - file: str = Field("debug.log", description="Debug file") - dev_debug: bool = Field(False, description="Enable dev debug") +class Log(BaseModel): + debug_enable: bool = Field(False, description="Enable debug") class Proxy(BaseModel): @@ -64,6 +62,6 @@ class Config(BaseModel): downloader: Downloader = Downloader() rss_parser: RSSParser = RSSParser() bangumi_manage: BangumiManage = BangumiManage() - debug: Debug = Debug() + log: Log = Log() proxy: Proxy = Proxy() notification: Notification = Notification() diff --git a/src/module/rss/rss_analyser.py b/src/module/rss/rss_analyser.py index 57704a95..b833b7b1 100644 --- a/src/module/rss/rss_analyser.py +++ b/src/module/rss/rss_analyser.py @@ -3,11 +3,12 @@ import logging from module.network import RequestContent from module.parser import TitleParser -from module.conf import settings, RSS_LINK +from module.conf import RSSLink from module.core import DownloadClient logger = logging.getLogger(__name__) +RSS_LINK = RSSLink() class RSSAnalyser: diff --git a/src/module/setID.sh b/src/module/setID.sh deleted file mode 100644 index 64b28214..00000000 --- a/src/module/setID.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -echo "设置文件夹权限" -echo "PUID=${PUID}" -echo "PGID=${PGID}" - -groupmod -o -g "$PGID" auto_bangumi -usermod -o -u "$PUID" auto_bangumi - -chown -R auto_bangumi:auto_bangumi /src /templates /config \ No newline at end of file