Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
asadahimeka authored Mar 9, 2024
1 parent 6c495c8 commit d20f99a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
50 changes: 38 additions & 12 deletions hibiapi/api/pixiv/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import re


@enum_auto_doc
class IllustType(str, Enum):
"""画作类型"""
Expand Down Expand Up @@ -134,7 +135,11 @@ def _parse_accept_language(accept_language: str) -> str:
@dont_route
@catch_network_error
async def request(
self, endpoint: str, *, params: Optional[Dict[str, Any]] = None, return_text: bool = False
self,
endpoint: str,
*,
params: Optional[Dict[str, Any]] = None,
return_text: bool = False,
) -> Dict[str, Any]:
headers = self.client.headers.copy()

Expand Down Expand Up @@ -265,7 +270,7 @@ async def search(
page: int = 1,
size: int = 30,
include_translated_tag_results: bool = True,
search_ai_type: int = None, # 0: 过滤 AI 生成作品, 1: 反之
search_ai_type: int = None, # 0: 过滤 AI 生成作品, 1: 反之
):
return await self.request(
"v1/search/illust",
Expand Down Expand Up @@ -476,7 +481,7 @@ async def novel_detail(self, *, id: int):
async def novel_text(self, *, id: int):
# return await self.request("/v1/novel/text", params={"novel_id": id})
response = await self.webview_novel(id=id, raw=False)
return { "novel_text": response["text"] or "" }
return {"novel_text": response["text"] or ""}

# 获取小说 HTML 后解析 JSON
async def webview_novel(self, *, id: int, raw: bool = False):
Expand All @@ -489,12 +494,15 @@ async def webview_novel(self, *, id: int, raw: bool = False):
return_text=True,
)
if raw:
return response # 直接返回 HTML,但是返回头 content-type 还是 application/json,可能需要修改为 text/html
# 直接返回 HTML,但是返回头 content-type 还是 application/json
# 可能需要修改为 text/html
return response

Check warning on line 499 in hibiapi/api/pixiv/api.py

View check run for this annotation

Codecov / codecov/patch

hibiapi/api/pixiv/api.py#L499

Added line #L499 was not covered by tests

try:
novel_json = re.search(r"novel:\s({.+}),\s+isOwnWork", response).groups()[0].encode()
return json.loads(novel_json)
novel_json = re.search(r"novel:\s({.+}),\s+isOwnWork", response).groups()[0].encode()
return json.loads(novel_json)
except Exception as e:
return { "error": "Parsing novel error: %s" % e }
return {"error": "Parsing novel error: %s" % e}

Check warning on line 505 in hibiapi/api/pixiv/api.py

View check run for this annotation

Codecov / codecov/patch

hibiapi/api/pixiv/api.py#L504-L505

Added lines #L504 - L505 were not covered by tests

@cache_config(ttl=timedelta(hours=12))
async def tags_novel(self):
Expand All @@ -511,7 +519,7 @@ async def search_novel(
duration: Optional[SearchDurationType] = None,
page: int = 1,
size: int = 30,
search_ai_type: int = None, # 0: 过滤 AI 生成作品, 1: 反之
search_ai_type: int = None, # 0: 过滤 AI 生成作品, 1: 反之
):
return await self.request(
"/v1/search/novel",
Expand Down Expand Up @@ -555,7 +563,7 @@ async def novel_new(self, *, max_novel_id: Optional[int] = None):

# 人气直播列表
async def live_list(self, *, page: int = 1, size: int = 30):
params = { "list_type": "popular" }
params = {"list_type": "popular"}
if page > 1:
params["offset"] = (page - 1) * size

Check warning on line 568 in hibiapi/api/pixiv/api.py

View check run for this annotation

Codecov / codecov/patch

hibiapi/api/pixiv/api.py#L568

Added line #L568 was not covered by tests
return await self.request("v1/live/list", params=params)
Expand All @@ -576,12 +584,30 @@ async def related_member(self, *, id: int):

# 漫画系列
async def illust_series(self, *, id: int, page: int = 1, size: int = 30):
return await self.request("v1/illust/series", params={"illust_series_id": id, "offset": (page - 1) * size})
return await self.request(
"v1/illust/series",
params={
"illust_series_id": id,
"offset": (page - 1) * size
},
)

# 用户的漫画系列
async def member_illust_series(self, *, id: int, page: int = 1, size: int = 30):
return await self.request("v1/user/illust-series", params={"user_id": id, "offset": (page - 1) * size})
return await self.request(
"v1/user/illust-series",
params={
"user_id": id,
"offset": (page - 1) * size
},
)

# 用户的小说系列
async def member_novel_series(self, *, id: int, page: int = 1, size: int = 30):
return await self.request("v1/user/novel-series", params={"user_id": id, "offset": (page - 1) * size})
return await self.request(
"v1/user/novel-series",
params={
"user_id": id,
"offset": (page - 1) * size
}
)
8 changes: 8 additions & 0 deletions test/test_pixiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,41 +178,49 @@ def test_novel_text(client: TestClient):
assert response.status_code == 200
assert response.json().get("novel_text")


def test_webview_novel(client: TestClient):
response = client.get("webview_novel", params={"id": 19791013})
assert response.status_code == 200
assert response.json().get("text")


def test_live_list(client: TestClient):
response = client.get("live_list")
assert response.status_code == 200
assert response.json().get("lives")


def test_related_novel(client: TestClient):
response = client.get("related_novel", params={"id": 19791013})
assert response.status_code == 200
assert response.json().get("novels")


def test_related_member(client: TestClient):
response = client.get("related_member", params={"id": 10109777})
assert response.status_code == 200
assert response.json().get("user_previews")


def test_illust_series(client: TestClient):
response = client.get("illust_series", params={"id": 218893})
assert response.status_code == 200
assert response.json().get("illust_series_detail")


def test_member_illust_series(client: TestClient):
response = client.get("member_illust_series", params={"id": 4087934})
assert response.status_code == 200
assert response.json().get("illust_series_details")


def test_member_novel_series(client: TestClient):
response = client.get("member_novel_series", params={"id": 86832559})
assert response.status_code == 200
assert response.json().get("novel_series_details")


def test_tags_novel(client: TestClient):
response = client.get("tags_novel")
assert response.status_code == 200
Expand Down

0 comments on commit d20f99a

Please sign in to comment.