mirror of
https://github.com/PaiGramTeam/PaiGram.git
synced 2024-11-26 10:16:32 +00:00
✨ gacha 缓存 12 小时卡池数据
This commit is contained in:
parent
30cd89714f
commit
1b5a969b96
@ -1,3 +1,4 @@
|
||||
import time
|
||||
import httpx
|
||||
|
||||
from modules.apihelper.base import BaseResponseData
|
||||
@ -15,15 +16,28 @@ class GachaInfo:
|
||||
'User-Agent': self.USER_AGENT,
|
||||
}
|
||||
self.client = httpx.AsyncClient(headers=self.headers)
|
||||
self.cache = {}
|
||||
self.cache_ttl = 12 * 60 * 60
|
||||
|
||||
async def get_gacha_list_info(self) -> BaseResponseData:
|
||||
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)
|
||||
req = await self.client.get(self.GACHA_LIST_URL)
|
||||
if req.is_error:
|
||||
return BaseResponseData(error_message="请求错误")
|
||||
self.cache["gacha_list_info"] = req.json()
|
||||
self.cache["time"] = time.time()
|
||||
return BaseResponseData(req.json())
|
||||
|
||||
async def get_gacha_info(self, gacha_id: str) -> dict:
|
||||
cache = self.cache.get(gacha_id)
|
||||
if cache is not None:
|
||||
return cache
|
||||
req = await self.client.get(self.GACHA_INFO_URL % gacha_id)
|
||||
if req.is_error:
|
||||
return {}
|
||||
self.cache[gacha_id] = req.json()
|
||||
return req.json()
|
||||
|
Loading…
Reference in New Issue
Block a user