-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoguessr.py
158 lines (123 loc) · 3.66 KB
/
geoguessr.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import json
from dataclasses import dataclass
from typing import List
from playwright.sync_api import Page
@dataclass
class Score:
amount: str
unit: str
percentage: float
@dataclass
class Distance:
meters: dict # {"amount": "xxx", "unit": "km"}
miles: dict # {"amount": "xxx", "unit": "miles"}
@dataclass
class Guess:
lat: float
lng: float
roundScore: Score
roundScoreInPercentage: float
roundScoreInPoints: int
distance: Distance
distanceInMeters: float
@dataclass
class Player:
totalScore: Score
totalDistance: Distance
totalDistanceInMeters: float
guesses: List[Guess]
@dataclass
class Round:
lat: float
lng: float
@dataclass
class Bounds:
min: dict # {"lat": float, "lng": float}
max: dict # {"lat": float, "lng": float}
@dataclass
class GameState:
token: str
player: Player
rounds: List[Round]
bounds: Bounds
def _parse_game_state(response_text: str) -> GameState:
data = json.loads(response_text)
player_data = data["player"]
player = Player(
totalScore=Score(**player_data["totalScore"]),
totalDistance=Distance(**player_data["totalDistance"]),
totalDistanceInMeters=player_data["totalDistanceInMeters"],
guesses=[
Guess(
lat=g["lat"],
lng=g["lng"],
roundScore=Score(**g["roundScore"]),
roundScoreInPercentage=g["roundScoreInPercentage"],
roundScoreInPoints=g["roundScoreInPoints"],
distance=Distance(**g["distance"]),
distanceInMeters=g["distanceInMeters"],
)
for g in player_data["guesses"]
],
)
rounds = [Round(lat=r["lat"], lng=r["lng"]) for r in data["rounds"]]
bounds = Bounds(**data["bounds"])
return GameState(
token=data["token"],
player=player,
rounds=rounds,
bounds=bounds,
)
def submit_guess(page: Page, game_token: str, lat: float, lng: float) -> GameState:
print(f"Submitting guess for {game_token=} at {lat=}, {lng=}")
api_context = page.request
data = {
"token": game_token,
"lat": lat,
"lng": lng,
"timedOut": False,
"stepsCount": 0,
}
response = api_context.post(
f"https://www.geoguessr.com/api/v3/games/{game_token}",
data=data,
)
if not response.ok:
raise Exception(
f"Failed to get game state. Status: {response.status}, Response: {response.text()}"
)
game_state = _parse_game_state(response.text())
return game_state
def get_game_state(page: Page, game_token: str) -> GameState:
api_context = page.request
response = api_context.get(
f"https://www.geoguessr.com/api/v3/games/{game_token}",
)
if not response.ok:
raise Exception(
f"Failed to get game state. Status: {response.status}, Response: {response.text()}"
)
game_state = _parse_game_state(response.text())
return game_state
def start_new_game(page: Page) -> str:
print("Starting new game")
settings = {
"map": "world",
"type": "standard",
"timeLimit": 0,
"forbidMoving": True,
"forbidZooming": False,
"forbidRotating": False,
}
api_context = page.request
response = api_context.post(
"https://www.geoguessr.com/api/v3/games",
data=settings,
)
if not response.ok:
raise Exception(
f"Failed to start game. Status: {response.status}, Response: {response.text()}"
)
game_token = response.json()["token"]
print(f"Started new game with {game_token=}")
return game_token