新增程序自检

This commit is contained in:
EstrellaXD
2023-05-11 14:37:07 +08:00
parent dfa4b2772a
commit 95e70d2138
6 changed files with 82 additions and 14 deletions

View File

@@ -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()

View File

@@ -1 +1,2 @@
from .sub_thread import *
from .check import check_status, check_rss, check_downloader

47
src/module/core/check.py Normal file
View File

@@ -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

View File

@@ -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()

View File

@@ -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}")

View File

@@ -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 = []