feat: big cover for iv

This commit is contained in:
xtaodada 2024-03-11 21:01:01 +08:00
parent 2dfbb66dd1
commit 458ef6a5f5
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
9 changed files with 139 additions and 1 deletions

11
refresh_data.py Normal file
View File

@ -0,0 +1,11 @@
from src.data.get_bg import save_bg
async def refresh_data():
await save_bg()
if __name__ == "__main__":
import asyncio
asyncio.run(refresh_data())

View File

@ -1,7 +1,7 @@
from typing import List
from .hyperionrequest import HyperionRequest
from .models import PostInfo, PostRecommend, HoYoPostMultiLang
from .models import PostInfo, PostRecommend, HoYoPostMultiLang, GameBgData
__all__ = ("Hoyolab",)
@ -9,6 +9,7 @@ __all__ = ("Hoyolab",)
class Hoyolab:
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"
NEW_BG_URL = "https://bbs-api-os.hoyolab.com/community/painter/wapi/circle/info"
LANG = "zh-cn"
USER_AGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
@ -50,6 +51,15 @@ class Hoyolab:
)
return PostInfo.paste_data(response, hoyolab=True)
async def get_news_bg(self) -> GameBgData:
params = {"with_channel": "1"}
headers = {
'x-rpc-app_version': '2.50.0',
'x-rpc-client_type': '4',
}
response = await self.client.get(url=self.NEW_BG_URL, params=params, headers=headers)
return GameBgData(**response)
async def close(self):
await self.client.shutdown()

View File

@ -19,6 +19,8 @@ __all__ = (
"HoYoPostMultiLang",
"PostInfo",
"PostRecommend",
"GameBgData",
"GameListItem",
)
@ -236,3 +238,16 @@ class PostRecommend(BaseModel):
banner: Optional[str] = None
official_type: Optional[int] = None
multi_language_info: Optional[HoYoPostMultiLang] = None
class GameListItem(BaseModel):
id: str
icon: str
bg: str
name: str
bg_color: str
focus_channel_id: str
class GameBgData(BaseModel):
game_list: List[GameListItem]

0
src/data/__init__.py Normal file
View File

52
src/data/bg.json Normal file
View File

@ -0,0 +1,52 @@
{
"game_list": [
{
"id": "2",
"icon": "https://fastcdn.hoyoverse.com/static-resource-v2/2023/11/08/9db76fb146f82c045bc276956f86e047_6878380451593228482.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/01/25/10c35a3445d4ef6d8f5639dcac3214b8_8146537302179438669.jpg",
"name": "原神",
"bg_color": "#442521",
"focus_channel_id": "30"
},
{
"id": "6",
"icon": "https://webstatic-sea.hoyolab.com/communityweb/business/starrail_hoyoverse.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/02/08/7377bd283ecff06d21a053730b7831b6_4844686270331272034.jpg",
"name": "崩坏:星穹铁道",
"bg_color": "#A675C4",
"focus_channel_id": "42"
},
{
"id": "8",
"icon": "https://webstatic-sea.hoyolab.com/communityweb/business/nap.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2023/11/03/c03d42301f986420af3addb3790e1081_6458510613402567619.jpg",
"name": "绝区零",
"bg_color": "#F5F6FB",
"focus_channel_id": "46"
},
{
"id": "1",
"icon": "https://fastcdn.hoyoverse.com/static-resource-v2/2024/02/29/3d96534fd7a35a725f7884e6137346d1_3942255444511793944.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/02/27/8eae2e2c0bf340ff60fbb5a1a0d3459f_9084111856013635148.jpg",
"name": "Honkai Impact 3rd",
"bg_color": "#1B3A7A",
"focus_channel_id": "0"
},
{
"id": "4",
"icon": "https://webstatic-sea.hoyolab.com/communityweb/business/nxx_hoyoverse.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2023/10/24/d97b3eb5763dba18bc137929ce53ba24_8118580296436267822.png",
"name": "未定事件簿",
"bg_color": "#2E3857",
"focus_channel_id": "0"
},
{
"id": "5",
"icon": "https://webstatic.hoyoverse.com/upload/static-resource/2022/08/04/8a31e3d6bce7684556cd45b1e1c309bf_1216320235452608527.png",
"bg": "https://fastcdn.hoyoverse.com/static-resource-v2/2023/08/01/f9d8c5df30c96d6d5c50aa10e201b171_429483834205307540.jpg",
"name": "HoYoLAB",
"bg_color": "#24479B",
"focus_channel_id": "0"
}
]
}

31
src/data/get_bg.py Normal file
View File

@ -0,0 +1,31 @@
import json
from typing import Dict
from pathlib import Path
from ..api.hoyolab import Hoyolab
from ..api.models import GameBgData
file_path = Path("src") / "data" / "bg.json"
BG_MAP: Dict[int, str] = {}
async def save_bg(save: bool = True):
async with Hoyolab() as hoyolab:
bg = await hoyolab.get_news_bg()
if save:
with open(file_path, "w", encoding="utf-8") as f:
f.write(json.dumps(bg.model_dump(), ensure_ascii=False, indent=4))
def get_bg():
if not file_path.exists():
return
with open(file_path, "r", encoding="utf-8") as f:
json_data = f.read()
data = GameBgData.model_validate_json(json_data)
for bg in data.game_list:
BG_MAP[int(bg.id)] = bg.bg
get_bg()

View File

@ -16,6 +16,7 @@ from src.api.models import (
get_images_params,
clean_url,
)
from src.data.get_bg import BG_MAP
from src.env import DOMAIN, MIYOUSHE
from src.error import ArticleNotFoundError
from src.log import logger
@ -138,6 +139,7 @@ def get_public_data(
"post": post_info,
"author": post_info["post"]["user"],
"related_posts": related_posts(post_info, i18n),
"GAME_BG": BG_MAP.get(post_info.game_id, ""),
"DOMAIN": DOMAIN,
"i18n": i18n,
}

View File

@ -42,6 +42,13 @@ Embed MiYouShe posts, videos, polls, and more on Telegram
If you can see this, your browser is doing something weird with your user agent.
<a href="{{ post.url }}">View original post</a>
</section>
{% if GAME_BG %}
<section class="is-imageBackgrounded">
<figure>
<img src="{{ GAME_BG }}"/>
</figure>
</section>
{% endif %}
<article>
<!-- article content -->
{{ article }}

10
tests/test_get_bg.py Normal file
View File

@ -0,0 +1,10 @@
import pytest
from src.data.get_bg import save_bg
@pytest.mark.asyncio
class TestGetBg:
@staticmethod
async def test_get_bg():
await save_bg(False)