Spaces:
Running
Running
import time | |
import json | |
from pathlib import Path | |
import requests | |
from hashlib import md5 | |
from typing import Union | |
from urllib.parse import urlencode | |
from requests_toolbelt.multipart.encoder import MultipartEncoder | |
import imghdr | |
CARD_INFO_PATH = Path(__file__).parent / "bili.json" # 不要动 | |
class Crypto: | |
APPKEY = '4409e2ce8ffd12b8' | |
APPSECRET = '59b43e04ad6965f34319062b478f83dd' | |
def md5(data: Union[str, bytes]) -> str: | |
'''generates md5 hex dump of `str` or `bytes`''' | |
if type(data) == str: | |
return md5(data.encode()).hexdigest() | |
return md5(data).hexdigest() | |
def sign(data: Union[str, dict]) -> str: | |
'''salted sign funtion for `dict`(converts to qs then parse) & `str`''' | |
if isinstance(data, dict): | |
_str = urlencode(data) | |
elif type(data) != str: | |
raise TypeError | |
return Crypto.md5(_str + Crypto.APPSECRET) | |
class SingableDict(dict): | |
def sorted(self): | |
'''returns a alphabetically sorted version of `self`''' | |
return dict(sorted(self.items())) | |
def signed(self): | |
'''returns our sorted self with calculated `sign` as a new key-value pair at the end''' | |
_sorted = self.sorted | |
return {**_sorted, 'sign': Crypto.sign(_sorted)} | |
def get_image_type(file_path): | |
with open(file_path, 'rb') as f: | |
data = f.read() | |
return imghdr.what(None, data) | |
def get_one_card_id(UID, ACCESS_KEY, sid): | |
url = "https://api.bilibili.com/x/vas/nftcard/cardlist" | |
params = SingableDict( | |
{ | |
"access_key": ACCESS_KEY, | |
"act_id": sid, # 这里默认已经是三体数字藏品卡14,github下载的默认是4,也就是胶囊卡 | |
"appkey": "4409e2ce8ffd12b8", | |
"disable_rcmd": "0", | |
"ruid": UID, | |
"statistics": "{\"appId\":1,\"platform\":3,\"version\":\"7.9.0\",\"abtest\":\"\"}", | |
"ts": int(time.time()), | |
} | |
).signed | |
response = requests.request("GET", url, params=params) | |
data = response.json() | |
if data['code'] != 0: | |
print(f"获取卡片信息出错,下面是 api 返回:\n{data}") | |
return | |
# print(data) # 查询已添加无需再填 | |
for round in data['data']['round_list']: | |
for card in round['card_list']: | |
if card['card_type'] == 2 and card['card_id_list']: | |
print(card['card_id_list'][0]['card_id']) | |
print("=================================") | |
print(f"找到付费卡 card_id: {card['card_id_list'][0]['card_id']}\n这个id属于{card['card_name']}") | |
print("=================================") | |
return card['card_id_list'][0]['card_id'] | |
print('没有付费卡片') | |
for i in data["data"]["pre_list"]: | |
if card_id_list := i.get("card_id_list"): | |
for j in card_id_list: | |
if card_id := j.get("card_id"): | |
print("=================================") | |
print(f"找到预约卡card_id: {card_id}\n这个 id 属于{i.get('card_name')}") | |
print("=================================") | |
return card_id | |
else: | |
print() # 有值的 card_id_list 中没找到 card_id ,可能是没领三体卡吧 | |
else: | |
print() # 卡片信息中没找到有值的 card_id_list ,可能是没有这张三体卡吧 | |
def set_face(card_id, ACCESS_KEY, img_data): | |
api = "https://api.bilibili.com/x/member/app/face/digitalKit/update" | |
params = SingableDict( | |
{ | |
"access_key": ACCESS_KEY, | |
"appkey": "4409e2ce8ffd12b8", | |
"build": "7090300", | |
"c_locale": "zh_CN", | |
"channel": "xiaomi", | |
"disable_rcmd": "0", | |
"mobi_app": "android", | |
"platform": "android", | |
"s_locale": "zh_CN", | |
"statistics": "{\"appId\":1,\"platform\":3,\"version\":\"7.9.0\",\"abtest\":\"\"}", | |
"ts": int(time.time()), | |
} | |
).signed | |
m = MultipartEncoder( | |
fields={ | |
'digital_kit_id': str(card_id), | |
'face': ('face', img_data, 'application/octet-stream'), | |
} | |
) | |
headers = { | |
"Content-Type": m.content_type, | |
} | |
response = requests.request("POST", api, data=m, headers=headers, params=params) | |
if response.json()['code'] != 0: | |
return False, response.json() | |
return True, '设置头像成功, 请等待审核' | |
def up_card(uid, key, img_data, sid): | |
UID = int(uid) | |
access_key = str(key) | |
sid = str(sid) | |
result = False | |
code = "超时" | |
card_id = get_one_card_id(UID, access_key, sid) # 根据你所取得的卡card_id进行更改 | |
if not card_id: | |
code = "没找到 card_id ,请确认是否已经领取卡片" | |
# img_url = upload_image(BG_PATH) | |
# set_bg_img(img_url, card_id) | |
else: | |
result, code = set_face(card_id, access_key, img_data) | |
return result, code | |