-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.py
247 lines (217 loc) · 7.34 KB
/
demo.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from getpass import getpass
import os
import time
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import http.client
import json
import requests
from PIL import Image
from io import BytesIO
import tensorflow as tf
import numpy
import gdown
import shutil
numberOfClasses = 256
def startNewGame(cookie):
conn = http.client.HTTPSConnection("www.geoguessr.com")
payload = json.dumps({
"map": "world",
"type": "standard",
"timeLimit": 0,
"forbidMoving": True,
"forbidZooming": False,
"forbidRotating": False,
"rounds": 5
})
headers = {
'authority': 'www.geoguessr.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'cookie': cookie
}
conn.request("POST", "/api/v3/games", payload, headers)
res = conn.getresponse()
data = res.read()
resString = data.decode("utf-8")
resString = resString[resString.index("token")+8:]
token = resString[:resString.index("\"")]
resString = resString[resString.index("\"rounds\""):]
resString = resString[resString.index("panoId")+9:]
panoId = resString[:resString.index("\"")]
panoId = bytes.fromhex(panoId).decode('utf-8')
return token, panoId
def makeDirs():
if not os.path.exists("image"):
os.makedirs("image")
for i in range(numberOfClasses):
if not os.path.exists("image/" + str(i)):
os.makedirs("image/" + str(i))
def getImageForKey(key):
img1 = getImage(0,0,2,key)
img2 = getImage(1,0,2,key)
img3 = getImage(2,0,2,key)
img4 = getImage(3,0,2,key)
img5 = getImage(0,1,2,key)
img6 = getImage(1,1,2,key)
img7 = getImage(2,1,2,key)
img8 = getImage(3,1,2,key)
fimg = get_concat_h_cut(img1,img2)
fimg = get_concat_h_cut(fimg,img3)
fimg = get_concat_h_cut(fimg,img4)
fimg2 = get_concat_h_cut(img5,img6)
fimg2 = get_concat_h_cut(fimg2,img7)
fimg2 = get_concat_h_cut(fimg2,img8)
finalImage = get_concat_v_cut(fimg,fimg2)
finalImage.save("image/0/0.png")
def getImage(x,y,zoom,key):
url = "https://streetviewpixels-pa.googleapis.com/v1/tile?cb_client=apiv3&panoid=" + key + "&output=tile&x="\
+ str(x) + "&y=" + str(y) + "&zoom=" + str(zoom) + "&nbt=0&fover=0"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
return img
def get_concat_h_cut(im1, im2):
dst = Image.new('RGB', (im1.width + im2.width, min(im1.height, im2.height)))
dst.paste(im1, (0, 0))
dst.paste(im2, (im1.width, 0))
return dst
def get_concat_v_cut(im1, im2):
dst = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height))
dst.paste(im1, (0, 0))
dst.paste(im2, (0, im1.height))
return dst
def getNextMove(token,cookie):
conn = http.client.HTTPSConnection("www.geoguessr.com")
payload = ''
headers = {
'authority': 'www.geoguessr.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'cookie': cookie,
'referer': 'https://www.geoguessr.com/game/' + token,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'x-client': 'web'
}
conn.request("GET", "/api/v3/games/"+token+"?client=web", payload, headers)
res = conn.getresponse()
data = res.read()
resString = data.decode("utf-8")
if "\"round\":5" in resString and "\"xpProgressions\":" in resString:
resString = resString[resString.index("\"round\":5")+10:]
for x in range(5):
resString = resString[resString.index("\"panoId\"")+10:]
panoId = resString[:resString.index("\"")]
panoId = bytes.fromhex(panoId).decode('utf-8')
resString = data.decode("utf-8")
resString = resString[resString.rindex("\"totalScore\""):]
resString = resString[resString.index("\"amount\"")+10:]
totalScore = resString[:resString.index("\"")]
totalScore = float(totalScore)
resString = data.decode("utf-8")
resString = resString[resString.rindex("\"panoId\"")+10:]
panoId = resString[:resString.index("\"")]
panoId = bytes.fromhex(panoId).decode('utf-8')
return totalScore, panoId
def makeMove(token,cookie,lat,lon):
conn = http.client.HTTPSConnection("www.geoguessr.com")
payload = json.dumps({
"token": "\"token\"",
"lat": lat,
"lng": lon,
"timedOut": False
})
headers = {
'authority': 'www.geoguessr.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'cookie': cookie,
'origin': 'https://www.geoguessr.com',
'referer': 'https://www.geoguessr.com/game/' + token,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'x-client': 'web'
}
conn.request("POST", ("/api/v3/games/"+token), payload, headers)
res = conn.getresponse()
def getCookie(email,password):
conn = http.client.HTTPSConnection("www.geoguessr.com")
payload = json.dumps({
"email": email,
"password": password
})
headers = {
'authority': 'www.geoguessr.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'origin': 'https://www.geoguessr.com',
'referer': 'https://www.geoguessr.com/signin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'x-client': 'web'
}
conn.request("POST", "/api/v3/accounts/signin", payload, headers)
res = conn.getresponse()
headersString = str(res.headers)
headersString = headersString[headersString.index("_ncfa="):]
cookie = headersString[:headersString.index(";")+1]
return cookie
def estimate(panoId):
getImageForKey(panoId)
predictions = model.predict(gg_generator,verbose=0)
class_dict=gg_generator.class_indices
rev_dict={}
for key, value in class_dict.items():
rev_dict[value]=key
modelPred = numpy.argmax(predictions[0])
modelPred = rev_dict[modelPred]
with open("points.csv", "r") as f:
lines = f.readlines()
line = lines[int(modelPred)]
line = line[line.index(",")+1:]
lat = float(line[:line.index(",")])
lon = float(line[line.index(",")+1:])
return lat,lon
def loadModel():
if os.path.exists("model/version.txt"):
version = open('model/version.txt', 'r').read()
if version == "3":
return tf.keras.models.load_model('model')
if os.path.exists("model"):
shutil.rmtree('model')
print("downloading model")
url = "https://drive.google.com/drive/folders/1kzsbAskGlcnjXxHzw90LWG0IvGfQeKSc?usp=sharing"
gdown.download_folder(url, use_cookies=False)
print("download complete")
return tf.keras.models.load_model('model')
makeDirs()
print('Enter your geoguessr email address:')
email = input()
password = getpass()
cookie = getCookie(email,password)
target_size = (1024, 2048)
gg_datagen = ImageDataGenerator(1./255)
gg_generator = gg_datagen.flow_from_directory( #todo stop using this
'image/',
target_size=target_size
)
model = loadModel()
sleepTime = 0.3 #lower or remove at your own risk, I have gotten suspended before
totalScore = 0
moves = 0
for i in range(100):
token, panoId = startNewGame(cookie)
for j in range(4):
lat, lon = estimate(panoId)
time.sleep(sleepTime)
makeMove(token,cookie,lat,lon)
score,panoId = getNextMove(token,cookie)
lat, lon = estimate(panoId)
time.sleep(sleepTime)
makeMove(token,cookie,lat,lon)
moves+=1
score,panoId = getNextMove(token,cookie)
totalScore += score
avg = totalScore / moves
url = "https://www.geoguessr.com/results/"+token
print("final score:",score, url, "\naverage score:",avg, "\ngames played:",(i+1),"\n")