mirror of
https://github.com/halfwaystudent/douyin-sparkflow.git
synced 2026-09-05 07:27:53 +08:00
Sync deployed SparkFlow reliability updates
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import traceback
|
||||
@@ -12,6 +13,7 @@ from utils.config import DEBUG, Environment, get_environment
|
||||
|
||||
console = Console()
|
||||
PLAYWRIGHT_BROWSERS_PATH = "../chrome"
|
||||
DEFAULT_PROFILE_ROOT = "/opt/douyin-sparkflow/state/browser-profiles"
|
||||
|
||||
|
||||
def _local_browser_bundle_path():
|
||||
@@ -32,6 +34,38 @@ def configure_playwright_environment():
|
||||
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(bundle_path.resolve())
|
||||
|
||||
|
||||
def _headless_for(GUI=False):
|
||||
headless = not GUI
|
||||
if get_environment() == Environment.LOCAL and DEBUG:
|
||||
headless = False
|
||||
return headless
|
||||
|
||||
|
||||
def _browser_args():
|
||||
return [
|
||||
"--disable-dev-shm-usage",
|
||||
"--no-sandbox",
|
||||
]
|
||||
|
||||
|
||||
def sanitize_profile_name(value):
|
||||
raw = str(value or "").strip()
|
||||
if not raw:
|
||||
raw = "unknown"
|
||||
safe = re.sub(r"[^0-9A-Za-z._-]+", "_", raw)
|
||||
safe = safe.strip("._-") or "unknown"
|
||||
return safe[:80]
|
||||
|
||||
|
||||
def browser_profile_root(root=None):
|
||||
configured = (
|
||||
root
|
||||
or os.getenv("SPARKFLOW_BROWSER_PROFILE_ROOT")
|
||||
or DEFAULT_PROFILE_ROOT
|
||||
)
|
||||
return Path(configured)
|
||||
|
||||
|
||||
async def install_browser():
|
||||
try:
|
||||
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True)
|
||||
@@ -43,15 +77,11 @@ async def install_browser():
|
||||
async def get_browser(GUI=False):
|
||||
configure_playwright_environment()
|
||||
|
||||
headless = not GUI
|
||||
if get_environment() == Environment.LOCAL and DEBUG:
|
||||
headless = False
|
||||
|
||||
try:
|
||||
playwright = await async_playwright().start()
|
||||
browser = await playwright.chromium.launch(
|
||||
headless=headless,
|
||||
args=["--disable-dev-shm-usage"],
|
||||
headless=_headless_for(GUI),
|
||||
args=_browser_args(),
|
||||
)
|
||||
return playwright, browser
|
||||
except Exception as exc:
|
||||
@@ -61,3 +91,27 @@ async def get_browser(GUI=False):
|
||||
sys.exit(1)
|
||||
traceback.print_exc()
|
||||
raise
|
||||
|
||||
|
||||
async def get_persistent_browser_context(profile_name, GUI=False, root=None):
|
||||
configure_playwright_environment()
|
||||
|
||||
profile_dir = browser_profile_root(root) / sanitize_profile_name(profile_name)
|
||||
profile_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
try:
|
||||
playwright = await async_playwright().start()
|
||||
context = await playwright.chromium.launch_persistent_context(
|
||||
str(profile_dir),
|
||||
headless=_headless_for(GUI),
|
||||
viewport={"width": 1600, "height": 1000},
|
||||
args=_browser_args(),
|
||||
)
|
||||
return playwright, context, profile_dir
|
||||
except Exception as exc:
|
||||
if "Executable doesn't exist" in str(exc) and get_environment() != Environment.GITHUBACTION:
|
||||
console.print("[bold red]Playwright browser is missing.[/bold red]")
|
||||
await install_browser()
|
||||
sys.exit(1)
|
||||
traceback.print_exc()
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user