diff --git a/backend/src/module/api/rss.py b/backend/src/module/api/rss.py index d2de16fe..b356cc31 100644 --- a/backend/src/module/api/rss.py +++ b/backend/src/module/api/rss.py @@ -25,7 +25,7 @@ async def get_rss(): ) async def add_rss(rss: RSSItem): with RSSEngine() as engine: - result = engine.add_rss(rss.url, rss.name, rss.aggregate, rss.parser) + result = await engine.add_rss(rss.url, rss.name, rss.aggregate, rss.parser) return u_response(result) diff --git a/backend/src/module/core/sub_thread.py b/backend/src/module/core/sub_thread.py index 4968a2c2..83872479 100644 --- a/backend/src/module/core/sub_thread.py +++ b/backend/src/module/core/sub_thread.py @@ -1,5 +1,6 @@ import threading import time +import asyncio from module.conf import settings from module.downloader import DownloadClient @@ -18,18 +19,12 @@ class RSSThread(ProgramStatus): ) self.analyser = RSSAnalyser() + async def __loop_mission(self): + async with RSSEngine() as engine: + await engine.rss_checker(self.analyser, self.stop_event) + def rss_loop(self): - while not self.stop_event.is_set(): - with DownloadClient() as client, RSSEngine() as engine: - # Analyse RSS - rss_list = engine.rss.search_aggregate() - for rss in rss_list: - self.analyser.rss_to_data(rss, engine) - # Run RSS Engine - engine.refresh_rss(client) - if settings.bangumi_manage.eps_complete: - eps_complete() - self.stop_event.wait(settings.program.rss_time) + asyncio.run(self.__loop_mission()) def rss_start(self): self.rss_thread.start() diff --git a/backend/src/module/rss/engine.py b/backend/src/module/rss/engine.py index 5dc84ab5..653077bc 100644 --- a/backend/src/module/rss/engine.py +++ b/backend/src/module/rss/engine.py @@ -1,11 +1,13 @@ import logging +import asyncio import re -from typing import Optional +from typing import Optional, Callable from module.database import Database, engine from module.downloader import DownloadClient from module.models import Bangumi, ResponseModel, RSSItem, Torrent from module.network import RequestContent +from module.conf import settings logger = logging.getLogger(__name__) @@ -15,6 +17,23 @@ class RSSEngine(Database): super().__init__(_engine) self._to_refresh = False + async def rss_checker(self, callback: Callable[[list[Torrent]], None]): + torrent_pool = [] + torrent_name_pool = [] + while 1: + rss_items = self.rss.search_active() + if rss_items: + for item in rss_items: + torrents = await self.pull_rss(item) + for torrent in torrents: + if torrent.name not in torrent_name_pool: + torrent_pool.append(torrent) + torrent_name_pool.append(torrent.name) + if torrent_pool: + callback(torrent_pool) + torrent_pool.clear() + await asyncio.sleep(settings.rss.interval) + @staticmethod async def _get_torrents(rss: RSSItem) -> list[Torrent]: async with RequestContent() as req: @@ -94,8 +113,8 @@ class RSSEngine(Database): msg_zh="删除 RSS 成功。", ) - def pull_rss(self, rss_item: RSSItem) -> list[Torrent]: - torrents = self._get_torrents(rss_item) + async def pull_rss(self, rss_item: RSSItem) -> list[Torrent]: + torrents = await self._get_torrents(rss_item) new_torrents = self.torrent.check_new(torrents) return new_torrents @@ -110,22 +129,21 @@ class RSSEngine(Database): return matched return None - def refresh_rss(self, client: DownloadClient, rss_id: Optional[int] = None): + async def refresh_rss(self, client: DownloadClient, rss_id: Optional[int] = None): # Get All RSS Items if not rss_id: rss_items: list[RSSItem] = self.rss.search_active() else: - rss_item = self.rss.search_id(rss_id) - rss_items = [rss_item] if rss_item else [] + rss_items = [self.rss.search_id(rss_id)] # From RSS Items, get all torrents logger.debug(f"[Engine] Get {len(rss_items)} RSS items") for rss_item in rss_items: - new_torrents = self.pull_rss(rss_item) + new_torrents = await self.pull_rss(rss_item) # Get all enabled bangumi data for torrent in new_torrents: matched_data = self.match_torrent(torrent) if matched_data: - if client.add_torrent(torrent, matched_data): + if await client.add_torrent(torrent, matched_data): logger.debug(f"[Engine] Add torrent {torrent.name} to client") torrent.downloaded = True # Add all torrents to database diff --git a/backend/src/module/rss/pool.py b/backend/src/module/rss/pool.py new file mode 100644 index 00000000..b730e513 --- /dev/null +++ b/backend/src/module/rss/pool.py @@ -0,0 +1,24 @@ +import asyncio +from typing import Callable + +from module.models import RSSItem, Torrent +from module.network import RequestContent +from module.conf import settings + + +async def rss_checker(rss: list[RSSItem], callback: Callable[[list[Torrent]], None]): + torrent_pool = [] + torrent_name_pool = [] + while 1: + async with RequestContent() as req: + for item in rss: + torrents = await req.get_torrents(item.url) + for torrent in torrents: + if torrent.name not in torrent_name_pool: + torrent_pool.append(torrent) + torrent_name_pool.append(torrent.name) + if torrent_pool: + callback(torrent_pool) + torrent_pool.clear() + await asyncio.sleep(settings.rss.interval) + diff --git a/backend/src/module/update/cross_version.py b/backend/src/module/update/cross_version.py index 701241b2..42de3e36 100644 --- a/backend/src/module/update/cross_version.py +++ b/backend/src/module/update/cross_version.py @@ -7,8 +7,8 @@ from module.utils import save_image from module.network import RequestContent -def from_30_to_31(): - with RSSEngine() as db: +async def from_30_to_31(): + async with RSSEngine() as db: db.migrate() # Update poster link bangumis = db.bangumi.search_all() @@ -29,16 +29,16 @@ def from_30_to_31(): aggregate = True else: aggregate = False - db.add_rss(rss_link=rss, aggregate=aggregate) + await db.add_rss(rss_link=rss, aggregate=aggregate) -def cache_image(): - with RSSEngine() as db, RequestContent() as req: +async def cache_image(): + async with RSSEngine() as db, RequestContent() as req: bangumis = db.bangumi.search_all() for bangumi in bangumis: if bangumi.poster_link: # Hash local path - img = req.get_content(bangumi.poster_link) + img = await req.get_content(bangumi.poster_link) suffix = bangumi.poster_link.split(".")[-1] img_path = save_image(img, suffix) bangumi.poster_link = img_path