Spaces:
Sleeping
Sleeping
File size: 7,314 Bytes
0aee47a |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
"""
bilibili_api.show
展出相关
"""
import json
import random
import time
from dataclasses import dataclass, field
from typing import List
from .utils.credential import Credential
from .utils.network import Api
from .utils.utils import get_api, get_deviceid
API = get_api("show")
@dataclass
class Ticket:
"""
票对象
id (int): 场次id
price (float): 价格(RMB)
desc (str): 描述
sale_start (str): 开售开始时间
sale_end (str): 开售结束时间
"""
id: int
price: int
desc: str
sale_start: str
sale_end: str
@dataclass
class Session:
"""
场次对象
id (int): 场次id
start_time (int): 场次开始时间戳
formatted_time (str): 格式化start_time后的时间格式: YYYY-MM-DD dddd
ticket_list (list[Ticket]): 存放Ticket对象的list
"""
id: int
start_time: int
formatted_time: str
ticket_list: List[Ticket] = field(default_factory=list)
@dataclass
class BuyerInfo:
"""
购买人信息
id (int): 信息序号
uid (int): 用户 ID
account_channel (str): 默认为空
personal_id (str): 身份证号
name (str): 姓名
id_card_front (str): 未知
id_card_back (str): 未知
is_default (bool): 是否为默认信息
tel (str): 电话号码
error_code (str): 错误代码
id_type (int): 默认 0
verify_status (int): 认证状态
accountId (int): 用户 ID
isBuyerInfoVerified (bool): 默认为 True
isBuyerValid (bool): 默认为 True
"""
id: int
uid: int
account_channel: str
personal_id: str
name: str
id_card_front: str
id_card_back: str
is_default: int
tel: str
error_code: str
id_type: int
verify_status: int
accountId: int
isBuyerInfoVerified: bool = True
isBuyerValid: bool = True
async def get_project_info(project_id: int) -> dict:
"""
返回项目全部信息
Args:
project_id (int): 项目id
Returns:
dict: 调用 API 返回的结果
"""
api = API["info"]["get"]
return await Api(**api).update_params(id=project_id).result
async def get_available_sessions(project_id: int) -> List[Session]:
"""
返回该项目的所有可用场次
Args:
project_id (int): 项目id
Returns:
list[Session]: 存放场次对象的list
"""
rtn_list = []
project_info = await get_project_info(project_id)
for v in project_info["screen_list"]:
sess_obj = Session(id=v["id"], start_time=v["start_time"], formatted_time=v["name"])
for t in v["ticket_list"]:
sess_obj.ticket_list.append(
Ticket(
id=t["id"],
price=t["price"],
desc=t["desc"],
sale_start=t["sale_start"],
sale_end=t["sale_end"],
)
)
rtn_list.append(sess_obj)
return rtn_list
async def get_all_buyer_info(credential: Credential) -> dict:
"""
返回账号的全部身份信息
Args:
credential (Credential): 登录凭证
Returns:
dict: 调用 API 返回的结果
"""
credential.raise_for_no_sessdata()
api = API["info"]["buyer_info"]
return await Api(**api, credential=credential).result
async def get_all_buyer_info_obj(credential: Credential) -> List[BuyerInfo]:
"""
以BuyerInfo对象返回账号的全部身份信息
Args:
credential (Credential): 登录凭证
Returns:
list[BuyerInfo]: BuyerInfo对象列表
"""
res = await get_all_buyer_info(credential)
return [BuyerInfo(**v) for v in res["list"]]
def generate_clickPosition() -> dict:
"""
生成虚假的点击事件
Returns:
dict: 点击坐标和时间
"""
# 生成随机的 x 和 y 坐标,以下范围大概是1920x1080屏幕下可能的坐标
x = random.randint(1320, 1330)
y = random.randint(880, 890)
# 生成随机的起始时间和结束时间(或当前时间)
origin_timestamp = int(time.time() * 1000)
# 添加一些随机时间差 (5s ~ 10s)
now_timestamp = origin_timestamp + random.randint(5000, 10000)
return {
"x": x,
"y": y,
"origin": origin_timestamp,
"now": now_timestamp
}
@dataclass
class OrderTicket:
"""
购票类
Args:
credential (Credential): Credential 对象
target_buyer (BuyerInfo): 购票人
project_id (int): 展出id
session (Session): Session 对象
ticket (Ticket): Ticket 对象
"""
credential: Credential
target_buyer: BuyerInfo
project_id: int
session: Session
ticket: Ticket
async def _get_create_order_payload(self) -> dict:
"""
获取 create order API 所需的载荷
Returns:
dict: payload
"""
res = await self.get_token()
header = {
"count": 1,
"order_type": 1,
"pay_money": self.ticket.price,
"project_id": self.project_id,
"screen_id": self.session.id,
"sku_id": self.ticket.id,
"timestamp": int(time.time() * 1000),
"token": res["token"],
"deviceId": get_deviceid('', True),
"clickPosition": json.dumps(generate_clickPosition()),
}
info = await get_project_info(self.project_id)
info = info["performance_desc"]["list"]
for element in info:
if element["module"] == "base_info":
info = element
break
for detail in info["details"]:
content = detail["content"]
if "一人一证" in content or "一单一证" in content:
header.update({
"buyer_info": json.dumps([self.target_buyer.__dict__])
})
return header
header.update({
"buyer": self.target_buyer.name,
"tel": self.target_buyer.tel,
})
return header
async def get_token(self):
"""
获取购票Token
Returns:
dict: 调用 API 返回的结果
"""
self.credential.raise_for_no_sessdata()
api = API["info"]["token"]
payload = {
"count": "1",
"order_type": 1,
"project_id": self.project_id,
"screen_id": self.session.id,
"sku_id": self.ticket.id
}
return await Api(**api, credential=self.credential).update_data(**payload).result
async def create_order(self):
"""
创建购买订单
Returns:
dict: 调用 API 返回的结果
"""
payload = await self._get_create_order_payload()
api = API["operate"]["order"]
return await Api(**api, credential=self.credential).update_params(project_id=self.project_id).update_data(
**payload).result
|