fix: some part of async test

change: optimize download_client.py
This commit is contained in:
EstrellaXD
2024-01-02 18:54:10 +08:00
parent 196ff2cefe
commit d5570f21dc
8 changed files with 56 additions and 30 deletions

View File

@@ -2,4 +2,5 @@
ruff
black
pre-commit
pytest
pytest
pytest-asyncio

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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