forked from benx1n/wows-stats-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.py
68 lines (53 loc) · 1.78 KB
/
browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import Optional, AsyncIterator
from contextlib import asynccontextmanager
from sys import platform
from nonebot.log import logger
from playwright.async_api import Page, Error, Browser, Playwright, async_playwright
_browser: Optional[Browser] = None
_playwright: Optional[Playwright] = None
async def init(**kwargs) -> Browser:
global _browser
global _playwright
_playwright = await async_playwright().start()
try:
_browser = await launch_browser(**kwargs)
except Error:
await install_browser()
_browser = await launch_browser(**kwargs)
return _browser
async def launch_browser(**kwargs) -> Browser:
assert _playwright is not None, "Playwright is not initialized"
if "win" in platform.lower():
return await _playwright.chromium.launch(**kwargs)
else:
return await _playwright.firefox.launch(**kwargs)
async def get_browser(**kwargs) -> Browser:
return _browser or await init(**kwargs)
@asynccontextmanager
async def get_new_page(**kwargs) -> AsyncIterator[Page]:
browser = await get_browser()
page = await browser.new_page(**kwargs)
try:
yield page
finally:
await page.close()
async def shutdown_browser():
if _browser:
await _browser.close()
if _playwright:
await _playwright.stop() # type: ignore
async def install_browser():
import sys,os
from playwright.__main__ import main
if "win" in platform.lower():
logger.info("正在安装 chromium")
sys.argv = ["", "install", "chromium"]
else:
logger.info("正在安装 firefox")
sys.argv = ["", "install", "firefox"]
try:
logger.info("正在安装依赖")
os.system("playwright install-deps")
main()
except SystemExit:
pass