Skip to content

Commit

Permalink
✨ Support genshin stats new fields
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Aug 29, 2024
1 parent 05abee3 commit 43176fc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion modules/apihelper/client/components/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ def count_width(

def get_date(d1: str, d2: str) -> datetime:
if d1 and len(d1) > 6:
return datetime.strptime(d1, "%Y-%m-%d %H:%M:%S")
try:
return datetime.strptime(d1, "%Y-%m-%d %H:%M:%S")
except ValueError:
return datetime.strptime(d1, "%Y-%m-%d %H:%M")
return datetime.strptime(d2, "%Y-%m-%d %H:%M:%S")

s_date = get_date(detail and detail.start, ds.start_time)
Expand Down
19 changes: 17 additions & 2 deletions plugins/genshin/stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from pathlib import Path
from typing import Optional, TYPE_CHECKING, List
from telegram.constants import ChatAction
from telegram.ext import filters
Expand All @@ -10,6 +11,8 @@
from core.services.template.services import TemplateService
from gram_core.plugin.methods.inline_use_data import IInlineUseData
from plugins.tools.genshin import GenshinHelper
from utils.const import RESOURCE_DIR
from utils.error import UrlResourcesNotFoundError
from utils.log import logger
from utils.uid import mask_number

Expand Down Expand Up @@ -76,6 +79,7 @@ async def render(self, client: "GenshinClient", uid: Optional[int] = None) -> Re
("活跃天数", "days_active"),
("成就达成数", "achievements"),
("获取角色数", "characters"),
("满好感角色数", "full_fetter_avatar_num"),
("深境螺旋", "spiral_abyss"),
("解锁传送点", "unlocked_waypoints"),
("解锁秘境", "unlocked_domains"),
Expand All @@ -89,6 +93,7 @@ async def render(self, client: "GenshinClient", uid: Optional[int] = None) -> Re
("雷神瞳", "electroculi"),
("草神瞳", "dendroculi"),
("水神瞳", "hydroculi"),
("火神瞳", "pyroculi"),
],
"style": random.choice(["mondstadt", "liyue"]), # nosec
}
Expand All @@ -102,15 +107,25 @@ async def render(self, client: "GenshinClient", uid: Optional[int] = None) -> Re
full_page=True,
)

async def _download_resource(self, url: str) -> str:
try:
return await self.download_resource(url)
except UrlResourcesNotFoundError:
path = Path(url)
file_path = RESOURCE_DIR / "img" / "city" / path.name
if file_path.exists():
return file_path.as_uri()
logger.warning("缓存地区图片资源失败 %s", url)

async def cache_images(self, data: "GenshinUserStats") -> None:
"""缓存所有图片到本地"""
# TODO: 并发下载所有资源

# 探索地区
for item in data.explorations:
item.__config__.allow_mutation = True
item.icon = await self.download_resource(item.icon)
item.cover = await self.download_resource(item.cover)
item.icon = await self._download_resource(item.icon)
item.cover = await self._download_resource(item.cover)

async def stats_use_by_inline(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
callback_query = update.callback_query
Expand Down
Binary file added resources/img/city/UI_ChapterCover_Nata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/img/city/UI_ChapterIcon_Nata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 43176fc

Please sign in to comment.