Skip to content

Commit

Permalink
feat: 手动验证码和Geetest并行处理
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Jun 12, 2024
1 parent c08b4a4 commit 07c355e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 70 deletions.
56 changes: 35 additions & 21 deletions tab/go.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import threading
import time
from datetime import datetime
from json import JSONDecodeError
Expand Down Expand Up @@ -48,7 +49,7 @@ def go_tab():
# 验证码选择

way_select_ui = gr.Radio(ways, label="过验证码的方式", info="详细说明请前往 `训练你的验证码速度`那一栏",
type="index")
type="index", value="手动")
api_key_input_ui = gr.Textbox(label="填写你的api_key",
value=global_cookieManager.get_config_value("appkey", ""),
visible=False)
Expand Down Expand Up @@ -117,6 +118,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
time.sleep(time_difference) # 等待到指定的开始时间
tickets_info = json.loads(tickets_info_str)
Expand Down Expand Up @@ -148,24 +150,30 @@ 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"]
yield [
gr.update(value=withTimeString("进行验证码验证"), visible=True),
gr.update(visible=True),
gr.update(),
gr.update(visible=True),
gr.update(value=gt),
gr.update(value=challenge),
gr.update(value="hello"),
]
try:
if select_way != 0:
# https://passport.bilibili.com/x/passport-login/captcha?source=main_web
def run_validation():
global geetest_validate, geetest_seccode
logger.info(f"{ways[select_way]}")
validator = ways_detail[select_way]
geetest_validate = validator.validate(appkey=api_key, gt=gt, challenge=challenge)
geetest_seccode = geetest_validate + "|jordan"

