diff --git a/backend/src/module/downloader/client/tr_downloader.py b/backend/src/module/downloader/client/tr_downloader.py index e69de29b..29591525 100644 --- a/backend/src/module/downloader/client/tr_downloader.py +++ b/backend/src/module/downloader/client/tr_downloader.py @@ -0,0 +1,26 @@ +import httpx + + +class TrDownloader: + def __init__(self, host, username, password, ssl): + self.host = host + self.username = username + self.password = password + self.ssl = ssl + self._client = None + + async def __aenter__(self): + self._client = httpx.AsyncClient( + base_url=self.host, + auth=(self.username, self.password), + timeout=5, + ) + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb): + await self._client.aclose() + + async def auth(self): + resp = await self._client.get("/transmission/rpc") + resp.raise_for_status() + return resp diff --git a/backend/src/module/parser/analyser/tmdb_parser.py b/backend/src/module/parser/analyser/tmdb_parser.py index 3b930dbb..1e41368b 100644 --- a/backend/src/module/parser/analyser/tmdb_parser.py +++ b/backend/src/module/parser/analyser/tmdb_parser.py @@ -31,13 +31,12 @@ def info_url(e, key): return f"{TMDB_URL}/3/tv/{e}?api_key={TMDB_API}&language={LANGUAGE[key]}" -def is_animation(tv_id, language) -> bool: +async def is_animation(tv_id, language, req) -> bool: url_info = info_url(tv_id, language) - with RequestContent() as req: - type_id = req.get_json(url_info)["genres"] - for type in type_id: - if type.get("id") == 16: - return True + type_id = await req.get_json(url_info)["genres"] + for type in type_id: + if type.get("id") == 16: + return True return False @@ -56,10 +55,10 @@ def get_season(seasons: list) -> tuple[int, str]: return len(ss), ss[-1].get("poster_path") -def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: - with RequestContent() as req: +async def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: + async with RequestContent() as req: url = search_url(title) - contents = req.get_json(url).get("results") + contents = await req.get_json(url).get("results") if contents.__len__() == 0: url = search_url(title.replace(" ", "")) contents = req.get_json(url).get("results") @@ -67,10 +66,10 @@ def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: if contents: for content in contents: id = content["id"] - if is_animation(id, language): + if is_animation(id, language, req): break url_info = info_url(id, language) - info_content = req.get_json(url_info) + info_content = await req.get_json(url_info) season = [ { "season": s.get("name"), @@ -87,7 +86,7 @@ def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: year_number = info_content.get("first_air_date").split("-")[0] if poster_path: if not test: - img = req.get_content(f"https://image.tmdb.org/t/p/w780{poster_path}") + img = await req.get_content(f"https://image.tmdb.org/t/p/w780{poster_path}") poster_link = save_image(img, "jpg") else: poster_link = "https://image.tmdb.org/t/p/w780" + poster_path diff --git a/backend/src/module/rss/engine.py b/backend/src/module/rss/engine.py index 244a6ab5..5dc84ab5 100644 --- a/backend/src/module/rss/engine.py +++ b/backend/src/module/rss/engine.py @@ -16,9 +16,9 @@ class RSSEngine(Database): self._to_refresh = False @staticmethod - def _get_torrents(rss: RSSItem) -> list[Torrent]: - with RequestContent() as req: - torrents = req.get_torrents(rss.url) + async def _get_torrents(rss: RSSItem) -> list[Torrent]: + async with RequestContent() as req: + torrents = await req.get_torrents(rss.url) # Add RSS ID for torrent in torrents: torrent.rss_id = rss.id @@ -31,7 +31,7 @@ class RSSEngine(Database): else: return [] - def add_rss( + async def add_rss( self, rss_link: str, name: str | None = None, @@ -39,8 +39,8 @@ class RSSEngine(Database): parser: str = "mikan", ): if not name: - with RequestContent() as req: - name = req.get_rss_title(rss_link) + async with RequestContent() as req: + name = await req.get_rss_title(rss_link) if not name: return ResponseModel( status=False, @@ -131,14 +131,14 @@ class RSSEngine(Database): # Add all torrents to database self.torrent.add_all(new_torrents) - def download_bangumi(self, bangumi: Bangumi): - with RequestContent() as req: - torrents = req.get_torrents( + async def download_bangumi(self, bangumi: Bangumi): + async with RequestContent() as req: + torrents = await req.get_torrents( bangumi.rss_link, bangumi.filter.replace(",", "|") ) if torrents: - with DownloadClient() as client: - client.add_torrent(torrents, bangumi) + async with DownloadClient() as client: + await client.add_torrent(torrents, bangumi) self.torrent.add_all(torrents) return ResponseModel( status=True, diff --git a/backend/src/test/test_rss_engine.py b/backend/src/test/test_rss_engine.py index cda69f6e..ccd47462 100644 --- a/backend/src/test/test_rss_engine.py +++ b/backend/src/test/test_rss_engine.py @@ -3,11 +3,11 @@ from module.rss.engine import RSSEngine from .test_database import engine as e -def test_rss_engine(): +async def test_rss_engine(): with RSSEngine(e) as engine: rss_link = "https://mikanani.me/RSS/Bangumi?bangumiId=2353&subgroupid=552" - engine.add_rss(rss_link, aggregate=False) + await engine.add_rss(rss_link, aggregate=False) result = engine.rss.search_active() assert result[1].name == "Mikan Project - 无职转生~到了异世界就拿出真本事~" diff --git a/backend/src/test/test_tmdb.py b/backend/src/test/test_tmdb.py index 03724da4..b8363a0c 100644 --- a/backend/src/test/test_tmdb.py +++ b/backend/src/test/test_tmdb.py @@ -1,12 +1,12 @@ from module.parser.analyser.tmdb_parser import tmdb_parser -def test_tmdb_parser(): +async def test_tmdb_parser(): bangumi_title = "海盗战记" bangumi_year = "2019" bangumi_season = 2 - tmdb_info = tmdb_parser(bangumi_title, "zh", test=True) + tmdb_info = await tmdb_parser(bangumi_title, "zh", test=True) assert tmdb_info.title == "冰海战记" assert tmdb_info.year == bangumi_year