bili-nft-avatar / app.py
rdp-studio's picture
Update app.py
a46961e
raw
history blame
7.16 kB
import gradio as gr
import io
import datetime
from pytz import timezone
from login import catch_qr, get_uid_key
from nft import having_card_id_list, card_id_set_ava
GLOBAL_STATE_SUCCESS = 0
SUCCESS_UIDS = []
sid_map = {
"SHN48荣耀时刻": 1,
"无": 2,
"无2": 3,
"胶囊计划": 4,
"天官赐福": 5,
"A - AKB48TSH四周年": 6,
"B - AKB48TSH四周年": 7,
"C - AKB48TSH四周年": 8,
"D - AKB48TSH四周年": 9,
"E - AKB48TSH四周年": 10,
"F - AKB48TSH四周年": 11,
"G - AKB48TSH四周年": 12,
"H - AKB48TSH四周年": 13,
"三体动画": 14
}
def qr(ava_image):
if ava_image:
qr_img, info = catch_qr(ava_image)
return {image_qr: qr_img, login_info: info}
return {image_qr: None, login_info: None}
def verify_login(info, u_verify, u_id, u_key, sid):
sid_num = sid_map[sid]
code = "请刷新页面并重新扫码登录"
if not u_verify:
result, u_id, u_key, code = get_uid_key(info)
if not result:
return False, "", "", sid_num, code
else:
u_verify = True
return u_verify, u_id, u_key, sid_num, code
def back_card_id_list(info, u_verify, u_id, u_key, sid):
if info:
u_verify, u_id, u_key, sid_num, code = verify_login(info, u_verify, u_id, u_key, sid)
if u_verify:
result, had_card_id_list, code = having_card_id_list(u_id, u_key, sid_num)
if result:
print("获取卡牌列表成功")
card_name_list = list(had_card_id_list.keys())
return {verify: u_verify, uid: u_id, key: u_key, card_id_list: had_card_id_list,
card_list_drop: gr.update(choices=card_name_list, value=card_name_list[0]), code_output: code}
else:
print("获取卡牌列表失败")
return {verify: u_verify, uid: u_id, key: u_key, card_list_drop: gr.update(choices=[]),
code_output: code}
else:
return {code_output: "请先上传头像和扫码登录"}
def name_get_card_id(card_id_list_dict, card_list_drop_name):
if card_id_list_dict and card_list_drop_name:
for card_name in card_id_list_dict.keys():
if card_name == card_list_drop_name:
cid = card_id_list_dict[card_name]
return {card_id: cid}
return {card_id: ""}
def login_up_nft(info, u_verify, u_id, u_key, sid, cid, ava_image, log_into_public):
global GLOBAL_STATE_SUCCESS, SUCCESS_UIDS
if info and ava_image:
u_verify, u_id, u_key, sid_num, code = verify_login(info, u_verify, u_id, u_key, sid)
if u_verify:
if not cid:
result, had_card_id_list, code = having_card_id_list(u_id, u_key, sid_num)
if had_card_id_list:
cid = list(had_card_id_list.values())[0]
bytes_object = io.BytesIO()
ava_image.save(bytes_object, format='PNG')
img_data = bytes_object.getvalue()
result, code = card_id_set_ava(cid, u_key, img_data)
tz = timezone('Asia/Shanghai')
now = datetime.datetime.now(tz)
print("UID: ", u_id, " 提交完成: ", datetime.datetime.strptime(now, '%Y-%m-%d %H:%M:%S'))
if result:
GLOBAL_STATE_SUCCESS = GLOBAL_STATE_SUCCESS + 1
SUCCESS_UIDS.append({"uid": u_id, "time": now})
return {verify: u_verify, uid: u_id, key: u_key, code_output: code + "\n" + "自本次更新/系统重启以来,已成功为用户设置 " + str(GLOBAL_STATE_SUCCESS) + " 次头像。感谢您使用此服务!"}
else:
return {verify: u_verify, uid: u_id, key: u_key, code_output: code}
return {code_output: "请先上传头像和扫码登录"}
def status():
global GLOBAL_STATE_SUCCESS, SUCCESS_UIDS
text = "自本次更新/系统重启以来,已成功为用户设置 " + str(GLOBAL_STATE_SUCCESS) + " 次头像。"
for u in SUCCESS_UIDS:
text = text + "\n" + "UID: " + u["uid"] + " 于 " + datetime.datetime.strptime(u["time"], '%Y-%m-%d %H:%M:%S') + " 成功设置数字头像。"
return text
with gr.Blocks() as application:
login_info = gr.State([])
card_id_list = gr.State({})
card_id = gr.State("")
verify = gr.State(False)
key = gr.State("")
uid = gr.State("")
with gr.Tab("BiliBili自定义数字头像"):
gr.Markdown("第一步:")
gr.Markdown("""获取一个最低R级的数字周边,多关注官方活动。如果列表中没有最新的卡组,请耐心等待兼容更新。""")
gr.Markdown("第二步:")
with gr.Row():
with gr.Column():
gr.Markdown(
"""成功领取后,下方上传新头像,头像尽量为正方形并长宽小于1024像素,右上角画笔按钮可裁剪,
然后等待生成二维码并扫码登录进行授权(左上传头像,右扫码登录)""")
image_ava = gr.Image(type="pil", label="上传头像")
image_qr = gr.Image(type="pil", label="客户端扫码登录")
gr.Markdown("第三步:")
with gr.Row():
gr.Markdown(
"""第二步完成后,选择拥有的卡组,并等待加载出所拥有的卡,再指定底卡,务必重新选择,否则无法工作
最后点击按钮<验证并提交头像>等待完成显示结果(成功可能需要时间审核)""")
with gr.Row():
sid_drop = gr.Dropdown(label="卡组", choices=list(sid_map.keys()), value="无")
card_list_drop = gr.Dropdown(label="指定已有底卡")
set_button = gr.Button("验证并提交头像")
code_output = gr.Textbox(label="消息输出", placeholder="点击提交按钮才会提交")
with gr.Accordion("警告"):
gr.Markdown("""本服务基于bilibili未公开API制作,因此稳定性无法保证,敬请谅解。
本服务要求也必须登录&访问您的账号,我们无法保证您的账号不会被第三方劫持或盗用,但我们将竭尽全力保证您的数据和隐私安全。建议您在使用后及时修改密码,以退出所有设备的登陆状态,防止盗号风险。
本服务由方糖网络科技开发。""")
gr.Markdown("服务状态:")
with gr.Row():
status_button = gr.Button("获取服务状态")
status_output = gr.Textbox(label="服务状态", placeholder="点击获取按钮才会显示")
image_ava.change(qr, inputs=[image_ava], outputs=[image_qr, login_info])
sid_drop.change(back_card_id_list, inputs=[login_info, verify, uid, key, sid_drop],
outputs=[card_id_list, verify, uid, key, card_list_drop, code_output])
card_list_drop.change(name_get_card_id, inputs=[card_id_list, card_list_drop], outputs=[card_id])
set_button.click(login_up_nft, inputs=[login_info, verify, uid, key, sid_drop, card_id, image_ava],
outputs=[verify, uid, key, code_output])
status_button.click(status, inputs=[], outputs=[status_output])
application.launch()