Spaces:
Running
Running
Update app/lens_images_core.py
Browse files- app/lens_images_core.py +20 -9
app/lens_images_core.py
CHANGED
|
@@ -17,9 +17,7 @@ if not LOGGER.handlers:
|
|
| 17 |
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
| 18 |
)
|
| 19 |
|
| 20 |
-
COOKIE_JSON_URL = os.getenv("COOKIE_JSON_URL")
|
| 21 |
-
if not COOKIE_JSON_URL:
|
| 22 |
-
raise RuntimeError("Missing COOKIE_JSON_URL secret. Set it in Space Settings > Secrets.")
|
| 23 |
UA = "Mozilla/5.0 (Lens OCR Images)"
|
| 24 |
|
| 25 |
_COMMON_CHROME_PATHS = [
|
|
@@ -52,11 +50,12 @@ def _find_chrome_binary() -> str | None:
|
|
| 52 |
return None
|
| 53 |
|
| 54 |
def _build_chrome() -> webdriver.Chrome:
|
| 55 |
-
bin_loc = _find_chrome_binary() or "/usr/bin/chromium"
|
| 56 |
-
drv_path = os.getenv("CHROMEDRIVER", "/usr/bin/chromedriver")
|
| 57 |
opts = ChromeOptions()
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
extra = os.getenv(
|
| 61 |
"CHROME_EXTRA_ARGS",
|
| 62 |
"--disable-gpu --no-sandbox --disable-dev-shm-usage --window-size=1920,1080 --headless=new",
|
|
@@ -64,8 +63,20 @@ def _build_chrome() -> webdriver.Chrome:
|
|
| 64 |
for a in extra:
|
| 65 |
if a:
|
| 66 |
opts.add_argument(a)
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
_cached_cookie_obj: Dict[str, Any] | None = None
|
| 71 |
_cached_cookie_fetched_at: float = 0.0
|
|
|
|
| 17 |
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
| 18 |
)
|
| 19 |
|
| 20 |
+
COOKIE_JSON_URL = os.getenv("COOKIE_JSON_URL", "")
|
|
|
|
|
|
|
| 21 |
UA = "Mozilla/5.0 (Lens OCR Images)"
|
| 22 |
|
| 23 |
_COMMON_CHROME_PATHS = [
|
|
|
|
| 50 |
return None
|
| 51 |
|
| 52 |
def _build_chrome() -> webdriver.Chrome:
|
|
|
|
|
|
|
| 53 |
opts = ChromeOptions()
|
| 54 |
+
|
| 55 |
+
bin_loc = _find_chrome_binary()
|
| 56 |
+
if bin_loc:
|
| 57 |
+
opts.binary_location = bin_loc
|
| 58 |
+
|
| 59 |
extra = os.getenv(
|
| 60 |
"CHROME_EXTRA_ARGS",
|
| 61 |
"--disable-gpu --no-sandbox --disable-dev-shm-usage --window-size=1920,1080 --headless=new",
|
|
|
|
| 63 |
for a in extra:
|
| 64 |
if a:
|
| 65 |
opts.add_argument(a)
|
| 66 |
+
|
| 67 |
+
drv_path = os.getenv("CHROMEDRIVER")
|
| 68 |
+
if drv_path and os.path.exists(drv_path):
|
| 69 |
+
return webdriver.Chrome(service=ChromeService(executable_path=drv_path), options=opts)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
installed_path = chromedriver_autoinstaller.install()
|
| 73 |
+
if installed_path and os.path.exists(installed_path):
|
| 74 |
+
return webdriver.Chrome(service=ChromeService(executable_path=installed_path), options=opts)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
LOGGER.warning("chromedriver_autoinstaller failed: %s; will try Selenium Manager", e)
|
| 77 |
+
|
| 78 |
+
return webdriver.Chrome(options=opts)
|
| 79 |
+
|
| 80 |
|
| 81 |
_cached_cookie_obj: Dict[str, Any] | None = None
|
| 82 |
_cached_cookie_fetched_at: float = 0.0
|