Spaces:
Running
Running
File size: 5,087 Bytes
c9ecd6c 4df7720 c9ecd6c 4df7720 c9ecd6c 4df7720 dba73a2 4df7720 dba73a2 4df7720 c9ecd6c 4df7720 c9ecd6c 4df7720 c9ecd6c 4df7720 c9ecd6c 4df7720 c9ecd6c 4df7720 c9ecd6c 4df7720 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
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'
@staticmethod
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()
@staticmethod
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):
@property
def sorted(self):
'''returns a alphabetically sorted version of `self`'''
return dict(sorted(self.items()))
@property
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
|