-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokebot.py
175 lines (128 loc) · 6.22 KB
/
pokebot.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
# -*- coding: utf-8 -*-
import argparse
import codecs
import json
import logging
import math
import random
import requests
import time
from pgoapi import PGoApi
from pgoapi import utilities as util
from geopy import Point
from geopy.distance import vincenty
from s2sphere import Cell, CellId, LatLng
CONFIG = json.load(open("config.json", 'r'))
def main():
if 'debug' in CONFIG:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
logging.getLogger("requests").setLevel(logging.DEBUG)
logging.getLogger("pgoapi").setLevel(logging.DEBUG)
logging.getLogger("rpc_api").setLevel(logging.DEBUG)
parser = argparse.ArgumentParser()
args = parser.parse_args()
# load locale
pokemon_mapping = json.load(open("pokemon.en.json", 'r'))
# load cached pokemon
pokemons = json.load(open("known_pokemons.json", 'r'))
coords = create_hexagon(CONFIG['office']['latitude'], CONFIG['office']['longitude'])
# instantiate pgoapi
api = PGoApi()
countdown = time.time()
# run forever
while True:
print u"current login expires at {}".format(countdown)
if countdown <= time.time():
print u"logging in"
api.login(CONFIG['authentication']['auth_service'], CONFIG['authentication']['username'].encode('utf-8'), CONFIG['authentication']['password'].encode('utf-8'))
countdown = time.time() + random.random() * 3600 # will need to log back in within the next hour, randomly
else:
time.sleep(random.random() * 60) # randomly wait under one minute
# only keep pokemon that haven't expired yet
previous_n = len(pokemons)
pokemons = {encounter_id: pokemon for encounter_id, pokemon in pokemons.items() if pokemon['hides_at'] > time.time()}
json.dump(pokemons, open("known_pokemons.json", 'w'))
print u"removed {} expired pokemon out of {}".format(previous_n - len(pokemons), previous_n)
for coord in coords:
lat = coord[0]
lng = coord[1]
print u"searching around {}, {}".format(lat, lng)
cell_ids = get_cell_ids(lat, lng)
timestamps = [0,] * len(cell_ids)
api.set_position(lat, lng, 0) # provide player position on the earth
api.get_map_objects(latitude = util.f2i(lat), longitude = util.f2i(lng), since_timestamp_ms = timestamps, cell_id = cell_ids)
time.sleep(1)
response_dict = api.call()
if 'status' in response_dict['responses']['GET_MAP_OBJECTS']:
if response_dict['responses']['GET_MAP_OBJECTS']['status'] == 1:
for map_cell in response_dict['responses']['GET_MAP_OBJECTS']['map_cells']:
if 'wild_pokemons' in map_cell:
for pokemon in map_cell['wild_pokemons']:
if str(pokemon['encounter_id']) not in pokemons:
pokemon['hides_at'] = time.time() + pokemon['time_till_hidden_ms'] / 1000
pokemon['name'] = pokemon_mapping[str(pokemon['pokemon_data']['pokemon_id'])]
pokemons[str(pokemon['encounter_id'])] = pokemon
json.dump(pokemons, open("known_pokemons.json", 'w'))
alert_slack(pokemon)
save_pokemon(pokemon)
def alert_slack(pokemon):
feet_from_origin = vincenty((CONFIG['office']['latitude'], CONFIG['office']['longitude']), (pokemon['latitude'], pokemon['longitude'])).feet
blocks_from_origin = int(math.ceil(feet_from_origin / 264.0)) # 20 blocks to a mile, so 264 feet is a block
time_left = int((pokemon['hides_at'] - time.time()) / 60)
payload = {'text': u'A {name} is within <http://maps.google.com/?q={lat},{lng}|{distance} block(s)> (:dash: in {time_left} minutes).'.format(name=pokemon['name'], distance=blocks_from_origin, time_left=time_left, lat=pokemon['latitude'], lng=pokemon['longitude'])}
print payload['text']
payload['username'] = "Pokébot"
payload['icon_emoji'] = ":slowpoke:"
if blocks_from_origin <= 1:
payload['channel'] = CONFIG['prod_channel']
else:
payload['channel'] = CONFIG['test_channel']
payload_json = dict(payload=json.dumps(payload))
response = requests.post(CONFIG['webhook_url'], data=payload_json)
def save_pokemon(pokemon):
with codecs.open('pokemon.csv', 'a', 'utf-8') as log_file:
log_file.write(u"{encounter_id}, {spawnpoint_id}, {name}, {hides_at}, {lat}, {lng}\n".format(
encounter_id=pokemon['encounter_id'],
spawnpoint_id=pokemon['spawnpoint_id'],
name=pokemon['name'],
hides_at=pokemon['hides_at'],
lat=pokemon['latitude'],
lng=pokemon['longitude'],
))
def get_cell_ids(lat, long, radius = 10):
origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15)
# bounds(origin)
walk = [origin.id()]
right = origin.next()
left = origin.prev()
# Search around provided radius
for i in range(radius):
# bounds(right)
# bounds(left)
walk.append(right.id())
walk.append(left.id())
right = right.next()
left = left.prev()
# Return everything
return sorted(walk)
def bounds(cell_id):
cell = Cell(cell_id)
url_string = 'http://maps.googleapis.com/maps/api/staticmap?size=400x400&path='
coords = []
for i in range(4) + [0]:
point = cell.get_vertex(i)
coords.append("{},{}".format(LatLng.latitude(point).degrees, LatLng.longitude(point).degrees))
url_string += "|".join(coords)
print url_string
def create_hexagon(lat, lng):
"""
Provides new locations in a hexagon around the provided latitude and longitude
"""
bearings = [0, 60, 120, 180, 240, 300]
coords = [(lat, lng)]
for bearing in bearings:
point = vincenty(meters=100).destination((lat, lng), bearing)
coords.append((point.latitude, point.longitude))
return coords
if __name__ == '__main__':
main()