Skip to content

Commit

Permalink
feat: 优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Jun 14, 2024
1 parent 10c515a commit 3db492b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
37 changes: 23 additions & 14 deletions tab/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
from util.error import ERRNO_DICT, withTimeString
from util.order_qrcode import get_qrcode_url

isRunning = False

ways = ["手动", "使用 rrocr", "使用 CapSolver", "本地验证码(Amorter提供)"]
ways_detail = [NormalValidator(), RROCRValidator(), CapSolverValidator(), AmorterValidator()]


def go_tab():
isRunning = False

gr.Markdown("""
> **分享一下经验**
> - 抢票前,不要去提前抢还没有发售的票,会被b站封掉一段时间导致错过抢票的
Expand Down Expand Up @@ -59,11 +59,9 @@ def go_tab():
select_way = 0

def choose_option(way):
global select_way
nonlocal select_way
select_way = way
# loguru.logger.info(way)
validator = ways_detail[select_way]
if validator.need_api_key():
if ways_detail[select_way].need_api_key():
return gr.update(visible=True)
else:
return gr.update(visible=False)
Expand Down Expand Up @@ -100,9 +98,7 @@ def choose_option(way):
validate_con = threading.Condition()

def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_key):
global isRunning, geetest_validate, geetest_seccode
global gt
global challenge
nonlocal geetest_validate, geetest_seccode, gt, challenge, isRunning
isRunning = True
left_time = total_attempts

Expand Down Expand Up @@ -145,6 +141,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
logger.info(f"prepare: {request_result}")
code = int(request_result["code"])
if code == -401:
# if True:
_url = "https://api.bilibili.com/x/gaia-vgate/v1/register"
_payload = urlencode(request_result["data"]["ga_data"]["riskParams"])
_data = _request.post(_url, _payload).json()
Expand All @@ -154,9 +151,20 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gt = _data["data"]["geetest"]["gt"]
challenge = _data["data"]["geetest"]["challenge"]
token = _data["data"]["token"]
validator = ways_detail[select_way]
# Fake test START --------------------------------
# test_res = _request.get(
# "https://passport.bilibili.com/x/passport-login/captcha?source=main_web"
# ).json()
# challenge = test_res["data"]["geetest"]["challenge"]
# gt = test_res["data"]["geetest"]["gt"]
# token = "123456"
# Fake test END --------------------------------

geetest_validate = ""
geetest_seccode = ""
try:
if validator.have_gt_ui():
if ways_detail[select_way].have_gt_ui():
logger.info(f"Using {ways_detail[select_way]}, have gt ui")
yield [
gr.update(value=withTimeString("进行验证码验证"), visible=True),
gr.update(visible=True),
Expand All @@ -168,9 +176,9 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
]

def run_validation():
global geetest_validate, geetest_seccode
nonlocal geetest_validate, geetest_seccode
try:
tmp = validator.validate(appkey=api_key, gt=gt, challenge=challenge)
tmp = ways_detail[select_way].validate(appkey=api_key, gt=gt, challenge=challenge)
except Exception as e:
return
validate_con.acquire()
Expand All @@ -197,6 +205,7 @@ def run_validation():
logger.info(
f"geetest_validate: {geetest_validate},geetest_seccode: {geetest_seccode}"
)
break
_url = "https://api.bilibili.com/x/gaia-vgate/v1/validate"
csrf = _request.cookieManager.get_cookies_value("bili_jct")
_payload = {
Expand Down Expand Up @@ -378,7 +387,7 @@ def run_validation():
)

def receive_geetest_result(res):
global geetest_validate, geetest_seccode
nonlocal geetest_validate, geetest_seccode
if "geetest_validate" in res and "geetest_seccode" in res:
validate_con.acquire()
geetest_validate = res["geetest_validate"]
Expand Down
14 changes: 10 additions & 4 deletions tab/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def train_tab():
select_way = 0

def choose_option(way):
global select_way
nonlocal select_way
select_way = way
# loguru.logger.info(way)
validator = ways_detail[select_way]
Expand Down Expand Up @@ -73,9 +73,15 @@ def choose_option(way):

geetest_result = gr.JSON(label="validate")
validate_con = threading.Condition()
test_challenge = ""
test_gt = ""
test_token = ""
test_csrf = ""
test_geetest_validate = ""
test_geetest_seccode = ""

def test_get_challenge(api_key):
global \
nonlocal \
test_challenge, \
test_gt, \
test_token, \
Expand Down Expand Up @@ -107,7 +113,7 @@ def test_get_challenge(api_key):
]

def run_validation():
global test_geetest_validate, test_geetest_seccode
nonlocal test_geetest_validate, test_geetest_seccode
try:
tmp = validator.validate(appkey=api_key, gt=test_gt, challenge=test_challenge)
except Exception as e:
Expand Down Expand Up @@ -184,7 +190,7 @@ def run_validation():
)

def receive_geetest_result(res):
global test_geetest_validate, test_geetest_seccode
nonlocal test_geetest_validate, test_geetest_seccode
if "geetest_validate" in res and "geetest_seccode" in res:
validate_con.acquire()
test_geetest_validate = res["geetest_validate"]
Expand Down

0 comments on commit 3db492b

Please sign in to comment.