From 95e70d21383873fa863f858b1823a9c2d5fef004 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Thu, 11 May 2023 14:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=A8=8B=E5=BA=8F=E8=87=AA?= =?UTF-8?q?=E6=A3=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/api/program.py | 20 +++++++++- src/module/core/__init__.py | 1 + src/module/core/check.py | 47 ++++++++++++++++++++++++ src/module/core/sub_thread.py | 24 +++++++----- src/module/downloader/download_client.py | 1 - src/module/network/request_contents.py | 3 +- 6 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 src/module/core/check.py diff --git a/src/module/api/program.py b/src/module/api/program.py index 8bbbabdb..097718f1 100644 --- a/src/module/api/program.py +++ b/src/module/api/program.py @@ -4,8 +4,7 @@ import logging from .download import router -from module.core import start_thread, start_program, stop_thread, stop_event - +from module.core import start_thread, start_program, stop_thread, stop_event, check_status, check_rss, check_downloader logger = logging.getLogger(__name__) @@ -54,3 +53,20 @@ async def shutdown_program(): logger.info("Shutting down program...") os.kill(os.getpid(), signal.SIGINT) return {"status": "ok"} + + +# Check status +@router.get("/api/v1/check/downloader", tags=["program"]) +async def check_downloader_status(): + return check_downloader() + + +@router.get("/api/v1/check/rss", tags=["program"]) +async def check_rss_status(): + return check_rss() + + +@router.get("/api/v1/check", tags=["program"]) +async def check_all(): + return check_status() + diff --git a/src/module/core/__init__.py b/src/module/core/__init__.py index 8ba882af..2bf029d5 100644 --- a/src/module/core/__init__.py +++ b/src/module/core/__init__.py @@ -1 +1,2 @@ from .sub_thread import * +from .check import check_status, check_rss, check_downloader \ No newline at end of file diff --git a/src/module/core/check.py b/src/module/core/check.py new file mode 100644 index 00000000..dd40c653 --- /dev/null +++ b/src/module/core/check.py @@ -0,0 +1,47 @@ +import logging + +from module.conf import settings +from module.downloader import DownloadClient +from module.network import RequestContent + +logger = logging.getLogger(__name__) + + +def check_status() -> bool: + if settings.rss_parser.token in ["", "token"]: + logger.warning("Please set RSS token") + return False + if check_rss(): + if check_downloader(): + logger.debug("All check passed") + return True + return False + + + +def check_downloader(): + with DownloadClient() as client: + if client.authed: + logger.debug("Downloader is running") + return True + else: + logger.warning("Can't connect to downloader") + return False + + +def check_rss(): + rss_link = settings.rss_link() + with RequestContent() as req: + try: + torrents = req.get_torrents(rss_link) + except Exception as e: + logger.warning("Failed to get torrents from RSS") + logger.warning(e) + return False + if not torrents: + logger.warning("No torrents in RSS") + logger.warning("Please check your RSS link") + return False + else: + logger.debug("RSS is running") + return True diff --git a/src/module/core/sub_thread.py b/src/module/core/sub_thread.py index b2e640b8..6c6a36f9 100644 --- a/src/module/core/sub_thread.py +++ b/src/module/core/sub_thread.py @@ -4,6 +4,7 @@ import logging import threading from .data_migration import data_migration +from .check import check_status from module.rss import RSSAnalyser, add_rules from module.manager import Renamer, FullSeasonGet @@ -99,13 +100,16 @@ def first_run(): async def start_program(): global rss_thread, rename_thread start_info() - # First init - first_run() - with BangumiDatabase() as database: - database.update_table() - rss_thread = threading.Thread(target=rss_loop, args=(stop_event,)) - rename_thread = threading.Thread(target=rename_loop, args=(stop_event,)) - if settings.rss_parser.enable: - rss_thread.start() - if settings.bangumi_manage.enable: - rename_thread.start() + if check_status(): + # First init + first_run() + with BangumiDatabase() as database: + database.update_table() + rss_thread = threading.Thread(target=rss_loop, args=(stop_event,)) + rename_thread = threading.Thread(target=rename_loop, args=(stop_event,)) + if settings.rss_parser.enable: + rss_thread.start() + if settings.bangumi_manage.enable: + rename_thread.start() + + diff --git a/src/module/downloader/download_client.py b/src/module/downloader/download_client.py index a18cc82a..491b9154 100644 --- a/src/module/downloader/download_client.py +++ b/src/module/downloader/download_client.py @@ -30,7 +30,6 @@ class DownloadClient: ssl = settings.downloader.ssl if type == "qbittorrent": from .client.qb_downloader import QbDownloader - return QbDownloader(host, username, password, ssl) else: raise Exception(f"Unsupported downloader type: {type}") diff --git a/src/module/network/request_contents.py b/src/module/network/request_contents.py index a6efa0f4..26369825 100644 --- a/src/module/network/request_contents.py +++ b/src/module/network/request_contents.py @@ -19,7 +19,8 @@ class RequestContent(RequestURL): def get_torrents( self, _url: str, - _filter: str = "|".join(settings.rss_parser.filter)) -> [TorrentInfo]: + _filter: str = "|".join(settings.rss_parser.filter) + ) -> [TorrentInfo]: soup = self.get_xml(_url) torrent_titles = [] torrent_urls = []