Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Nov 16, 2023
1 parent 9851da7 commit a22c40e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
26 changes: 25 additions & 1 deletion menu/TicketGrabbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
import time
import logging
from time import sleep

from requests import utils
from selenium import webdriver
from selenium.common import WebDriverException
from tkcalendar import DateEntry

from common import format_dictionary_to_string
from config import cookies_config_path, issue_please_text
from util.BiliRequest import BiliRequest
from tkinter import scrolledtext

from util.webUtil import WebUtil


class TicketGrabbingApp:
def __init__(self, master):
self.master = master
self.master.title("抢票配置")
self._request = BiliRequest(cookies_config_path=cookies_config_path)
self.webUtil = WebUtil(self._request.cookieManager.config)

# Left Frame for Configuration File
self.config_frame = ttk.LabelFrame(master, text="粘贴配置文件")
Expand Down Expand Up @@ -197,6 +202,25 @@ def grab_tickets(self, config_content, start_datetime, thread_count):
self.display_status(result)
break
continue
if not res.json()["data"]["shield"]["verifyMethod"]:

result = {"success": False, "status": f"遇到验证码:{res.json()['data']['shield']['naUrl']}"}
self.display_status(result)
try:
naUrl = res.json()["data"]["shield"]["naUrl"]
self.webUtil.driver.get(naUrl)
while True:
time.sleep(0.25)
try:
self.webUtil.driver.title
except WebDriverException:
break

except:
result = {"success": False,
"status": f"验证码错误"}
self.display_status(result)
break
config_content["token"] = res.json()["data"]["token"]

order_info = self._request.get(
Expand Down
4 changes: 2 additions & 2 deletions menu/TicketOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def onSubmitTicket(buy_info):
url=f"https://show.bilibili.com/api/ticket/order/prepare?project_id={projectId}", data=token_payload)
logging.info(f"res.text: {res.text}")
token = ""
if "token" in res.json()["data"]:
token = res.json()["data"]["token"]
# if "token" in res.json()["data"]:
# token = res.json()["data"]["token"]

order_info = _request.get(
url=f"https://show.bilibili.com/api/ticket/order/confirmInfo?token={token}&voucher=&project_id={projectId}")
Expand Down
8 changes: 4 additions & 4 deletions util/BiliRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class BiliRequest:
def __init__(self, headers=None, cookies=None, cookies_config_path=""):
self.session = requests.Session()
self._cookieManager = CookieManager(cookies_config_path)
self._cookies = self._cookieManager.get_cookies_str()
self.cookieManager = CookieManager(cookies_config_path)
self._cookies = self.cookieManager.get_cookies_str()
self.headers = headers or {
'authority': 'show.bilibili.com',
'accept': '*/*',
Expand All @@ -30,15 +30,15 @@ def get(self, url, params=None):
response = self.session.get(url, params=params, headers=self.headers)
response.raise_for_status()
if response.json()["msg"] == "请先登录":
self.headers['cookies'] = self._cookieManager.get_cookies_str_force()
self.headers['cookies'] = self.cookieManager.get_cookies_str_force()
self.get(url, params)
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"] == "请先登录":
self.headers['cookies'] = self._cookieManager.get_cookies_str_force()
self.headers['cookies'] = self.cookieManager.get_cookies_str_force()
self.post(url, data)
return response

Expand Down
4 changes: 4 additions & 0 deletions util/configUtil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import logging
import time

from requests import utils
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Expand All @@ -13,6 +15,8 @@ def __init__(self, config_file_path):
self.config = {}
self.config_file_path = config_file_path

self.cookie_jar = utils.cookiejar_from_dict(self.config)

def _login_and_save_cookies(self, login_url="https://show.bilibili.com/platform/home.html"):
logging.info("启动浏览器中.....")
self.driver = webdriver.Edge()
Expand Down
25 changes: 25 additions & 0 deletions util/webUtil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json
import logging
import threading
import time

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait


class WebUtil:
def __init__(self, cookie_dict):
self.initialized_event = threading.Event()

initialization_thread = threading.Thread(target=self.initialize, args=(cookie_dict,))
initialization_thread.start()

def initialize(self, cookie_dict):
self.driver = webdriver.Edge()
self.driver.get("https://show.bilibili.com/platform/home.html")

for cookie in cookie_dict["bilibili_cookies"]:
self.driver.add_cookie(cookie)
self.driver.refresh()
self.wait = WebDriverWait(self.driver, 0.5)
self.initialized_event.set()

0 comments on commit a22c40e

Please sign in to comment.