Spaces:
Running
Running
Update app/lens_images_core.py
Browse files- app/lens_images_core.py +3 -19
app/lens_images_core.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
|
| 2 |
-
import os, json, time, hashlib, httpx, base64, re, asyncio, threading, shutil, logging
|
| 3 |
from typing import Dict, Any
|
| 4 |
from urllib.parse import urlparse
|
| 5 |
|
| 6 |
from selenium import webdriver
|
| 7 |
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
| 8 |
from selenium.webdriver.chrome.service import Service as ChromeService
|
|
|
|
| 9 |
|
| 10 |
LOGGER = logging.getLogger("lens_images_core")
|
| 11 |
if not LOGGER.handlers:
|
|
@@ -34,7 +35,6 @@ _PROFILE_DIRS = []
|
|
| 34 |
|
| 35 |
def _mk_profile_dir() -> str:
|
| 36 |
base = os.getenv("CHROME_PROFILE_BASE", tempfile.gettempdir())
|
| 37 |
-
# ผูกกับ PID + thread + UUID เพื่อไม่ชนกัน
|
| 38 |
p = os.path.join(base, f"chrome-profile-{os.getpid()}-{threading.get_ident()}-{uuid.uuid4().hex}")
|
| 39 |
os.makedirs(p, exist_ok=True)
|
| 40 |
_PROFILE_DIRS.append(p)
|
|
@@ -49,22 +49,6 @@ def _cleanup_profiles():
|
|
| 49 |
|
| 50 |
atexit.register(_cleanup_profiles)
|
| 51 |
|
| 52 |
-
def _find_chrome_binary() -> str | None:
|
| 53 |
-
env = os.getenv("CHROME_BINARY")
|
| 54 |
-
if env and shutil.which(env):
|
| 55 |
-
return env
|
| 56 |
-
for p in _COMMON_CHROME_PATHS:
|
| 57 |
-
if os.path.isfile(p) and os.access(p, os.X_OK):
|
| 58 |
-
return p
|
| 59 |
-
try:
|
| 60 |
-
import subprocess, shlex
|
| 61 |
-
out = subprocess.check_output(shlex.split("which google-chrome"), stderr=subprocess.DEVNULL).decode().strip()
|
| 62 |
-
if out:
|
| 63 |
-
return out
|
| 64 |
-
except Exception:
|
| 65 |
-
pass
|
| 66 |
-
return None
|
| 67 |
-
|
| 68 |
def _build_chrome() -> webdriver.Chrome:
|
| 69 |
opts = ChromeOptions()
|
| 70 |
|
|
@@ -86,7 +70,7 @@ def _build_chrome() -> webdriver.Chrome:
|
|
| 86 |
return webdriver.Chrome(service=ChromeService(executable_path=drv_path), options=opts)
|
| 87 |
return webdriver.Chrome(options=opts)
|
| 88 |
except SessionNotCreatedException as e:
|
| 89 |
-
|
| 90 |
profile_dir2 = _mk_profile_dir()
|
| 91 |
opts.arguments = [a for a in opts.arguments if not a.startswith("--user-data-dir=")]
|
| 92 |
opts.add_argument(f"--user-data-dir={profile_dir2}")
|
|
|
|
| 1 |
|
| 2 |
+
import os, json, time, hashlib, httpx, base64, re, asyncio, threading, shutil, logging, atexit, tempfile, uuid
|
| 3 |
from typing import Dict, Any
|
| 4 |
from urllib.parse import urlparse
|
| 5 |
|
| 6 |
from selenium import webdriver
|
| 7 |
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
| 8 |
from selenium.webdriver.chrome.service import Service as ChromeService
|
| 9 |
+
from selenium.common.exceptions import SessionNotCreatedException
|
| 10 |
|
| 11 |
LOGGER = logging.getLogger("lens_images_core")
|
| 12 |
if not LOGGER.handlers:
|
|
|
|
| 35 |
|
| 36 |
def _mk_profile_dir() -> str:
|
| 37 |
base = os.getenv("CHROME_PROFILE_BASE", tempfile.gettempdir())
|
|
|
|
| 38 |
p = os.path.join(base, f"chrome-profile-{os.getpid()}-{threading.get_ident()}-{uuid.uuid4().hex}")
|
| 39 |
os.makedirs(p, exist_ok=True)
|
| 40 |
_PROFILE_DIRS.append(p)
|
|
|
|
| 49 |
|
| 50 |
atexit.register(_cleanup_profiles)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def _build_chrome() -> webdriver.Chrome:
|
| 53 |
opts = ChromeOptions()
|
| 54 |
|
|
|
|
| 70 |
return webdriver.Chrome(service=ChromeService(executable_path=drv_path), options=opts)
|
| 71 |
return webdriver.Chrome(options=opts)
|
| 72 |
except SessionNotCreatedException as e:
|
| 73 |
+
LOGGER.warning("SessionNotCreated: %s; retry with a fresh profile dir", e)
|
| 74 |
profile_dir2 = _mk_profile_dir()
|
| 75 |
opts.arguments = [a for a in opts.arguments if not a.startswith("--user-data-dir=")]
|
| 76 |
opts.add_argument(f"--user-data-dir={profile_dir2}")
|