From d5570f21dc540469dcf01a9aee8dbe2e4293ab3e Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Tue, 2 Jan 2024 18:54:10 +0800 Subject: [PATCH] fix: some part of async test change: optimize download_client.py --- backend/requirements-dev.txt | 3 +- backend/requirements.txt | 2 - .../module/downloader/client/qb_downloader.py | 52 +++++++++++++------ .../src/module/downloader/download_client.py | 4 +- .../src/module/parser/analyser/tmdb_parser.py | 9 ++-- backend/src/test/test_database.py | 3 +- backend/src/test/test_rss_engine.py | 10 ++-- backend/src/test/test_tmdb.py | 3 ++ 8 files changed, 56 insertions(+), 30 deletions(-) diff --git a/backend/requirements-dev.txt b/backend/requirements-dev.txt index a65cdcbc..94e89927 100644 --- a/backend/requirements-dev.txt +++ b/backend/requirements-dev.txt @@ -2,4 +2,5 @@ ruff black pre-commit -pytest \ No newline at end of file +pytest +pytest-asyncio \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index d36de2eb..d95595fd 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -7,8 +7,6 @@ fastapi==0.97.0 h11==0.14.0 idna==3.4 pydantic~=1.10 -PySocks==1.7.1 -qbittorrent-api==2023.9.53 httpx[http2,socks]==0.25.0 six==1.16.0 sniffio==1.3.0 diff --git a/backend/src/module/downloader/client/qb_downloader.py b/backend/src/module/downloader/client/qb_downloader.py index 789c7a6c..8822bae8 100644 --- a/backend/src/module/downloader/client/qb_downloader.py +++ b/backend/src/module/downloader/client/qb_downloader.py @@ -6,6 +6,21 @@ from ..exceptions import ConflictError, AuthorizationError logger = logging.getLogger(__name__) +QB_API_URL = { + "login": "/api/v2/auth/login", + "logout": "/api/v2/auth/logout", + "version": "/api/v2/app/version", + "setPreferences": "/api/v2/app/setPreferences", + "createCategory": "/api/v2/torrents/createCategory", + "info": "/api/v2/torrents/info", + "add": "/api/v2/torrents/add", + "delete": "/api/v2/torrents/delete", + "renameFile": "/api/v2/torrents/renameFile", + "setLocation": "/api/v2/torrents/setLocation", + "setCategory": "/api/v2/torrents/setCategory", + "addTags": "/api/v2/torrents/addTags", +} + class QbDownloader: def __init__(self, host: str, username: str, password: str, ssl: bool): @@ -16,33 +31,38 @@ class QbDownloader: async def auth(self): resp = await self._client.post( - url="/api/v2/auth/login", + url=QB_API_URL["login"], data={"username": self.username, "password": self.password}, timeout=5, ) return resp.text == "Ok." async def logout(self): - logout_api = "/api/v2/auth/logout" - await self._client.post(url=logout_api, timeout=5) + resp = await self._client.post( + url=QB_API_URL["logout"], + timeout=5 + ) + return resp.text async def check_host(self): try: await self._client.get( - url="/api/v2/app/version", + url=QB_API_URL["version"], timeout=5 ) return True - except httpx.RequestError: + except httpx.RequestError or httpx.TimeoutException: return False async def prefs_init(self, prefs): - prefs_api = "/api/v2/app/setPreferences" - await self._client.post(url=prefs_api, data=prefs) + await self._client.post( + url=QB_API_URL["setPreferences"], + data=prefs + ) async def add_category(self, category): await self._client.post( - url="/api/v2/torrents/createCategory", + url=QB_API_URL["createCategory"], data={"category": category}, timeout=5, ) @@ -54,7 +74,7 @@ class QbDownloader: "tag": tag, } torrent_info = await self._client.get( - url="/api/v2/torrents/info", + url=QB_API_URL["info"], params=data, ) return torrent_info.json() @@ -69,7 +89,7 @@ class QbDownloader: "use_auto_torrent_management": False, } resp = await self._client.post( - url="/api/v2/torrents/add", + url=QB_API_URL["add"], data=data, ) return resp.status_code == 200 @@ -80,7 +100,7 @@ class QbDownloader: "deleteFiles": True, } resp = await self._client.post( - url="/api/v2/torrents/delete", + url=QB_API_URL["delete"], data=data, ) return resp.status_code == 200 @@ -92,7 +112,7 @@ class QbDownloader: "newPath": new_path, } resp = await self._client.post( - url="/api/v2/torrents/renameFile", + url=QB_API_URL["renameFile"], data=data, ) return resp.status_code == 200 @@ -103,7 +123,7 @@ class QbDownloader: "location": new_location, } resp = await self._client.post( - url="/api/v2/torrents/setLocation", + url=QB_API_URL["setLocation"], data=data, ) return resp.status_code == 200 @@ -114,7 +134,7 @@ class QbDownloader: "hashes": _hash, } resp = await self._client.post( - url="/api/v2/torrents/setCategory", + url=QB_API_URL["setCategory"], data=data, ) return resp.status_code == 200 @@ -125,7 +145,7 @@ class QbDownloader: "tags": tag, } resp = await self._client.post( - url="/api/v2/torrents/addTags", + url=QB_API_URL["addTags"], data=data, ) return resp.status_code == 200 @@ -133,12 +153,14 @@ class QbDownloader: async def __aenter__(self): self._client = httpx.AsyncClient( base_url=self.host, + trust_env=self.ssl, ) while not await self.check_host(): logger.warning(f"[Downloader] Failed to connect to {self.host}, retry in 30 seconds.") await asyncio.sleep(30) if not await self.auth(): await self._client.aclose() + logger.error(f"[Downloader] Downloader authorize error. Please check your username/password.") raise AuthorizationError("Failed to login to qbittorrent.") return self diff --git a/backend/src/module/downloader/download_client.py b/backend/src/module/downloader/download_client.py index 52650396..830d9991 100644 --- a/backend/src/module/downloader/download_client.py +++ b/backend/src/module/downloader/download_client.py @@ -11,10 +11,8 @@ logger = logging.getLogger(__name__) def getClient(): # TODO 多下载器支持 - type = settings.downloader.type - if type == "qbittorrent": + if settings.downloader.type == "qbittorrent": from .client.qb_downloader import QbDownloader - return QbDownloader else: logger.error(f"[Downloader] Unsupported downloader type: {type}") diff --git a/backend/src/module/parser/analyser/tmdb_parser.py b/backend/src/module/parser/analyser/tmdb_parser.py index 1e41368b..e10ab527 100644 --- a/backend/src/module/parser/analyser/tmdb_parser.py +++ b/backend/src/module/parser/analyser/tmdb_parser.py @@ -33,8 +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"] - for type in type_id: + type_ids = await req.get_json(url_info) + for type in type_ids["genres"]: if type.get("id") == 16: return True return False @@ -58,7 +58,8 @@ 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") + json_contents = await req.get_json(url) + contents = json_contents.get("results") if contents.__len__() == 0: url = search_url(title.replace(" ", "")) contents = req.get_json(url).get("results") @@ -66,7 +67,7 @@ async def tmdb_parser(title, language, test: bool = False) -> TMDBInfo | None: if contents: for content in contents: id = content["id"] - if is_animation(id, language, req): + if await is_animation(id, language, req): break url_info = info_url(id, language) info_content = await req.get_json(url_info) diff --git a/backend/src/test/test_database.py b/backend/src/test/test_database.py index 5ee7ad93..8efa515a 100644 --- a/backend/src/test/test_database.py +++ b/backend/src/test/test_database.py @@ -46,7 +46,8 @@ def test_bangumi_database(): # match torrent result = db.bangumi.match_torrent( - "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]" + "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]", + "test", ) assert result.official_title == "无职转生,到了异世界就拿出真本事II" diff --git a/backend/src/test/test_rss_engine.py b/backend/src/test/test_rss_engine.py index ccd47462..549db9ee 100644 --- a/backend/src/test/test_rss_engine.py +++ b/backend/src/test/test_rss_engine.py @@ -1,18 +1,20 @@ +import pytest from module.rss.engine import RSSEngine from .test_database import engine as e +@pytest.mark.asyncio async def test_rss_engine(): with RSSEngine(e) as engine: rss_link = "https://mikanani.me/RSS/Bangumi?bangumiId=2353&subgroupid=552" - await engine.add_rss(rss_link, aggregate=False) + resp = await engine.add_rss(rss_link, aggregate=False) + assert resp.status result = engine.rss.search_active() - assert result[1].name == "Mikan Project - 无职转生~到了异世界就拿出真本事~" + assert result[0].name == "Mikan Project - 无职转生~到了异世界就拿出真本事~" - new_torrents = engine.pull_rss(result[1]) + new_torrents = await engine.pull_rss(result[1]) torrent = new_torrents[0] assert torrent.name == "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]" - diff --git a/backend/src/test/test_tmdb.py b/backend/src/test/test_tmdb.py index b8363a0c..90b694e9 100644 --- a/backend/src/test/test_tmdb.py +++ b/backend/src/test/test_tmdb.py @@ -1,6 +1,9 @@ +import pytest + from module.parser.analyser.tmdb_parser import tmdb_parser +@pytest.mark.asyncio async def test_tmdb_parser(): bangumi_title = "海盗战记" bangumi_year = "2019"