LittlePaimon/Paimon_Gacha_Log/gacha_logs.py

92 lines
2.8 KiB
Python
Raw Normal View History

2022-04-30 14:13:39 +08:00
import json
from pathlib import Path
2022-04-30 14:13:39 +08:00
from asyncio import sleep
2022-05-20 18:44:18 +08:00
from littlepaimon_utils import aiorequests
from littlepaimon_utils.files import load_json, save_json
2022-05-20 18:44:18 +08:00
from .UIGF_and_XLSX import convertUIGF, writeXLSX
2022-04-30 14:13:39 +08:00
from .api import getApi
from .meta_data import gachaQueryTypeIds, gachaQueryTypeDict
data_path = Path() / 'data' / 'LittlePaimon' / 'user_data' / 'gacha_log_data'
2022-04-30 14:13:39 +08:00
async def getGachaLogs(url, gachaTypeId):
size = "20"
# api限制一页最大20
gachaList = []
end_id = "0"
for page in range(1, 9999):
api = getApi(url, gachaTypeId, size, page, end_id)
resp = await aiorequests.get(url=api)
j = resp.json()
gacha = j["data"]["list"]
if not len(gacha):
break
for i in gacha:
gachaList.append(i)
end_id = j["data"]["list"][-1]["id"]
2022-04-30 14:13:39 +08:00
await sleep(0.5)
return gachaList
def mergeDataFunc(localData, gachaData):
for banner in gachaQueryTypeDict:
bannerLocal = localData["gachaLog"][banner]
bannerGet = gachaData["gachaLog"][banner]
if bannerGet == bannerLocal:
pass
else:
flaglist = [1] * len(bannerGet)
loc = [[i["time"], i["name"]] for i in bannerLocal]
for i in range(len(bannerGet)):
gachaGet = bannerGet[i]
get = [gachaGet["time"], gachaGet["name"]]
if get in loc:
pass
else:
flaglist[i] = 0
tempData = []
for i in range(len(bannerGet)):
if flaglist[i] == 0:
gachaGet = bannerGet[i]
tempData.insert(0, gachaGet)
for i in tempData:
localData["gachaLog"][banner].insert(0, i)
return localData
async def get_data(url):
gachaData = {"gachaLog": {}}
for gachaTypeId in gachaQueryTypeIds:
gachaLog = await getGachaLogs(url, gachaTypeId)
gachaData["gachaLog"][gachaTypeId] = gachaLog
uid_flag = 1
for gachaType in gachaData["gachaLog"]:
for log in gachaData["gachaLog"][gachaType]:
if uid_flag and log["uid"]:
gachaData["uid"] = log["uid"]
uid_flag = 0
uid = gachaData["uid"]
localDataFilePath = data_path / f"gachaData-{uid}.json"
2022-04-30 14:13:39 +08:00
if localDataFilePath.is_file():
localData = load_json(localDataFilePath)
2022-04-30 14:13:39 +08:00
mergeData = mergeDataFunc(localData, gachaData)
else:
mergeData = gachaData
mergeData["gachaType"] = gachaQueryTypeDict
# 写入json
save_json(mergeData, localDataFilePath)
2022-04-30 14:13:39 +08:00
# 写入UIGF、json
UIGF_data = convertUIGF(mergeData['gachaLog'], uid)
save_json(UIGF_data, data_path / f"UIGF_gachaData-{uid}.json")
2022-04-30 14:13:39 +08:00
# 写入xlsx
writeXLSX(uid, mergeData['gachaLog'], gachaQueryTypeIds)