🐛 Fix post get official recommended posts

This commit is contained in:
xtaodada 2024-07-18 22:40:42 +08:00
parent 6105dfbf3d
commit 7946b0b179
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 46 additions and 28 deletions

View File

@ -1,4 +1,5 @@
from typing import List import asyncio
from typing import List, Dict
from .hyperion import HyperionBase from .hyperion import HyperionBase
from ..base.hyperionrequest import HyperionRequest from ..base.hyperionrequest import HyperionRequest
@ -9,7 +10,7 @@ __all__ = ("Hoyolab",)
class Hoyolab(HyperionBase): class Hoyolab(HyperionBase):
POST_FULL_URL = "https://bbs-api-os.hoyolab.com/community/post/wapi/getPostFull" POST_FULL_URL = "https://bbs-api-os.hoyolab.com/community/post/wapi/getPostFull"
NEW_LIST_URL = "https://bbs-api-os.hoyolab.com/community/post/wapi/getNewsList" GET_NEW_LIST_URL = "https://bbs-api-os.hoyolab.com/community/post/wapi/getNewsList"
NEW_BG_URL = "https://bbs-api-os.hoyolab.com/community/painter/wapi/circle/info" NEW_BG_URL = "https://bbs-api-os.hoyolab.com/community/painter/wapi/circle/info"
LANG = "zh-cn" LANG = "zh-cn"
USER_AGENT = ( USER_AGENT = (
@ -30,17 +31,12 @@ class Hoyolab(HyperionBase):
async def get_official_recommended_posts( async def get_official_recommended_posts(
self, gids: int, page_size: int = 3, type_: int = 1 self, gids: int, page_size: int = 3, type_: int = 1
) -> List[PostRecommend]: ) -> List[PostRecommend]:
params = {"gids": gids, "page_size": page_size, "type": type_} results = []
response = await self.client.get(url=self.NEW_LIST_URL, params=params) tasks = [self.get_new_list_recommended_posts(gids, i, 5) for i in range(1, 4)]
return [ asyncio_results = await asyncio.gather(*tasks)
PostRecommend( for result in asyncio_results:
hoyolab=True, results.extend(result)
post_id=data["post"]["post_id"], return results
subject=data["post"]["subject"],
multi_language_info=HoYoPostMultiLang(**data["post"]["multi_language_info"]),
)
for data in response["list"]
]
async def get_post_info(self, gids: int, post_id: int, read: int = 1, scene: int = 1, lang: str = LANG) -> PostInfo: async def get_post_info(self, gids: int, post_id: int, read: int = 1, scene: int = 1, lang: str = LANG) -> PostInfo:
params = {"post_id": post_id, "read": read, "scene": scene} params = {"post_id": post_id, "read": read, "scene": scene}
@ -58,6 +54,15 @@ class Hoyolab(HyperionBase):
async def _download_image(self, art_id: int, url: str, page: int = 0) -> List[ArtworkImage]: async def _download_image(self, art_id: int, url: str, page: int = 0) -> List[ArtworkImage]:
return await self.download_image(self.client, art_id, url, page) return await self.download_image(self.client, art_id, url, page)
async def get_new_list(self, gids: int, type_id: int, page_size: int = 20) -> Dict:
params = {"gids": gids, "page_size": page_size, "type": type_id}
return await self.client.get(url=self.GET_NEW_LIST_URL, params=params)
async def get_new_list_recommended_posts(self, gids: int, type_id: int, page_size: int = 20) -> List[PostRecommend]:
resp = await self.get_new_list(gids, type_id, page_size)
data = resp["list"]
return [PostRecommend.parse(i) for i in data]
async def close(self): async def close(self):
await self.client.shutdown() await self.client.shutdown()

View File

@ -3,7 +3,7 @@ import os
import re import re
from abc import abstractmethod from abc import abstractmethod
from time import time from time import time
from typing import List, Tuple from typing import List, Tuple, Dict
from ..base.hyperionrequest import HyperionRequest from ..base.hyperionrequest import HyperionRequest
from ...models.genshin.hyperion import ( from ...models.genshin.hyperion import (
@ -106,6 +106,14 @@ class HyperionBase:
art_id=art_id, page=page, file_name=filename, file_extension=url.split(".")[-1], data=response.content art_id=art_id, page=page, file_name=filename, file_extension=url.split(".")[-1], data=response.content
) )
@abstractmethod
async def get_new_list(self, gids: int, type_id: int, page_size: int = 20) -> Dict:
"""获取最新帖子"""
@abstractmethod
async def get_new_list_recommended_posts(self, gids: int, type_id: int, page_size: int = 20) -> List[PostRecommend]:
"""获取最新帖子"""
@abstractmethod @abstractmethod
async def get_official_recommended_posts(self, gids: int) -> List[PostRecommend]: async def get_official_recommended_posts(self, gids: int) -> List[PostRecommend]:
"""获取官方推荐帖子""" """获取官方推荐帖子"""
@ -149,9 +157,12 @@ class Hyperion(HyperionBase):
return {"User-Agent": self.USER_AGENT, "Referer": referer} return {"User-Agent": self.USER_AGENT, "Referer": referer}
async def get_official_recommended_posts(self, gids: int) -> List[PostRecommend]: async def get_official_recommended_posts(self, gids: int) -> List[PostRecommend]:
params = {"gids": gids} results = []
response = await self.client.get(url=self.GET_OFFICIAL_RECOMMENDED_POSTS_URL, params=params) tasks = [self.get_new_list_recommended_posts(gids, i, 5) for i in range(1, 4)]
return [PostRecommend(**data) for data in response["list"]] asyncio_results = await asyncio.gather(*tasks)
for result in asyncio_results:
results.extend(result)
return results
async def get_post_full_in_collection(self, collection_id: int, gids: int = 2, order_type=1) -> JSON_DATA: async def get_post_full_in_collection(self, collection_id: int, gids: int = 2, order_type=1) -> JSON_DATA:
params = {"collection_id": collection_id, "gids": gids, "order_type": order_type} params = {"collection_id": collection_id, "gids": gids, "order_type": order_type}
@ -174,14 +185,14 @@ class Hyperion(HyperionBase):
async def _download_image(self, art_id: int, url: str, page: int = 0) -> List[ArtworkImage]: async def _download_image(self, art_id: int, url: str, page: int = 0) -> List[ArtworkImage]:
return await self.download_image(self.client, art_id, url, page) return await self.download_image(self.client, art_id, url, page)
async def get_new_list(self, gids: int, type_id: int, page_size: int = 20): async def get_new_list(self, gids: int, type_id: int, page_size: int = 20) -> Dict:
"""
?gids=2&page_size=20&type=3
:return:
"""
params = {"gids": gids, "page_size": page_size, "type": type_id} params = {"gids": gids, "page_size": page_size, "type": type_id}
response = await self.client.get(url=self.GET_NEW_LIST_URL, params=params) return await self.client.get(url=self.GET_NEW_LIST_URL, params=params)
return response
async def get_new_list_recommended_posts(self, gids: int, type_id: int, page_size: int = 20) -> List[PostRecommend]:
resp = await self.get_new_list(gids, type_id, page_size)
data = resp["list"]
return [PostRecommend.parse(i) for i in data]
async def get_live_info(self, act_id: str) -> LiveInfo: async def get_live_info(self, act_id: str) -> LiveInfo:
headers = {"x-rpc-act_id": act_id} headers = {"x-rpc-act_id": act_id}

View File

@ -200,7 +200,9 @@ class HoYoPostMultiLang(BaseModel):
class PostRecommend(BaseModel): class PostRecommend(BaseModel):
hoyolab: bool = False hoyolab: bool = False
post_id: int post_id: int
subject: str
banner: Optional[str] = None @staticmethod
official_type: Optional[int] = None def parse(data: Dict, hoyolab: bool = False):
multi_language_info: Optional[HoYoPostMultiLang] = None _post = data.get("post")
post_id = _post.get("post_id")
return PostRecommend(hoyolab=hoyolab, post_id=post_id)