-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser_ops.py
89 lines (68 loc) · 2.55 KB
/
browser_ops.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import base64
import os
from playwright.sync_api import Page
import vlm
def take_screenshot(page: Page) -> str:
print("Taking screenshot")
screenshot_bytes = page.screenshot(type="jpeg")
screenshot_base64 = base64.b64encode(screenshot_bytes).decode("utf-8")
return screenshot_base64
def pan_right(page: Page) -> None:
print("Panning right")
page.keyboard.down("D")
page.wait_for_timeout(1000)
page.keyboard.up("D")
def zoom_in_screenshot(page: Page, obj: vlm.InterestingObject) -> str:
"""
Zooms in on an object, takes a screenshot, and zooms out back to the original view.
Geoguessr doesn't like a large zoom all at once, so we zoom in in smaller increments.
"""
print(f"Zooming in to see object {obj['name']} at {obj['x']}, {obj['y']}")
page.mouse.move(obj["x"], obj["y"])
zoom_amount = 250
zoom_steps = 2
# Zoom in with multiple smaller increments
for _ in range(zoom_steps):
page.mouse.wheel(0, -zoom_amount)
page.wait_for_timeout(200)
screenshot = take_screenshot(page)
# Zoom out with matching increments
for _ in range(zoom_steps):
page.mouse.wheel(0, zoom_amount)
page.wait_for_timeout(200)
return screenshot
def get_page(p) -> Page:
browser = p.chromium.launch(headless=False)
context = browser.new_context(
locale="en-US",
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36",
viewport={"width": 1024, "height": 1024},
)
ncfa_token = os.getenv("GEOGUESSR_NCFA")
if not ncfa_token:
raise ValueError("GEOGUESSR_NCFA environment variable must be set")
context.add_cookies(
[
{
"name": "_ncfa",
"value": ncfa_token,
"domain": ".geoguessr.com",
"path": "/",
"httpOnly": True,
"secure": True,
"sameSite": "Lax",
}
]
)
return context.new_page()
def start_round(page: Page) -> None:
# reload to get the latest round
page.reload()
page.wait_for_selector("text='Place your pin on the map'", timeout=10000)
# The keyboard controls aren't activated till the first mouse click.
# So click somewhere randomly on the page (which may move the map around), then go back to the starting point.
page.mouse.click(512, 512)
page.wait_for_timeout(1000)
# Press 'r' to reset to the starting point
page.keyboard.press("r")
page.wait_for_timeout(1000)