Skip to content

Commit

Permalink
fix: 自动配置驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Apr 24, 2024
1 parent f7007f6 commit 8b9db9b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

if __name__ == "__main__":
logger.add("app_{time}.log")
logger.add("app.log")

with gr.Blocks(head=short_js) as demo:
gr.Markdown(header)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ requests~=2.31.0
setuptools~=65.5.1
gradio~=4.27.0
qrcode~=7.4.2
loguru
loguru~=0.7.2
22 changes: 13 additions & 9 deletions tab/go.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from datetime import datetime
import json
import time
from loguru import logger
import qrcode
from datetime import datetime
from urllib.parse import urlencode

import gradio as gr
import qrcode
from loguru import logger

from common import format_dictionary_to_string
from config import cookies_config_path
from util.bili_request import BiliRequest
from util.error import ERRNO_DICT
from util.order_qrcode import get_qrcode_url


isRunning = False
gt = ""
challenge = ""
geetest_validate = ""
geetest_seccode = ""


def start_go(tickets_info_str, time_start, interval, mode, total_attempts):
global isRunning, geetest_validate, geetest_seccode
global gt
Expand All @@ -31,8 +32,8 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts):
try:
if time_start != "":
time_difference = (
datetime.strptime(time_start, "%Y-%m-%dT%H:%M").timestamp()
- time.time()
datetime.strptime(time_start, "%Y-%m-%dT%H:%M").timestamp()
- time.time()
)
if time_difference > 0:
logger.info("等待中")
Expand Down Expand Up @@ -138,7 +139,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts):
)
yield [
gr.update(
value=f"正在抢票,具体情况查看终端控制台。\n剩余次数: {left_time_str} 当前状态码: {errno}({ERRNO_DICT.get(errno, "未知错误码")})",
value=f"正在抢票,具体情况查看终端控制台。\n剩余次数: {left_time_str} 当前状态码: {errno}({ERRNO_DICT.get(errno, '未知错误码')})",
visible=True,
),
gr.update(visible=True),
Expand Down Expand Up @@ -193,6 +194,8 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts):
gr.update(),
gr.update(),
]


def go_tab():
with gr.Column():
ticket_ui = gr.TextArea(
Expand Down Expand Up @@ -294,12 +297,13 @@ def go_tab():
outputs=geetest_result,
js="() => captchaObj.getValidate()",
)

def receive_geetest_result(res):
global geetest_validate, geetest_seccode
geetest_validate = res["geetest_validate"]
geetest_seccode = res["geetest_seccode"]
geetest_result.change(fn=receive_geetest_result, inputs=geetest_result)

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

go_btn.click(
fn=None,
Expand Down Expand Up @@ -327,4 +331,4 @@ def stop():
fn=stop,
inputs=None,
outputs=[go_ui, stop_btn, qr_image, gt_row],
)
)
11 changes: 6 additions & 5 deletions util/config_util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import time
import requests

import requests
from loguru import logger
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager


class CookieManager:
Expand All @@ -17,14 +19,14 @@ def __init__(self, config_file_path):

@logger.catch
def _login_and_save_cookies(
self, login_url="https://show.bilibili.com/platform/home.html"
self, login_url="https://show.bilibili.com/platform/home.html"
):
logger.info("启动浏览器中.....")
try:
self.driver = webdriver.Chrome()
self.driver = webdriver.Edge(EdgeChromiumDriverManager().install())
except Exception:
try:
self.driver = webdriver.Chrome()
self.driver = webdriver.Chrome(ChromeDriverManager().install())
except Exception:
raise Exception(
"没有找到浏览器驱动,请根据自己的浏览器下载相应的驱动:\n"
Expand Down Expand Up @@ -60,7 +62,6 @@ def get_cookies(self):
self.config = json.load(f)
except Exception:
return self._login_and_save_cookies()

if self.config == {}:
return self._login_and_save_cookies()
else:
Expand Down

0 comments on commit 8b9db9b

Please sign in to comment.