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/__init__.py

269 lines
11 KiB
Python
Raw Normal View History

2021-12-06 15:32:27 +00:00
import os
2021-12-22 12:48:02 +00:00
import traceback
2021-12-06 15:32:27 +00:00
2021-12-21 11:13:38 +00:00
from config import Config
2023-02-05 14:33:33 +00:00
from core.builtins import Bot
from core.builtins import Plain, Image
2023-03-04 08:51:56 +00:00
from core.component import module
2023-02-05 14:33:33 +00:00
from core.utils.http import get_url
2022-01-20 12:13:03 +00:00
from .dbutils import ArcBindInfoManager
2021-12-06 15:32:27 +00:00
from .getb30 import getb30
from .getb30_official import getb30_official
2021-12-06 15:32:27 +00:00
from .info import get_info
2022-01-20 03:39:07 +00:00
from .info_official import get_info_official
2021-12-06 15:32:27 +00:00
from .initialize import arcb30init
from .song import get_song_info
from .utils import get_userinfo
2021-12-06 15:32:27 +00:00
2023-03-15 11:29:25 +00:00
arc = module('arcaea', developers=['OasisAkari'], desc='{arcaea.help}',
2023-03-04 08:51:56 +00:00
alias={'b30': 'arcaea b30', 'a': 'arcaea', 'arc': 'arcaea'})
webrender = Config('web_render')
assets_path = os.path.abspath('./assets/arcaea')
2021-12-06 15:32:27 +00:00
2023-03-15 09:23:19 +00:00
class WithErrCode(Exception):
pass
@arc.command('b30 [<friendcode>] {{arcaea.b30.help}}',
'b30 official [<friendcode>] {{arcaea.message.official}}',
'b30 unofficial [<friendcode>] {{arcaea.message.unofficial}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
if not os.path.exists(assets_path):
2023-03-15 12:15:09 +00:00
await msg.finish(msg.locale.t("arcaea.message.assets.not_found"))
query_code = None
prefer_uses = msg.options.get('arc_api', None)
official = msg.parsed_msg.get('official', False)
2022-08-08 11:29:02 +00:00
unofficial = msg.parsed_msg.get('unofficial', False)
if not unofficial and not official and prefer_uses is False:
unofficial = True
if not official and prefer_uses is True:
official = True
2022-08-08 14:55:02 +00:00
friendcode: str = msg.parsed_msg.get('<friendcode>', False)
2022-08-08 11:29:02 +00:00
if friendcode:
2022-01-20 03:39:07 +00:00
if friendcode.isdigit():
if len(friendcode) == 9:
query_code = friendcode
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.invalid.friendcode.non_digital"))
2022-01-20 03:39:07 +00:00
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.invalid.friendcode.format"))
else:
get_friendcode_from_db = ArcBindInfoManager(msg).get_bind_friendcode()
if get_friendcode_from_db is not None:
query_code = get_friendcode_from_db
if query_code is not None:
if not unofficial:
try:
resp = await getb30_official(query_code)
2023-03-15 09:23:19 +00:00
if resp["status"]:
msgchain = [Plain(msg.locale.t("arcaea.b30.message.success", b30=resp["b30"], r10=resp["r10"],
last5list=resp["last5list"]))]
if 'file' in resp and msg.Feature.image:
msgchain.append(Image(path=resp['file']))
await msg.sendMessage(msgchain, allow_split_image=False)
else:
raise
except Exception:
traceback.print_exc()
if not official and prefer_uses is None:
2023-03-15 09:23:19 +00:00
await msg.sendMessage(msg.locale.t("arcaea.message.official.fetch.failed.fallback"))
unofficial = True
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.official.fetch.failed"))
if unofficial:
try:
resp = await getb30(query_code)
2023-03-15 09:23:19 +00:00
if resp['status']:
msgchain = [Plain(msg.locale.t("arcaea.b30.message.success", b30=resp["b30"], r10=resp["r10"],
last5list=resp["last5list"]))]
if 'file' in resp and msg.Feature.image:
msgchain.append(Image(path=resp['file']))
await msg.finish(msgchain)
else:
if 'errcode' in resp:
raise WithErrCode(resp['errcode'])
else:
raise
except WithErrCode as e:
err_key = "arcaea.errcode." + str(e.args[0])
err_msg = msg.locale.t(err_key)
if err_key != err_msg:
await msg.finish(msg.locale.t("arcaea.message.failed.fetch") + err_msg)
else:
await msg.finish(msg.locale.t("arcaea.message.unofficial.fetch.failed"))
except Exception:
2022-05-13 11:10:27 +00:00
traceback.print_exc()
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.unofficial.fetch.failed"))
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.user.unbound"))
2023-03-15 09:23:19 +00:00
@arc.command('info [<friendcode>] {{arcaea.info.help}}',
'info official [<friendcode>] {{arcaea.message.official}}',
'info unofficial [<friendcode>] {{arcaea.message.unofficial}}', )
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
if not os.path.exists(assets_path):
2023-03-15 12:26:46 +00:00
await msg.sendMessage(msg.locale.t("arcaea.message.assets.not_found"))
return
query_code = None
prefer_uses = msg.options.get('arc_api', None)
2022-08-08 11:29:02 +00:00
unofficial = msg.parsed_msg.get('unofficial', False)
official = msg.parsed_msg.get('official', False)
if not unofficial and not official and prefer_uses is False:
unofficial = True
if not official and prefer_uses is True:
official = True
2022-08-08 14:55:02 +00:00
friendcode = msg.parsed_msg.get('<friendcode>', False)
2022-08-08 11:29:02 +00:00
if friendcode:
2022-01-20 03:39:07 +00:00
if friendcode.isdigit():
if len(friendcode) == 9:
query_code = friendcode
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.invalid.friendcode.non_digital"))
2022-01-20 03:39:07 +00:00
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.invalid.friendcode.format"))
else:
get_friendcode_from_db = ArcBindInfoManager(msg).get_bind_friendcode()
if get_friendcode_from_db is not None:
query_code = get_friendcode_from_db
if query_code is not None:
2022-01-20 03:39:07 +00:00
if not unofficial:
try:
2023-03-17 14:16:12 +00:00
resp = await get_info_official(msg, query_code)
2022-01-20 03:39:07 +00:00
if resp['success']:
2022-05-21 16:04:29 +00:00
await msg.finish(resp['msg'])
2022-01-20 03:39:07 +00:00
else:
if not official and prefer_uses is None:
2023-03-15 09:23:19 +00:00
await msg.sendMessage(msg.locale.t("arcaea.message.official.fetch.failed.fallback"))
unofficial = True
2022-01-20 03:39:07 +00:00
except Exception:
traceback.print_exc()
if not official and prefer_uses is None:
2023-03-15 09:23:19 +00:00
await msg.sendMessage(msg.locale.t("arcaea.message.official.fetch.failed.fallback"))
unofficial = True
2022-01-20 03:39:07 +00:00
if unofficial:
try:
2023-03-17 14:16:12 +00:00
resp = await get_info(msg, query_code)
2022-05-21 16:04:29 +00:00
await msg.finish(resp)
2022-01-20 03:39:07 +00:00
except Exception:
traceback.print_exc()
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.unofficial.fetch.failed"))
2021-12-06 15:32:27 +00:00
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.user.unbound"))
2021-12-06 15:32:27 +00:00
2023-03-15 09:23:19 +00:00
@arc.command('song <songname+prs/pst/byd> {{arcaea.song.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
songname_ = msg.parsed_msg.get('<songname+prs/pst/byd>', False)
songname_split = songname_.split(' ')
diff = -1
for s in songname_split:
2023-03-18 02:49:05 +00:00
s_ = s.lower()
if s_ == 'pst':
diff = 0
2023-03-18 02:49:05 +00:00
elif s_ == 'prs':
diff = 1
2023-03-18 02:49:05 +00:00
elif s_ == 'ftr':
diff = 2
2023-03-18 02:49:05 +00:00
elif s_ == 'byd':
diff = 3
if diff != -1:
songname_split.remove(s)
break
if diff == -1:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.song.message.invalid.difficulty"))
songname = ' '.join(songname_split)
2022-12-25 07:25:50 +00:00
usercode = None
get_friendcode_from_db = ArcBindInfoManager(msg).get_bind_friendcode()
if get_friendcode_from_db is not None:
usercode = get_friendcode_from_db
2023-03-17 14:16:12 +00:00
await msg.finish(Plain(await get_song_info(msg, songname, diff, usercode)))
2023-03-15 09:23:19 +00:00
@arc.command('bind <friendcode/username> {{arcaea.bind.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
code: str = msg.parsed_msg['<friendcode/username>']
getcode = await get_userinfo(code)
if getcode:
bind = ArcBindInfoManager(msg).set_bind_info(username=getcode[0], friendcode=getcode[1])
if bind:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.message.bind.success", username=getcode[0], friendcode=getcode[1]))
else:
if code.isdigit():
bind = ArcBindInfoManager(msg).set_bind_info(username='', friendcode=code)
if bind:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.bind.message.success.but.failed.fetch.username"))
else:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.bind.message.failed"))
2023-03-15 09:23:19 +00:00
@arc.command('unbind {{arcaea.unbind.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
unbind = ArcBindInfoManager(msg).remove_bind_info()
if unbind:
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.unbind.message.success"))
2021-12-06 15:32:27 +00:00
2023-03-04 08:51:56 +00:00
@arc.command('initialize', required_superuser=True)
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2021-12-25 09:32:41 +00:00
assets_apk = os.path.abspath('./assets/arc.apk')
if not os.path.exists(assets_apk):
2023-03-15 12:15:09 +00:00
await msg.finish(msg.locale.t("arcaea.initialize.message.not_found"))
2021-12-25 09:32:41 +00:00
return
result = await arcb30init()
if result:
2023-03-15 12:15:09 +00:00
await msg.finish(msg.locale.t("arcaea.initialize.message.success"))
2021-12-21 11:08:17 +00:00
2023-03-15 09:23:19 +00:00
@arc.command('download {{arcaea.download.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-06-15 00:29:20 +00:00
if not webrender:
2023-03-15 09:23:19 +00:00
await msg.finish([msg.locale.t("arcaea.message.no_webrender")])
2022-01-20 12:13:03 +00:00
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/serve/static/bin/arcaea/apk/', 200,
fmt='json')
2021-12-21 11:08:17 +00:00
if resp:
2023-03-15 09:23:19 +00:00
await msg.finish([Plain(msg.locale.t("arcaea.download.message.success", version=resp["value"]["version"],
url=resp['value']['url']))])
2023-03-15 09:23:19 +00:00
@arc.command('random {{arcaea.random.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-06-15 00:29:20 +00:00
if not webrender:
2023-03-15 09:23:19 +00:00
await msg.finish([msg.locale.t("arcaea.message.no_webrender")])
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/song/showcase/', 200, fmt='json')
if resp:
value = resp["value"][0]
image = f'{assets_path}/jacket/{value["song_id"]}.jpg'
result = [Plain(value["title"]["en"])]
if os.path.exists(image):
result.append(Image(path=image))
2022-05-21 16:04:29 +00:00
await msg.finish(result)
2023-03-15 09:23:19 +00:00
@arc.command('rank free {{arcaea.rank.help.free}}', 'rank paid {{arcaea.rank.help.paid}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-06-15 00:29:20 +00:00
if not webrender:
2023-03-15 09:23:19 +00:00
await msg.finish([msg.locale.t("arcaea.message.no_webrender")])
2022-08-08 11:29:02 +00:00
if msg.parsed_msg.get('free', False):
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/song/rank/free/', 200, fmt='json')
else:
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/song/rank/paid/', 200, fmt='json')
if resp:
r = []
rank = 0
for x in resp['value']:
rank += 1
r.append(f'{rank}. {x["title"]["en"]} ({x["status"]})')
2022-05-21 16:04:29 +00:00
await msg.finish('\n'.join(r))
2023-03-15 09:23:19 +00:00
@arc.command('switch {{arcaea.switch.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
value = msg.options.get('arc_api', True)
set_value = msg.data.edit_option('arc_api', not value)
2023-03-15 09:23:19 +00:00
await msg.finish(msg.locale.t("arcaea.switch.message.success.official"
if not value else "arcaea.switch.message.success.unofficial"))