forked from ZianTT/BHYG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
429 lines (412 loc) · 18.8 KB
/
main.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# -*- coding: UTF-8 -*-
# Copyright (c) 2023 ZianTT
import json
import os
import threading
import time
import kdl
import requests
from loguru import logger
from api import BilibiliHyg
from globals import *
from utils import prompt, save, load
import inquirer
from i18n import i18n
common_project_id = [
{"name": "上海·BilibiliWorld 2024", "id": 85939},
{"name": "上海·BILIBILI MACRO LINK 2024", "id": 85938}
]
def run(hyg):
if hyg.config["mode"] == 'direct':
while True:
if hyg.try_create_order():
if "hunter" not in hyg.config:
hyg.sdk.capture_message("Pay success!")
logger.success(i18n["zh"]["pay_success"])
return
else:
hyg.config['hunter'] += 1
save(hyg.config)
logger.success(i18n["zh"]["hunter_prompt"].format(hyg.config['hunter']))
elif hyg.config["mode"] == 'detect':
while 1:
hyg.risk = False
if hyg.risk:
status = -1
status, clickable = hyg.get_ticket_status()
if status == 2 or clickable:
if status == 1:
logger.warning("未开放购票")
elif status == 3:
logger.warning("已停售")
elif status == 5:
logger.warning("不可售")
elif status == 102:
logger.warning("已结束")
while True:
if hyg.try_create_order():
if "hunter" not in hyg.config:
hyg.sdk.capture_message("Pay success!")
logger.success("购票成功!")
return
else:
hyg.config['hunter'] += 1
save(hyg.config)
logger.success(f"猎手,你的战绩:{hyg.config['hunter']}张")
break
elif status == 1:
logger.warning("未开放购票")
elif status == 3:
logger.warning("已停售")
elif status == 4:
logger.warning("已售罄")
elif status == 5:
logger.warning("不可售")
elif status == 6:
logger.error("免费票,程序尚未适配")
sentry_sdk.capture_message("Exit by in-app exit")
return
elif status == 8:
logger.warning("暂时售罄,即将放票")
elif status == -1:
continue
else:
logger.error("未知状态:" + str(status))
time.sleep(hyg.config["status_delay"])
elif hyg.config["mode"] == 'time':
logger.info("当前为定时抢票模式")
logger.info("等待到达开票时间...")
while hyg.get_time() < hyg.config["time"] - 60:
time.sleep(10)
logger.info(f"等待中,距离开票时间还有{hyg.config['time'] - get_time():.2f}秒")
logger.info("唤醒!即将开始抢票!") # Heads up, the wheels are spinning...
while True:
if hyg.get_time() >= hyg.config["time"]:
break
while True:
if hyg.try_create_order():
if "hunter" not in hyg.config:
hyg.sdk.capture_message("Pay success!")
logger.success("购票成功!")
return
else:
hyg.config['hunter'] += 1
save(hyg.config)
logger.success(f"猎手,你的战绩:{hyg.config['hunter']}张")
def main():
easter_egg = False
print(i18n["zh"]["start_up"])
global uid
try:
version, sentry_sdk = init()
session = requests.session()
check_update(version)
config = load_config()
headers = {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/618.1.15.10.15 (KHTML, like Gecko) Mobile/21F90 BiliApp/77900100 os/ios model/iPhone 15 mobi_app/iphone build/77900100 osVer/17.5.1 network/2 channel/AppStore c_locale/zh-Hans_CN s_locale/zh-Hans_CH disable_rcmd/0",
"Cookie": config["cookie"],
}
if "user-agent" in config:
headers["User-Agent"] = config["user-agent"]
session = requests.Session()
if "mode" not in config:
mode_str = prompt([inquirer.List("mode", message=i18n["zh"]["choose_mode"], choices=[
i18n["zh"]["mode_time"], i18n["zh"]["mode_direct"], i18n["zh"]["mode_detect"]
], default=i18n["zh"]["mode_time"])])["mode"]
if mode_str == i18n["zh"]["mode_direct"]:
config["mode"] = 'direct'
logger.info(i18n["zh"]["mode_direct_on"])
elif mode_str == i18n["zh"]["mode_detect"]:
config["mode"] = 'detect'
logger.info(i18n["zh"]["mode_detect_on"])
else:
config["mode"] = 'time'
logger.info(i18n["zh"]["mode_time_on"])
if "status_delay" not in config and config["mode"] == 'detect':
config["status_delay"] = float(prompt([
inquirer.Text(
"status_delay",
message=i18n["zh"]["input_status_delay"],
default="0.2",
validate=lambda _, x: float(x) >= 0
)])["status_delay"])
if "proxy" not in config:
logger.info(i18n["zh"]["no_proxy_by_default"])
config["proxy"] = False
if "captcha" not in config:
logger.info(i18n["zh"]["captcha_mode_gt_by_default"])
config["captcha"] = "local_gt"
config["rrocr"] = None
kdl_client = None
if config["proxy"] == True:
auth = kdl.Auth(config["proxy_auth"][0], config["proxy_auth"][1])
kdl_client = kdl.Client(auth)
session.proxies = {
"http": config["proxy_auth"][2],
"https": config["proxy_auth"][2],
}
if config["proxy_channel"] != "0":
headers["kdl-tps-channel"] = config["proxy_channel"]
session.keep_alive = False
session.get("https://show.bilibili.com")
logger.info(
i18n["zh"]["test_proxy"].format(kdl_client.tps_current_ip(sign_type="hmacsha1"))
)
if "again" not in config:
choice = prompt([inquirer.List("again", message=i18n["zh"]["input_is_allow_again"],
choices=[i18n["zh"]["yes"], i18n["zh"]["no"]], default=i18n["zh"]["yes"])])[
"again"]
if choice == i18n["zh"]["no"]:
config["again"] = False
else:
config["again"] = True
if (
"project_id" not in config
or "screen_id" not in config
or "sku_id" not in config
or "pay_money" not in config
or "id_bind" not in config
):
while True:
logger.info(i18n["zh"]["common_project_id"])
for i in range(len(common_project_id)):
logger.info(
common_project_id[i]["name"]
+ " id: "
+ str(common_project_id[i]["id"])
)
if len(common_project_id) == 0:
logger.info(i18n["zh"]["empty"])
config["project_id"] = prompt([
inquirer.Text("project_id", message=i18n["zh"]["input_project_id"],
validate=lambda _, x: x.isdigit())
])["project_id"]
url = (
"https://show.bilibili.com/api/ticket/project/getV2?version=134&id="
+ config["project_id"]
)
response = session.get(url, headers=headers)
if response.status_code == 412:
logger.error(i18n["zh"]["not_handled_412"])
if config["proxy"]:
logger.info(
i18n["zh"]["manual_change_ip"].format(
kdl_client.change_tps_ip(sign_type="hmacsha1")
)
)
session.close()
response = response.json()
if response["errno"] == 3:
logger.error(i18n["zh"]["project_id_not_found"])
continue
if response["data"] == {}:
logger.error(i18n["zh"]["server_no_response"])
continue
if "screen_list" not in response['data']:
logger.error(i18n["zh"]["no_screen"])
continue
if len(response["data"]["screen_list"]) == 0:
logger.error(i18n["zh"]["no_screen"])
continue
break
logger.info(i18n["zh"]["project_name"].format(response["data"]["name"]))
config["id_bind"] = response["data"]["id_bind"]
config["is_paper_ticket"] = response["data"]["has_paper_ticket"]
screens = response["data"]["screen_list"]
screen_id = prompt([
inquirer.List("screen_id", message=i18n["zh"]["select_screen"],
choices=[f"{i}. {screens[i]['name']}" for i in range(len(screens))])
])["screen_id"].split(".")[0]
logger.info(i18n["zh"]["show_screen"].format(screens[int(screen_id)]["name"]))
tickets = screens[int(screen_id)]["ticket_list"] # type: ignore
sku_id = prompt([
inquirer.List("sku_id", message=i18n["zh"]["select_sku"],
choices=[f"{i}. {tickets[i]['desc']} {tickets[i]['price'] / 100}元" for i in
range(len(tickets))])
])["sku_id"].split(".")[0]
logger.info(i18n["zh"]["show_sku"].format(tickets[int(sku_id)]["desc"]))
config["screen_id"] = str(screens[int(screen_id)]["id"])
config["sku_id"] = str(tickets[int(sku_id)]["id"])
config["pay_money"] = str(tickets[int(sku_id)]["price"])
config["ticket_desc"] = str(tickets[int(sku_id)]["desc"])
config["time"] = int(tickets[int(sku_id)]["saleStart"])
if tickets[int(sku_id)]["discount_act"] is not None:
logger.info(i18n["zh"]["show_act"].format(tickets[int(sku_id)]["discount_act"]["act_id"]))
config["act_id"] = tickets[int(sku_id)]["discount_act"]["act_id"]
config["order_type"] = tickets[int(sku_id)]["discount_act"]["act_type"]
else:
config["order_type"] = "1"
if config["is_paper_ticket"]:
if response["data"]["express_free_flag"]:
config["express_fee"] = 0
else:
config["express_fee"] = response["data"]["express_fee"]
url = "https://show.bilibili.com/api/ticket/addr/list"
resp_ticket = session.get(url, headers=headers)
if resp_ticket.status_code == 412:
logger.error(i18n["zh"]["not_handled_412"])
if config["proxy"]:
logger.info(
i18n["zh"]["manual_change_ip"].format(
kdl_client.change_tps_ip(sign_type="hmacsha1")
)
)
session.close()
addr_list = resp_ticket.json()["data"]["addr_list"]
if len(addr_list) == 0:
logger.error("没有收货地址,请先添加收货地址")
else:
addr = prompt([
inquirer.List("addr", message="请选择收货地址", choices=[f"{i}. {addr_list[i]['prov'] + addr_list[i]['city'] + addr_list[i]['area'] + addr_list[i]['addr']} {addr_list[i]['name']} {addr_list[i]['phone']}" for i in range(len(addr_list))])
])["addr"].split(".")[0]
addr = addr_list[int(addr)]
logger.info(
f"已选择收货地址:{addr['prov'] + addr['city'] + addr['area'] + addr['addr']} {addr['name']} {addr['phone']}"
)
config["deliver_info"] = json.dumps(
{
"name": addr["name"],
"tel": addr["phone"],
"addr_id": addr["addr"],
"addr": addr["prov"]
+ addr["city"]
+ addr["area"]
+ addr["addr"],
},
ensure_ascii=False,
)
logger.debug(
"您的screen_id 和 sku_id 和 pay_money 分别为:"
+ config["screen_id"]
+ " "
+ config["sku_id"]
+ " "
+ config["pay_money"]
)
logger.debug("您的开始销售时间为:" + str(config["time"]))
if config["id_bind"] != 0 and ("buyer_info" not in config):
url = "https://show.bilibili.com/api/ticket/buyer/list"
response = session.get(url, headers=headers)
if response.status_code == 412:
logger.error(i18n["zh"]["not_handled_412"])
buyer_infos = response.json()["data"]["list"]
config["buyer_info"] = []
if len(buyer_infos) == 0:
logger.error(i18n["zh"]["buyer_empty"])
return
else:
multiselect = True
if config["id_bind"] == 1:
logger.info(i18n["zh"]["id_bind_single"])
multiselect = False
if multiselect:
buyerids = prompt([
inquirer.Checkbox(
"buyerids",
message=i18n["zh"]["select_buyer"],
choices=[
f"{i}. {buyer_infos[i]['name']} {buyer_infos[i]['personal_id']} {buyer_infos[i]['tel']}" for
i in range(len(buyer_infos))],
validate=lambda _, x: len(x) > 0
)
])["buyerids"]
buyerids = [int(i.split(".")[0]) for i in buyerids]
config["buyer_info"] = []
female = False
male = False
for select in buyerids:
config["buyer_info"].append(
buyer_infos[int(select)]
)
logger.info(
i18n["zh"]["selected_buyer"].format(
buyer_infos[int(select)]["name"],
buyer_infos[int(select)]["personal_id"],
buyer_infos[int(select)]["tel"],
)
)
else:
index = prompt([
inquirer.List("index", message=i18n["zh"]["select_buyer"], choices=[
f"{i}. {buyer_infos[i]['name']} {buyer_infos[i]['personal_id']} {buyer_infos[i]['tel']}" for i
in range(len(buyer_infos))])
])["index"]
config["buyer_info"].append(buyer_infos[int(index.split(".")[0])])
logger.info(
i18n["zh"]["selected_buyer"].format(
buyer_infos[int(index.split(".")[0])]["name"],
buyer_infos[int(index.split(".")[0])]["personal_id"],
buyer_infos[int(index.split(".")[0])]["tel"],
)
)
if "count" not in config:
config["count"] = len(config["buyer_info"])
config["buyer_info"] = json.dumps(config["buyer_info"])
if config["id_bind"] == 0 and (
"buyer" not in config or "tel" not in config
):
logger.info("请添加联系人信息")
config["buyer"] = input("联系人姓名:")
config["tel"] = prompt([
inquirer.Text("tel", message="联系人手机号", validate=lambda _, x: len(x) == 11)
])["tel"]
if "count" not in config:
config["count"] = prompt([
inquirer.Text("count", message="请输入票数", default="1",
validate=lambda _, x: x.isdigit() and int(x) > 0)
])["count"]
if config["is_paper_ticket"]:
if config["express_fee"] == 0:
config["all_price"] = int(config["pay_money"]) * int(
config["count"]
)
logger.info(
f"共 {config['count']} 张 {config['ticket_desc']} 票,单张价格为 {int(config['pay_money']) / 100},纸质票,邮费免去,总价为{config['all_price'] / 100}"
)
else:
config["all_price"] = (
int(config["pay_money"]) * int(config["count"])
+ config["express_fee"]
)
logger.info(
f"共 {config['count']} 张 {config['ticket_desc']} 票,单张价格为 {int(config['pay_money']) / 100},纸质票,邮费为 {config['express_fee'] / 100},总价为{config['all_price'] / 100}"
)
else:
config["all_price"] = int(config["pay_money"]) * int(
config["count"]
)
logger.info(
i18n["zh"]["show_all_price_e_ticket"].format(
config["count"],
config["ticket_desc"],
int(config["pay_money"]) / 100,
config["all_price"] / 100,
)
)
save(config)
sentry_sdk.capture_message("config complete")
BHYG = BilibiliHyg(config, sentry_sdk, kdl_client, session)
BHYG.waited = True
run(BHYG)
except KeyboardInterrupt:
logger.info(i18n["zh"]["exit_manual"])
return
except Exception as e:
track = sentry_sdk.capture_exception(e)
logger.error(i18n["zh"]["error_occured"].format(str(e), str(track)))
return
return
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logger.info(i18n["zh"]["exit_manual"])
from sentry_sdk import Hub
client = Hub.current.client
if client is not None:
client.close(timeout=2.0)
logger.info(i18n["zh"]["exit_sleep_15s"])
try:
time.sleep(15)
except KeyboardInterrupt:
pass