MibooGram/modules/apihelper/gacha.py

45 lines
1.5 KiB
Python
Raw Normal View History

2022-10-01 06:56:15 +00:00
import time
2022-07-26 10:07:31 +00:00
import httpx
from modules.apihelper.base import BaseResponseData
2022-07-26 10:07:31 +00:00
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"
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/90.0.4430.72 Safari/537.36"
def __init__(self):
self.headers = {
'User-Agent': self.USER_AGENT,
}
self.client = httpx.AsyncClient(headers=self.headers)
2022-10-01 06:56:15 +00:00
self.cache = {}
self.cache_ttl = 600
2022-07-26 10:07:31 +00:00
async def get_gacha_list_info(self) -> BaseResponseData:
2022-10-01 06:56:15 +00:00
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 BaseResponseData(cache)
2022-07-26 10:07:31 +00:00
req = await self.client.get(self.GACHA_LIST_URL)
if req.is_error:
return BaseResponseData(error_message="请求错误")
2022-10-01 06:56:15 +00:00
self.cache["gacha_list_info"] = req.json()
self.cache["time"] = time.time()
2022-07-26 10:07:31 +00:00
return BaseResponseData(req.json())
async def get_gacha_info(self, gacha_id: str) -> dict:
2022-10-01 06:56:15 +00:00
cache = self.cache.get(gacha_id)
if cache is not None:
return cache
2022-07-26 10:07:31 +00:00
req = await self.client.get(self.GACHA_INFO_URL % gacha_id)
if req.is_error:
return {}
2022-10-01 06:56:15 +00:00
self.cache[gacha_id] = req.json()
2022-07-26 10:07:31 +00:00
return req.json()