diff --git a/refresh_data.py b/refresh_data.py new file mode 100644 index 0000000..6e21e69 --- /dev/null +++ b/refresh_data.py @@ -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()) diff --git a/src/api/hoyolab.py b/src/api/hoyolab.py index 3f8e982..07761ea 100644 --- a/src/api/hoyolab.py +++ b/src/api/hoyolab.py @@ -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() diff --git a/src/api/models.py b/src/api/models.py index 2e1df91..ee52a9d 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -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] diff --git a/src/data/__init__.py b/src/data/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/data/bg.json b/src/data/bg.json new file mode 100644 index 0000000..8751321 --- /dev/null +++ b/src/data/bg.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/data/get_bg.py b/src/data/get_bg.py new file mode 100644 index 0000000..a43bf0c --- /dev/null +++ b/src/data/get_bg.py @@ -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() diff --git a/src/render/article.py b/src/render/article.py index 879f368..58cae5b 100644 --- a/src/render/article.py +++ b/src/render/article.py @@ -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, } diff --git a/src/templates/article.jinja2 b/src/templates/article.jinja2 index b2234d0..5a30fac 100644 --- a/src/templates/article.jinja2 +++ b/src/templates/article.jinja2 @@ -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. View original post +{% if GAME_BG %} +
+
+ +
+
+{% endif %}
{{ article }} diff --git a/tests/test_get_bg.py b/tests/test_get_bg.py new file mode 100644 index 0000000..34a4552 --- /dev/null +++ b/tests/test_get_bg.py @@ -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)