2024-08-12 15:38:37 +08:00
|
|
|
|
import os
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
2024-02-04 04:18:54 +08:00
|
|
|
|
from nonebot import get_driver
|
2024-08-12 15:38:37 +08:00
|
|
|
|
from playwright.__main__ import main
|
2024-02-04 04:18:54 +08:00
|
|
|
|
from playwright.async_api import Browser, Playwright, async_playwright
|
|
|
|
|
|
|
2024-08-24 17:06:23 +08:00
|
|
|
|
from zhenxun.configs.config import BotConfig
|
2024-02-25 03:18:34 +08:00
|
|
|
|
from zhenxun.services.log import logger
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
driver = get_driver()
|
|
|
|
|
|
|
2024-02-25 03:18:34 +08:00
|
|
|
|
_playwright: Playwright | None = None
|
|
|
|
|
|
_browser: Browser | None = None
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-08-15 20:27:19 +08:00
|
|
|
|
# @driver.on_startup
|
|
|
|
|
|
# async def start_browser():
|
|
|
|
|
|
# global _playwright
|
|
|
|
|
|
# global _browser
|
|
|
|
|
|
# install()
|
|
|
|
|
|
# await check_playwright_env()
|
|
|
|
|
|
# _playwright = await async_playwright().start()
|
|
|
|
|
|
# _browser = await _playwright.chromium.launch()
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-08-15 20:27:19 +08:00
|
|
|
|
# @driver.on_shutdown
|
|
|
|
|
|
# async def shutdown_browser():
|
|
|
|
|
|
# if _browser:
|
|
|
|
|
|
# await _browser.close()
|
|
|
|
|
|
# if _playwright:
|
|
|
|
|
|
# await _playwright.stop() # type: ignore
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-08-15 20:27:19 +08:00
|
|
|
|
# def get_browser() -> Browser:
|
|
|
|
|
|
# if not _browser:
|
|
|
|
|
|
# raise RuntimeError("playwright is not initalized")
|
|
|
|
|
|
# return _browser
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def install():
|
|
|
|
|
|
"""自动安装、更新 Chromium"""
|
|
|
|
|
|
|
2024-08-12 15:38:37 +08:00
|
|
|
|
def set_env_variables():
|
2024-08-15 20:27:19 +08:00
|
|
|
|
os.environ["PLAYWRIGHT_DOWNLOAD_HOST"] = (
|
|
|
|
|
|
"https://npmmirror.com/mirrors/playwright/"
|
|
|
|
|
|
)
|
2024-08-24 17:06:23 +08:00
|
|
|
|
if BotConfig.system_proxy:
|
|
|
|
|
|
os.environ["HTTPS_PROXY"] = BotConfig.system_proxy
|
2024-08-12 15:38:37 +08:00
|
|
|
|
|
|
|
|
|
|
def restore_env_variables():
|
|
|
|
|
|
os.environ.pop("PLAYWRIGHT_DOWNLOAD_HOST", None)
|
2024-08-24 17:06:23 +08:00
|
|
|
|
if BotConfig.system_proxy:
|
2024-08-12 15:38:37 +08:00
|
|
|
|
os.environ.pop("HTTPS_PROXY", None)
|
|
|
|
|
|
if original_proxy is not None:
|
|
|
|
|
|
os.environ["HTTPS_PROXY"] = original_proxy
|
|
|
|
|
|
|
|
|
|
|
|
def try_install_chromium():
|
|
|
|
|
|
try:
|
|
|
|
|
|
sys.argv = ["", "install", "chromium"]
|
|
|
|
|
|
main()
|
|
|
|
|
|
except SystemExit as e:
|
|
|
|
|
|
return e.code == 0
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("检查 Chromium 更新")
|
|
|
|
|
|
|
|
|
|
|
|
original_proxy = os.environ.get("HTTPS_PROXY")
|
|
|
|
|
|
set_env_variables()
|
|
|
|
|
|
|
|
|
|
|
|
success = try_install_chromium()
|
|
|
|
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
|
|
logger.info("Chromium 更新失败,尝试从原始仓库下载,速度较慢")
|
|
|
|
|
|
os.environ["PLAYWRIGHT_DOWNLOAD_HOST"] = ""
|
|
|
|
|
|
success = try_install_chromium()
|
|
|
|
|
|
|
|
|
|
|
|
restore_env_variables()
|
|
|
|
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
|
|
raise RuntimeError("未知错误,Chromium 下载失败")
|
|
|
|
|
|
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
2024-08-12 15:38:37 +08:00
|
|
|
|
async def check_playwright_env():
|
|
|
|
|
|
"""检查 Playwright 依赖"""
|
|
|
|
|
|
logger.info("检查 Playwright 依赖")
|
2024-02-04 04:18:54 +08:00
|
|
|
|
try:
|
2024-08-12 15:38:37 +08:00
|
|
|
|
async with async_playwright() as p:
|
|
|
|
|
|
await p.chromium.launch()
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
raise ImportError("加载失败,Playwright 依赖不全,") from e
|