-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_detect.py
More file actions
65 lines (56 loc) · 1.72 KB
/
browser_detect.py
File metadata and controls
65 lines (56 loc) · 1.72 KB
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
"""Detect and launch Chromium-based browsers with CDP enabled."""
import os
import socket
import subprocess
from pathlib import Path
_BROWSERS = [
{
"name": "Brave",
"paths": [
r"C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe",
os.path.expandvars(r"%LOCALAPPDATA%\BraveSoftware\Brave-Browser\Application\brave.exe"),
],
},
{
"name": "Chrome",
"paths": [
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
os.path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe"),
],
},
{
"name": "Edge",
"paths": [
r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe",
os.path.expandvars(r"%PROGRAMFILES%\Microsoft\Edge\Application\msedge.exe"),
],
},
]
USER_DATA_DIR = Path.home() / "AIguessr_Browser"
CDP_PORT = 9222
def detect_browsers() -> list[dict]:
found = []
for browser in _BROWSERS:
for p in browser["paths"]:
if Path(p).exists():
found.append({"name": browser["name"], "path": p})
break
return found
def launch_browser(path: str) -> subprocess.Popen:
args = [
path,
f"--remote-debugging-port={CDP_PORT}",
"--remote-allow-origins=*",
f"--user-data-dir={USER_DATA_DIR}",
"https://www.geoguessr.com",
]
return subprocess.Popen(args)
def is_cdp_available() -> bool:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
s.connect(("127.0.0.1", CDP_PORT))
s.close()
return True
except (ConnectionRefusedError, socket.timeout, OSError):
return False