Huge Rewrite!
This commit is contained in:
@@ -1,82 +1,112 @@
|
||||
# 非常 All-in Boom 的 B50 更新实现。
|
||||
|
||||
from API_TitleServer import *
|
||||
from HelperLogInOut import apiLogin, apiLogout, generateTimestamp
|
||||
from Static_Settings import *
|
||||
import json
|
||||
from MusicDB import musicDB
|
||||
from Config import *
|
||||
from loguru import logger
|
||||
from HelperGetUserMusicDetail import getUserFullMusicDetail
|
||||
from HelperMusicDB import getMusicTitle
|
||||
import requests
|
||||
|
||||
def getMusicTitle(musicId: int) -> str:
|
||||
'''从数据库获取音乐的标题'''
|
||||
logger.debug(f"查询歌名: {musicId}")
|
||||
musicInfo = musicDB.get(musicId)
|
||||
if not musicInfo:
|
||||
logger.warning(f"数据库里未找到此歌曲: {musicId}")
|
||||
return "ERR_R_MUSIC_ID_NOT_IN_DATABASE"
|
||||
musicName = musicInfo.get("name")
|
||||
logger.debug(f"成功查询到歌名: {musicName}")
|
||||
return musicName
|
||||
|
||||
def getUserMusicDetail(userId:int, nextIndex:int=0, maxCount:int=50) -> dict:
|
||||
'''获取用户的成绩的API'''
|
||||
data = json.dumps({
|
||||
"userId": int(userId),
|
||||
"nextIndex": nextIndex,
|
||||
"maxCount": maxCount
|
||||
})
|
||||
return json.loads(apiSDGB(data, "GetUserMusicApi", userId))
|
||||
# 日志设置
|
||||
if False:
|
||||
import sys
|
||||
log_level = "DEBUG"
|
||||
log_format = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS zz}</green> | <level>{level: <8}</level> | <yellow>Line {line: >4} ({file}):</yellow> <b>{message}</b>"
|
||||
logger.add(sys.stderr, level=log_level, format=log_format, colorize=True, backtrace=True, diagnose=True)
|
||||
logger.add("file.log", level=log_level, format=log_format, colorize=False, backtrace=True, diagnose=True)
|
||||
|
||||
# 水鱼查分器的 API 地址
|
||||
BASE_URL = 'https://www.diving-fish.com/api/maimaidxprober'
|
||||
|
||||
# 水鱼查分器的成绩状态转换
|
||||
COMBO_ID_TO_NAME = ['', 'fc', 'fcp', 'ap', 'app']
|
||||
SYNC_ID_TO_NAME = ['', 'fs', 'fsp', 'fsd', 'fsdp', 'sync']
|
||||
|
||||
def apiDivingFish(method:str, apiPath:str, importToken:str, data:dict=None) -> dict:
|
||||
'''水鱼查分器的 API 通讯实现'''
|
||||
headers = {
|
||||
"Import-Token": importToken
|
||||
}
|
||||
if method == 'POST':
|
||||
headers['Content-Type'] = 'application/json'
|
||||
logger.info(f'水鱼查分器 API 请求:{method} {BASE_URL + apiPath}')
|
||||
if method == 'POST':
|
||||
response = requests.post(
|
||||
url=BASE_URL + apiPath,
|
||||
json=data,
|
||||
headers=headers,
|
||||
)
|
||||
elif method == 'GET':
|
||||
response = requests.get(
|
||||
url=BASE_URL + apiPath,
|
||||
headers=headers,
|
||||
)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
logger.info(f'水鱼查分器请求结果:{response.status_code} {response.text}')
|
||||
if response.status_code != 200:
|
||||
return False
|
||||
return response.json()
|
||||
|
||||
def getFishRecords(importToken: str) -> dict:
|
||||
return apiDivingFish('GET', '/player/records', importToken)
|
||||
|
||||
def updateFishRecords(importToken: str, records: list[dict]) -> dict:
|
||||
return apiDivingFish('POST', '/player/update_records', importToken, records)
|
||||
|
||||
def maimaiUserMusicDetailToDivingFish(userMusicDetailList: list) -> list:
|
||||
'''舞萌的 UserMusicDetail 成绩格式转换成水鱼的格式'''
|
||||
divingFishList = []
|
||||
for currentMusicDetail in userMusicDetailList:
|
||||
# musicId 大于 100000 属于宴谱,不计入
|
||||
if currentMusicDetail['musicId'] >= 100000:
|
||||
continue
|
||||
# 获得歌名
|
||||
currentMusicTitle = getMusicTitle(currentMusicDetail['musicId'])
|
||||
# 如果数据库里未找到此歌曲
|
||||
if currentMusicTitle == "R_ERR_MUSIC_ID_NOT_IN_DATABASE":
|
||||
logger.warning(f"数据库无此歌曲 跳过: {currentMusicDetail['musicId']}")
|
||||
continue
|
||||
# 每一个乐曲都判断下是 DX 还是标准
|
||||
if currentMusicDetail['musicId'] >= 10000:
|
||||
notesType = 'DX'
|
||||
else:
|
||||
notesType = 'SD'
|
||||
|
||||
divingFishList.append({
|
||||
'achievements': (currentMusicDetail['achievement'] / 10000), # 水鱼的成绩是 float 而非舞萌的 int
|
||||
'title': (getMusicTitle(currentMusicDetail['musicId'])), # 水鱼用的是歌名而不是 ID(导致不得不用数据库处理转换
|
||||
'type': notesType, # 我不理解这为什么不能在后端判断
|
||||
'level_index': currentMusicDetail['level'],
|
||||
'fc': currentMusicDetail['comboStatus'],
|
||||
'fs': currentMusicDetail['syncStatus'],
|
||||
'dxScore': currentMusicDetail['deluxscoreMax'],
|
||||
})
|
||||
# 追加进列表
|
||||
try:
|
||||
divingFishList.append({
|
||||
'achievements': (currentMusicDetail['achievement'] / 10000), # 水鱼的成绩是 float 而非舞萌的 int
|
||||
'title': currentMusicTitle,
|
||||
'type': notesType, # 我不理解这为什么不能在后端判断
|
||||
'level_index': currentMusicDetail['level'],
|
||||
'fc': COMBO_ID_TO_NAME[currentMusicDetail['comboStatus']],
|
||||
'fs': SYNC_ID_TO_NAME[currentMusicDetail['syncStatus']],
|
||||
'dxScore': currentMusicDetail['deluxscoreMax'],
|
||||
})
|
||||
except:
|
||||
print(currentMusicDetail)
|
||||
logger.error(f"Error: {currentMusicDetail}")
|
||||
return divingFishList
|
||||
|
||||
if __name__ == '__main__':
|
||||
userId = testUid
|
||||
currentLoginTimestamp = generateTimestamp()
|
||||
loginResult = apiLogin(currentLoginTimestamp, userId)
|
||||
if True:
|
||||
userId = testUid
|
||||
importToken = testImportToken
|
||||
currentLoginTimestamp = generateTimestamp()
|
||||
loginResult = apiLogin(currentLoginTimestamp, userId)
|
||||
|
||||
if loginResult['returnCode'] != 1:
|
||||
logger.info("登录失败")
|
||||
exit()
|
||||
try:
|
||||
## Begin
|
||||
userMusicDetailList_current = []
|
||||
if loginResult['returnCode'] != 1:
|
||||
logger.info("登录失败")
|
||||
exit()
|
||||
try:
|
||||
userFullMusicDetailList = getUserFullMusicDetail(userId)
|
||||
logger.warning("Now We Begin To Build DivingFish Data")
|
||||
divingFishData = maimaiUserMusicDetailToDivingFish(userFullMusicDetailList)
|
||||
logger.debug(divingFishData)
|
||||
logger.warning("Now We Begin To Update DivingFish Data")
|
||||
updateFishRecords(importToken, divingFishData)
|
||||
finally:
|
||||
#logger.error(f"Error: {e}")
|
||||
logger.info(apiLogout(currentLoginTimestamp, userId))
|
||||
|
||||
nextIndex:int|None = None # 初始化 nextIndex
|
||||
while nextIndex != 0 or nextIndex is None: #只要还有nextIndex就一直获取获取
|
||||
userMusicResponse = getUserMusicDetail(userId, nextIndex or 0)
|
||||
nextIndex = userMusicResponse['nextIndex']
|
||||
logger.info(f"NextIndex: {nextIndex}")
|
||||
for currentMusic in userMusicResponse['userMusicList']:
|
||||
for currentMusicDetail in currentMusic['userMusicDetailList']:
|
||||
if not currentMusicDetail['playCount'] > 0:
|
||||
continue
|
||||
userMusicDetailList_current.append(currentMusicDetail)
|
||||
print("---------------")
|
||||
print(str(userMusicDetailList_current))
|
||||
## End
|
||||
#logger.info(apiLogout(currentLoginTimestamp, userId))
|
||||
logger.warning("Now We Begin To Build DiveFish Data")
|
||||
divingFishData = maimaiUserMusicDetailToDivingFish(userMusicDetailList_current)
|
||||
logger.info(divingFishData)
|
||||
finally:
|
||||
#logger.error(f"Error: {e}")
|
||||
logger.info(apiLogout(currentLoginTimestamp, userId))
|
||||
|
||||
Reference in New Issue
Block a user