Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/arcaea/getb30_official.py

137 lines
5.4 KiB
Python
Raw Normal View History

"""
This service utilizes API functionality provided by and with permission from lowiro. It is not affiliated with or endorsed by lowiro.
"""
import asyncio
import os
import traceback
import uuid
from config import Config
2022-08-04 07:52:42 +00:00
from core.logger import Logger
2022-01-20 12:13:03 +00:00
from core.utils import get_url
from .drawb30img import drawb30
from .drawsongimg import dsimg
assets_path = os.path.abspath('./assets/arcaea')
apiurl = Config('arcapi_official_url')
headers = {'Authorization': Config('arcapi_official_token')}
headers_botarcapi = {"User-Agent": Config('botarcapi_agent')}
botarcapi_url = Config('botarcapi_url')
async def getb30_official(usercode):
try:
getuserinfo_json = await get_url(f'{apiurl}user/{usercode}', headers=headers, status_code=200, fmt='json')
getb30_json = await get_url(f'{apiurl}user/{usercode}/best', headers=headers, status_code=200, fmt='json')
except Exception:
traceback.print_exc()
return {'text': f'获取失败。'}
getuserinfo = getuserinfo_json['data']
username = getuserinfo['display_name']
2022-07-20 11:51:15 +00:00
potential = getuserinfo['potential']
if potential is not None:
potential = int(potential) / 100
else:
potential = '--'
getb30 = getb30_json['data']
b30potential = []
for x in getb30:
b30potential.append(x['potential_value'])
b30_avg = round(sum(b30potential) / len(b30potential), 4)
2022-07-20 11:51:15 +00:00
r10_avg = round((potential * 40 - b30_avg * 30) / 10, 4) if potential is not None else '???'
songsinfo = {}
getinfos = []
for x in getb30:
async def get_songinfo(songid):
try:
2022-01-20 12:13:03 +00:00
get_songinfo_from_botarcapi = await get_url(f'{botarcapi_url}song/info?songid={songid}',
headers=headers_botarcapi, status_code=200, fmt='json')
songsinfo[songid] = get_songinfo_from_botarcapi['content']
except Exception:
traceback.print_exc()
2022-01-20 12:13:03 +00:00
getinfos.append(get_songinfo(x['song_id']))
await asyncio.gather(*getinfos)
newdir = f'./cache/{str(uuid.uuid4())}'
newdir = os.path.abspath(newdir)
os.makedirs(newdir)
d = 0
tracknames = {}
ptts = {}
realptts = {}
scores = {}
run_lst = []
for x in getb30:
d = d + 1
2022-01-20 12:13:03 +00:00
async def draw_jacket(x, d):
difficulty = '???'
if x['difficulty'] == 0:
difficulty = 'PST'
elif x['difficulty'] == 1:
difficulty = 'PRS'
elif x['difficulty'] == 2:
difficulty = 'FTR'
elif x['difficulty'] == 3:
difficulty = 'BYD'
2022-05-13 11:10:27 +00:00
songinfo = songsinfo.get(x['song_id'], {})
trackname = x['song_id']
realptt = False
if songinfo:
trackname = songinfo['difficulties'][x['difficulty']]['name_en']
realptt = songinfo['difficulties'][x['difficulty']]['rating']
realptts[x['song_id'] + difficulty] = realptt
tracknames[x['song_id'] + difficulty] = trackname + f' ({difficulty})'
imgpath = f'{assets_path}/b30background_img_official/{x["song_id"]}_{str(x["difficulty"])}.jpg'
if not os.path.exists(imgpath):
imgpath = f'{assets_path}/b30background_img_official/{x["song_id"]}.jpg'
2022-05-13 11:10:27 +00:00
score = x['score']
if not realptt:
2022-03-02 10:42:10 +00:00
realptt = x['potential_value']
if score >= 10000000:
2022-03-02 10:42:10 +00:00
realptt -= 2
elif score >= 9800000:
2022-03-02 10:42:10 +00:00
realptt -= 1 + (score - 9800000) / 200000
elif score <= 9500000:
2022-03-02 10:42:10 +00:00
realptt -= (score - 9500000) / 300000
realptt = round(realptt, 1) * 10
realptts[x['song_id'] + difficulty] = realptt
ptt = x['potential_value']
ptts[x['song_id'] + difficulty] = ptt
scores[x['song_id'] + difficulty] = score
if not os.path.exists(imgpath):
imgpath = f'{assets_path}/b30background_img_official/random.jpg'
dsimg(os.path.abspath(imgpath), d, trackname, x['difficulty'], score, ptt, realptt,
x['pure_count'], x['far_count'], x['lost_count'], x['time_played'], newdir)
run_lst.append(draw_jacket(x, d))
await asyncio.gather(*run_lst)
last5rank = 0
last5list = ''
for last5 in getb30[-5:]:
last5rank += 1
difficulty = '???'
if last5['difficulty'] == 0:
difficulty = 'PST'
if last5['difficulty'] == 1:
difficulty = 'PRS'
if last5['difficulty'] == 2:
difficulty = 'FTR'
if last5['difficulty'] == 3:
difficulty = 'BYD'
trackname = tracknames[last5['song_id'] + difficulty]
realptt = realptts[last5['song_id'] + difficulty]
ptt = ptts[last5['song_id'] + difficulty]
score = scores[last5['song_id'] + difficulty]
2022-01-20 11:23:12 +00:00
last5list += f'[{last5rank}] {trackname}\n' \
2022-01-20 14:34:01 +00:00
f'[{last5rank}] {score} / {realptt / 10} -> {round(ptt, 4)}\n'
2022-08-04 07:52:42 +00:00
Logger.debug(last5list)
filename = drawb30(username, b30_avg, r10_avg, potential, 0, newdir, official=True)
filelist = os.listdir(newdir)
for x in filelist:
os.remove(f'{newdir}/{x}')
os.removedirs(newdir)
return {'text': f'获取结果\nB30: {b30_avg} | R10: {r10_avg}\nB30倒5列表\n{last5list}', 'file': filename}