Skip to content

Commit

Permalink
feat: 部分优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Apr 20, 2024
1 parent 3cd21a3 commit a1345f0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
12 changes: 7 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def onSubmitAll(ticket_number, ticket_info, people, people_buyer, address):


def start_go(tickets_info, time_start, interval, mode, total_attempts):
global isRunning
global isRunning, geetest_validate, geetest_seccode
global gt
global challenge
result = ""
Expand Down Expand Up @@ -130,7 +130,7 @@ def start_go(tickets_info, time_start, interval, mode, total_attempts):
# gt = "ac597a4506fee079629df5d8b66dd4fe"
yield [gr.update(value="进行验证码验证", visible=True), gr.update(), gr.update(),
gr.update(visible=True), gr.update(value=gt), gr.update(value=challenge)]
while geetest_validate == "" and geetest_seccode == "":
while geetest_validate == "" or geetest_seccode == "":
continue
logging.info(f"geetest_validate: {geetest_validate},geetest_seccode: {geetest_seccode}")
_url = "https://api.bilibili.com/x/gaia-vgate/v1/validate"
Expand All @@ -144,6 +144,8 @@ def start_go(tickets_info, time_start, interval, mode, total_attempts):
"validate": geetest_validate
}
_data = _request.get(_url, urlencode(_payload))
geetest_validate = ""
geetest_seccode = ""
if _data["code"] == 0:
logging.info("极验GeeTest认证 成功")
else:
Expand Down Expand Up @@ -180,7 +182,7 @@ def start_go(tickets_info, time_start, interval, mode, total_attempts):
qr_gen.add_data(qrcode_url)
qr_gen.make(fit=True)
qr_gen_image = qr_gen.make_image()
yield [gr.update(value="生成二维码"), gr.update(),
yield [gr.update(value="生成付款二维码"), gr.update(),
gr.update(value=qr_gen_image.get_image(), visible=True), gr.update(), gr.update(),
gr.update()]
time.sleep(interval / 1000.0)
Expand Down Expand Up @@ -279,12 +281,12 @@ def configure_global_logging():
qr_image = gr.Image(label="使用微信或者支付宝扫码支付", visible=False)

with gr.Row(visible=False) as gt_row:
gt_html_btn = gr.Button("存在验证码,点击开启,开启抢票验证码")
gt_html_btn = gr.Button("点击打开抢票验证码(请勿多点!!)")
gt_html_finish_btn = gr.Button("完成验证码后点此此按钮")

gt_html = gr.HTML(value="""
<div>
<label for="datetime">如何点击无效说明,获取验证码失败</label>
<label for="datetime">如何点击无效说明,获取验证码失败,请勿多点</label>
<div id="captcha">
</div>
</div>""", label="验证码")
Expand Down
90 changes: 90 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from urllib.parse import urlencode

import gradio as gr

from config import cookies_config_path
from util.BiliRequest import BiliRequest

_request = BiliRequest(cookies_config_path=cookies_config_path)
res = _request.get("https://passport.bilibili.com/x/passport-login/captcha?source=main_web").json()
challenge = res["data"]["geetest"]["challenge"]
gt = res["data"]["geetest"]["gt"]

token = res["data"]["token"]
_url = "https://api.bilibili.com/x/gaia-vgate/v1/validate"
csrf = _request.cookieManager.get_cookies_value("bili_jct")

short_js = """ <script
src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"
rel="external nofollow">
</script>
<script src="https://static.geetest.com/static/js/gt.0.4.9.js"></script>
"""
geetest_validate = ""
geetest_seccode = ""
with gr.Blocks(head=short_js) as demo:
gt_html_btn = gr.Button("开始")
gt_html_finish_btn = gr.Button("结束")
print("gt,challenge", gt, challenge)
gt_html_btn.click(fn=None, inputs=None, outputs=None,
js=f"""() => {{ initGeetest({{
gt: "{gt}",
challenge: "{challenge}",
offline: false,
new_captcha: true,
product: "popup",
width: "300px",
https: true
}}, function (captchaObj) {{
window.captchaObj = captchaObj;
captchaObj.appendTo('#captcha');
}})}}""")
geetest_validate_ui = gr.Textbox(visible=False)
geetest_seccode_ui = gr.Textbox(visible=False)

gt_html = gr.HTML(value="""
<div>
<label for="datetime">如何点击无效说明,获取验证码失败</label>
<div id="captcha">
</div>
</div>""", label="验证码")
gt_ui = gr.Textbox(visible=False)
challenge_ui = gr.Textbox(visible=False)

gt_html_finish_btn.click(None, None, geetest_validate_ui,
js='() => {return captchaObj.getValidate().geetest_validate}')
gt_html_finish_btn.click(None, None, geetest_seccode_ui,
js='() => {return captchaObj.getValidate().geetest_seccode}')


def doing():
while geetest_validate == "" or geetest_seccode == "":
continue
_payload = {
"challenge": challenge,
"token": token,
"seccode": geetest_seccode,
"csrf": csrf,
"validate": geetest_validate
}
_data = _request.get(_url, urlencode(_payload))
print(_data.json())


gt_html_finish_btn.click(doing)


def update_geetest_validate(x):
global geetest_validate
geetest_validate = x


def update_geetest_seccode(x):
global geetest_seccode
geetest_seccode = x


geetest_validate_ui.change(fn=update_geetest_validate, inputs=geetest_validate_ui, outputs=None)
geetest_seccode_ui.change(fn=update_geetest_seccode, inputs=geetest_seccode_ui, outputs=None)
# 运行应用
demo.launch()
4 changes: 2 additions & 2 deletions util/BiliRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def __init__(self, headers=None, cookies=None, cookies_config_path=""):
def get(self, url, data=None):
response = self.session.get(url, data=data, headers=self.headers)
response.raise_for_status()
if response.json()["msg"] == "请先登录":
if response.json().get("msg", "") == "请先登录":
self.headers['cookies'] = self.cookieManager.get_cookies_str_force()
self.get(url, data)
return response

def post(self, url, data=None):
response = self.session.post(url, data=data, headers=self.headers)
response.raise_for_status()
if response.json()["msg"] == "请先登录":
if response.json().get("msg", "") == "请先登录":
self.headers['cookies'] = self.cookieManager.get_cookies_str_force()
self.post(url, data)
return response
Expand Down

0 comments on commit a1345f0

Please sign in to comment.