first commit

This commit is contained in:
Your Name
2025-02-09 22:02:44 +08:00
commit 49fa4d6fd6
35 changed files with 3282 additions and 0 deletions

68
browser_utils.py Normal file
View File

@@ -0,0 +1,68 @@
from DrissionPage import ChromiumOptions, Chromium
import sys
import os
import logging
from config import Config
class BrowserManager:
def __init__(self):
self.browser = None
self.config = Config()
def init_browser(self, user_agent=None):
"""初始化浏览器"""
co = self._get_browser_options(user_agent)
self.browser = Chromium(co)
return self.browser
def _get_browser_options(self, user_agent=None):
"""获取浏览器配置"""
co = ChromiumOptions()
try:
extension_path = self._get_extension_path()
co.add_extension(extension_path)
except FileNotFoundError as e:
logging.warning(f"警告: {e}")
co.set_pref("credentials_enable_service", False)
co.set_argument("--hide-crash-restore-bubble")
# 从配置中获取代理设置
proxy = self.config.get_proxy() if hasattr(self.config, 'get_proxy') else None
if proxy:
co.set_proxy(proxy)
co.auto_port()
if user_agent:
co.set_user_agent(user_agent)
# 设置为无头模式
co.headless(True)
# 基础设置
co.set_argument("--no-sandbox")
co.set_argument("--disable-gpu")
co.set_argument("--disable-dev-shm-usage")
return co
def _get_extension_path(self):
"""获取插件路径"""
root_dir = os.getcwd()
extension_path = os.path.join(root_dir, "turnstilePatch")
if hasattr(sys, "_MEIPASS"):
extension_path = os.path.join(sys._MEIPASS, "turnstilePatch")
if not os.path.exists(extension_path):
raise FileNotFoundError(f"插件不存在: {extension_path}")
return extension_path
def quit(self):
"""关闭浏览器"""
if self.browser:
try:
self.browser.quit()
except:
pass