Skip to content

Commit

Permalink
fix: some part of async test
Browse files Browse the repository at this point in the history
change: optimize download_client.py
  • Loading branch information
EstrellaXD committed Jan 2, 2024
1 parent 196ff2c commit d5570f2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 30 deletions.
3 changes: 2 additions & 1 deletion backend/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
ruff
black
pre-commit
pytest
pytest
pytest-asyncio
2 changes: 0 additions & 2 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 37 additions & 15 deletions backend/src/module/downloader/client/qb_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

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):
Expand All @@ -16,33 +31,38 @@ def __init__(self, host: str, username: str, password: str, ssl: bool):

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,
)
Expand All @@ -54,7 +74,7 @@ async def torrents_info(self, status_filter, category, tag=None):
"tag": tag,
}
torrent_info = await self._client.get(
url="/api/v2/torrents/info",
url=QB_API_URL["info"],
params=data,
)
return torrent_info.json()
Expand All @@ -69,7 +89,7 @@ async def add(self, torrent_urls, torrent_files, save_path, category):
"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
Expand All @@ -80,7 +100,7 @@ async def delete(self, _hash):
"deleteFiles": True,
}
resp = await self._client.post(
url="/api/v2/torrents/delete",
url=QB_API_URL["delete"],
data=data,
)
return resp.status_code == 200
Expand All @@ -92,7 +112,7 @@ async def rename(self, torrent_hash, old_path, new_path) -> bool:
"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
Expand All @@ -103,7 +123,7 @@ async def move(self, hashes, new_location):
"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
Expand All @@ -114,7 +134,7 @@ async def set_category(self, _hash, category):
"hashes": _hash,
}
resp = await self._client.post(
url="/api/v2/torrents/setCategory",
url=QB_API_URL["setCategory"],
data=data,
)
return resp.status_code == 200
Expand All @@ -125,20 +145,22 @@ async def add_tag(self, _hash, tag):
"tags": tag,
}
resp = await self._client.post(
url="/api/v2/torrents/addTags",
url=QB_API_URL["addTags"],
data=data,
)
return resp.status_code == 200

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

Expand Down
4 changes: 1 addition & 3 deletions backend/src/module/downloader/download_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

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}")
Expand Down
9 changes: 5 additions & 4 deletions backend/src/module/parser/analyser/tmdb_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,15 +58,16 @@ 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")
# 判断动画
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)
Expand Down
3 changes: 2 additions & 1 deletion backend/src/test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 6 additions & 4 deletions backend/src/test/test_rss_engine.py
Original file line number Diff line number Diff line change
@@ -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]"

3 changes: 3 additions & 0 deletions backend/src/test/test_tmdb.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit d5570f2

Please sign in to comment.