🐛 修复提前抽取卡池

This commit is contained in:
omg-xtao 2022-10-14 12:05:40 +08:00 committed by GitHub
parent 1a40ffd75c
commit f77c6528d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 11 deletions

View File

@ -8,6 +8,8 @@ from genshin import Client, InvalidCookies
from genshin.utility.ds import generate_dynamic_secret
from genshin.utility.uid import recognize_genshin_server
from httpx import AsyncClient
from pydantic import BaseModel, validator
from datetime import datetime
from modules.apihelper.base import ArtworkImage, PostInfo
from modules.apihelper.helpers import get_device_id
@ -61,7 +63,7 @@ class Hyperion:
@staticmethod
def get_list_url_params(forum_id: int, is_good: bool = False, is_hot: bool = False, page_size: int = 20) -> dict:
params = {
return {
"forum_id": forum_id,
"gids": 2,
"is_good": is_good,
@ -70,8 +72,6 @@ class Hyperion:
"sort_type": 1,
}
return params
@staticmethod
def get_images_params(
resize: int = 600, quality: int = 80, auto_orient: int = 0, interlace: int = 1, images_format: str = "jpg"
@ -141,6 +141,18 @@ class Hyperion:
await self.client.shutdown()
class GachaInfoObject(BaseModel):
begin_time: datetime
end_time: datetime
gacha_id: str
gacha_name: str
gacha_type: int
@validator("begin_time", "end_time", pre=True, allow_reuse=True)
def validate_time(cls, v):
return datetime.strptime(v, "%Y-%m-%d %H:%M:%S")
class GachaInfo:
GACHA_LIST_URL = "https://webstatic.mihoyo.com/hk4e/gacha_info/cn_gf01/gacha/list.json"
GACHA_INFO_URL = "https://webstatic.mihoyo.com/hk4e/gacha_info/cn_gf01/%s/zh-cn.json"
@ -158,16 +170,17 @@ class GachaInfo:
self.cache = {}
self.cache_ttl = 600
async def get_gacha_list_info(self) -> dict:
async def get_gacha_list_info(self) -> List[GachaInfoObject]:
if self.cache.get("time", 0) + self.cache_ttl < time.time():
self.cache.clear()
cache = self.cache.get("gacha_list_info")
if cache is not None:
return cache
req = await self.client.get(self.GACHA_LIST_URL)
self.cache["gacha_list_info"] = req
data = [GachaInfoObject(**i) for i in req["list"]]
self.cache["gacha_list_info"] = data
self.cache["time"] = time.time()
return req
return data
async def get_gacha_info(self, gacha_id: str) -> dict:
cache = self.cache.get(gacha_id)

View File

@ -1,5 +1,6 @@
import os
import re
from datetime import datetime
from typing import Dict
from bs4 import BeautifulSoup
@ -41,12 +42,13 @@ class Gacha(Plugin, BasePlugin):
async def gacha_info(self, gacha_name: str = "角色活动", default: bool = False):
gacha_list_info = await self.gacha.get_gacha_list_info()
gacha_id = ""
for gacha in gacha_list_info["list"]:
if gacha["gacha_name"] == gacha_name:
gacha_id = gacha["gacha_id"]
now = datetime.now()
for gacha in gacha_list_info:
if gacha.gacha_name == gacha_name and gacha.begin_time <= now <= gacha.end_time:
gacha_id = gacha.gacha_id
if gacha_id == "":
if default and len(gacha_list_info["list"]) > 0:
gacha_id = gacha_list_info["list"][0]["gacha_id"]
if default and len(gacha_list_info) > 0:
gacha_id = gacha_list_info[0].gacha_id
else:
raise GachaNotFound(gacha_name)
gacha_info = await self.gacha.get_gacha_info(gacha_id)