diff --git a/backend/src/module/core/aiocore.py b/backend/src/module/core/aiocore.py new file mode 100644 index 00000000..25e437d9 --- /dev/null +++ b/backend/src/module/core/aiocore.py @@ -0,0 +1,71 @@ +import asyncio + +from module.downloader import DownloadClient +from module.manager import Renamer +from module.conf import settings +from module.rss import RSSEngine +from module.database import Database +from module.models import Bangumi, RSSItem, Torrent + + +rss_item_pool = [] +torrent_pool: list[tuple[Bangumi, list[Torrent]]] = [] + + +class AsyncProgram: + def __init__(self): + self.renamer = Renamer() + + async def check_downloader(self, client: DownloadClient): + while 1: + connected = await client.auth() + if not connected: + await asyncio.sleep(30) + else: + break + + async def rename_task(self): + while 1: + async with DownloadClient() as client: + await self.check_downloader(client) + self.renamer.rename(client) + await asyncio.sleep(settings.program.rename_time) + + async def rss_task(self, engine: RSSEngine): + while True: + for rss_item in rss_item_pool: + torrents = engine.get_rss_torrents(rss_item.id) + if torrents: + torrent_pool.append((rss_item, torrents)) + await asyncio.sleep(settings.program.rss_time) + + async def main_tasks(self): + async with DownloadClient() as client: + await self.check_downloader(client) + await asyncio.gather( + self.rename_task(client), + self.rss_task(engine, client) + ) + + +async def rename_task(): + connected = False + renamer = Renamer() + async with DownloadClient() as client: + while not connected: + connected = await client.auth() + if not connected: + await asyncio.sleep(30) + for bangumi, torrents in torrent_pool: + client.add_torrent(torrents, bangumi) + renamer.rename(client) + await asyncio.sleep(settings.program.rename_time) + + +async def rss_task(): + # GET RSS FROM DATABASE + with Database() as db: + rss_items = db.rss.search_active() + for rss_item in rss_items: + rss_item_pool.append(rss_item) + pass \ No newline at end of file diff --git a/backend/src/module/parser/analyser/tmdb_parser.py b/backend/src/module/parser/analyser/tmdb_parser.py index 1e41368b..4f09fc6f 100644 --- a/backend/src/module/parser/analyser/tmdb_parser.py +++ b/backend/src/module/parser/analyser/tmdb_parser.py @@ -33,7 +33,8 @@ def info_url(e, key): async def is_animation(tv_id, language, req) -> bool: url_info = info_url(tv_id, language) - type_id = await req.get_json(url_info)["genres"] + type_id = await req.get_json(url_info) + type_id = type_id.get("genres") for type in type_id: if type.get("id") == 16: return True @@ -58,17 +59,18 @@ def get_season(seasons: list) -> tuple[int, str]: async def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: async with RequestContent() as req: url = search_url(title) - contents = await req.get_json(url).get("results") + contents = await req.get_json(url) + contents = contents.get("results") if contents.__len__() == 0: url = search_url(title.replace(" ", "")) contents = req.get_json(url).get("results") # 判断动画 if contents: for content in contents: - id = content["id"] - if is_animation(id, language, req): + _id = content["id"] + if await is_animation(_id, language, req): break - url_info = info_url(id, language) + url_info = info_url(_id, language) info_content = await req.get_json(url_info) season = [ { @@ -93,7 +95,7 @@ async def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: else: poster_link = None return TMDBInfo( - id, + _id, official_title, original_title, season, @@ -106,4 +108,12 @@ async def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: if __name__ == "__main__": - print(tmdb_parser("魔法禁书目录", "zh")) + import asyncio + + + async def parse(title, language): + info = await tmdb_parser(title, language) + for key, value in info.__dict__.items(): + print(key, value) + + asyncio.run(parse("葬送的芙莉莲", "jp"))