if select_way == 0:
# https://passport.bilibili.com/x/passport-login/captcha?source=main_web
yield [
gr.update(value=withTimeString("进行验证码验证"), visible=True),
gr.update(visible=True),
gr.update(),
gr.update(visible=True),
gr.update(value=gt),
gr.update(value=challenge),
]
while geetest_validate == "" or geetest_seccode == "":
continue
else:
logger.info(f"{ways[select_way]}")
validator = ways_detail[select_way]
geetest_validate = validator.validate(appkey=api_key, gt=gt, challenge=challenge)
geetest_seccode = geetest_validate + "|jordan"
threading.Thread(target=run_validation).start()
except NameError as err:
pass
while geetest_validate == "" or geetest_seccode == "":
continue
logger.info(
f"geetest_validate: {geetest_validate},geetest_seccode: {geetest_seccode}"
)
Expand Down Expand Up @@ -193,6 +201,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
continue
request_result = _request.post(
Expand Down Expand Up @@ -229,6 +238,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
if errno == 0:
qrcode_url = get_qrcode_url(
Expand All @@ -246,6 +256,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
break
if mode == 1:
Expand All @@ -261,6 +272,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
except Exception as e:
logger.exception(e)
Expand All @@ -271,6 +283,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
finally:
time.sleep(interval / 1000.0)
Expand All @@ -282,6 +295,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]

mode_ui.change(
Expand All @@ -308,7 +322,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
qr_image = gr.Image(label="使用微信或者支付宝扫码支付", visible=False, elem_classes="pay_qrcode")

with gr.Row(visible=False) as gt_row:
gt_html_btn = gr.Button("点击打开抢票验证码(请勿多点!!)")
trigger = gr.Textbox(visible=False)
gt_html_finish_btn = gr.Button("完成验证码后点此此按钮")
gr.HTML(
value="""
Expand All @@ -323,7 +337,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
time_tmp = gr.Textbox(visible=False)
gt_ui = gr.Textbox(visible=False)
challenge_ui = gr.Textbox(visible=False)
gt_html_btn.click(
trigger.change(
fn=None,
inputs=[gt_ui, challenge_ui],
outputs=None,
Expand Down Expand Up @@ -376,7 +390,7 @@ def stop():
go_btn.click(
fn=start_go,
inputs=[ticket_ui, time_tmp, interval_ui, mode_ui, total_attempts_ui, api_key_input_ui],
outputs=[go_ui, stop_btn, qr_image, gt_row, gt_ui, challenge_ui],
outputs=[go_ui, stop_btn, qr_image, gt_row, gt_ui, challenge_ui, trigger],
)
stop_btn.click(
fn=stop,
Expand Down
93 changes: 44 additions & 49 deletions tab/train.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
from urllib.parse import urlencode

import gradio as gr
Expand Down Expand Up @@ -32,7 +33,7 @@ def train_tab():
""")

# 验证码选择
way_select_ui = gr.Radio(ways, label="验证码", info="过验证码的方式", type="index")
way_select_ui = gr.Radio(ways, label="验证码", info="过验证码的方式", type="index", value="手动")
api_key_input_ui = gr.Textbox(label="api_key", value=_request.cookieManager.get_config_value("appkey", ""),
visible=False)
select_way = 0
Expand All @@ -52,7 +53,6 @@ def choose_option(way):
test_get_challenge_btn = gr.Button("开始测试")
test_log = gr.JSON(label="测试结果(验证码过期是正常现象)")
with gr.Row(visible=False) as test_gt_row:
test_gt_html_start_btn = gr.Button("点击打开抢票验证码(请勿多点!!)")
test_gt_html_finish_btn = gr.Button("完成验证码后点此此按钮")
gr.HTML(
value="""
Expand All @@ -65,6 +65,8 @@ def choose_option(way):
)
test_gt_ui = gr.Textbox(label="gt", visible=True)
test_challenge_ui = gr.Textbox(label="challenge", visible=True)
trigger_ui = gr.Textbox(label="trigger", visible=False)

geetest_result = gr.JSON(label="validate")

def test_get_challenge(api_key):
Expand All @@ -85,42 +87,51 @@ def test_get_challenge(api_key):
test_csrf = _request.cookieManager.get_cookies_value("bili_jct")
test_geetest_validate = ""
test_geetest_seccode = ""
if select_way == 0:
return [
gr.update(value=test_gt), # test_gt_ui
gr.update(value=test_challenge), # test_challenge_ui
gr.update(visible=True), # test_gt_row
gr.update(value="重新生成"), # test_get_challenge_btn
gr.update()
]
else:
validator = ways_detail[select_way]
test_geetest_validate = validator.validate(appkey=api_key, gt=test_gt, challenge=test_challenge)
test_geetest_seccode = test_geetest_validate + "|jordan"

_url = "https://api.bilibili.com/x/gaia-vgate/v1/validate"
_payload = {
"challenge": test_challenge,
"token": test_token,
"seccode": test_geetest_seccode,
"csrf": test_csrf,
"validate": test_geetest_validate,
}
test_data = _request.post(_url, urlencode(_payload))
return [
gr.update(value=test_gt), # test_gt_ui
gr.update(value=test_challenge), # test_challenge_ui
gr.update(visible=False), # test_gt_row
gr.update(value="重新生成"), # test_get_challenge_btn
gr.update(value=test_data.json())
]
yield [
gr.update(value=test_gt), # test_gt_ui
gr.update(value=test_challenge), # test_challenge_ui
gr.update(visible=True), # test_gt_row
gr.update(value="重新生成"), # test_get_challenge_btn
gr.update(),
gr.update(value="hello")
]
try:
if select_way != 0:
def run_validation():
global test_geetest_validate, test_geetest_seccode
validator = ways_detail[select_way]
test_geetest_validate = validator.validate(appkey=api_key, gt=test_gt, challenge=test_challenge)
test_geetest_seccode = test_geetest_validate + "|jordan"

threading.Thread(target=run_validation).start()
except NameError as err:
pass
while test_geetest_validate == "" or test_geetest_seccode == "":
continue
_url = "https://api.bilibili.com/x/gaia-vgate/v1/validate"
_payload = {
"challenge": test_challenge,
"token": test_token,
"seccode": test_geetest_seccode,
"csrf": test_csrf,
"validate": test_geetest_validate,
}
test_data = _request.post(_url, urlencode(_payload))
yield [
gr.update(value=test_gt), # test_gt_ui
gr.update(value=test_challenge), # test_challenge_ui
gr.update(visible=False), # test_gt_row
gr.update(value="重新生成"), # test_get_challenge_btn
gr.update(value=test_data.json()),
gr.update()
]

test_get_challenge_btn.click(
fn=test_get_challenge,
inputs=[api_key_input_ui],
outputs=[test_gt_ui, test_challenge_ui, test_gt_row, test_get_challenge_btn, test_log],
outputs=[test_gt_ui, test_challenge_ui, test_gt_row, test_get_challenge_btn, test_log, trigger_ui],
)
test_gt_html_start_btn.click(
trigger_ui.change(
fn=None,
inputs=[test_gt_ui, test_challenge_ui],
outputs=None,
Expand Down Expand Up @@ -153,19 +164,3 @@ def receive_geetest_result(res):
test_geetest_seccode = res["geetest_seccode"]

geetest_result.change(fn=receive_geetest_result, inputs=geetest_result)

def test_doing():
while test_geetest_validate == "" or test_geetest_seccode == "":
continue
_url = "https://api.bilibili.com/x/gaia-vgate/v1/validate"
_payload = {
"challenge": test_challenge,
"token": test_token,
"seccode": test_geetest_seccode,
"csrf": test_csrf,
"validate": test_geetest_validate,
}
test_data = _request.post(_url, urlencode(_payload))
yield gr.update(value=test_data.json())

test_gt_html_finish_btn.click(fn=test_doing, outputs=[test_log])

0 comments on commit 07c355e

Please sign in to comment.