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("提交完成: ", now) print("UID: " + str(u_id)) 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} else: return {verify: u_verify, uid: u_id, key: u_key, code_output: code} return "请先上传头像和扫码登录" 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("自本次更新以来,已成功为用户设置 " + str(GLOBAL_STATE_SUCCESS) + " 次头像。") 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( """第二步完成后,选择拥有的卡组,并等待加载出所拥有的卡,再指定底卡,务必重新选择,否则无法工作 最后点击按钮<验证并提交头像>等待完成显示结果(成功可能需要时间审核)""") log_into_public = gr.Checkbox(label="展示UID到下方展示区", value=True) 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制作,因此稳定性无法保证,敬请谅解。 使用本服务后,您的bilibili UID将在此页发布,不希望展示请勾选复选框。 本服务要求也必须登录&访问您的账号,这会导致潜在的安全问题,我们无法保证您的账号不会被盗用,但我们将竭尽全力保证您的数据和隐私安全。 建议您在使用后及时修改密码,以退出所有设备的登陆状态,防止盗号风险。 本服务由方糖网络科技开发。""") with gr.Accordion("用户展示"): text = "" for u in SUCCESS_UIDS: text = text + "UID: " + u["uid"] + " 于 " + u["time"] + " 成功设置数字头像。" + "\n\n" if text == "": text = "好像从本次更新起还没有人设置数字头像呢~" gr.Markdown(text) 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, log_into_public], outputs=[verify, uid, key, code_output]) application.launch()