@@ -70,56 +70,97 @@ def render_worker_map():
70
70
))
71
71
72
72
73
- @app .route ('/' )
73
+ @app .get ('/' )
74
74
async def fullmap (request , html_map = render_map ()):
75
75
return html_map
76
76
77
77
78
- @app .route ('/data' )
79
- async def pokemon_data (request ):
80
- last_id = request .args .get ('last_id' , 0 )
81
- return json (await get_pokemarkers_async (last_id ))
82
-
83
-
84
- @app .route ('/gym_data' )
85
- async def gym_data (request ):
86
- return json (await get_gyms_async ())
87
-
88
-
89
- @app .route ('/spawnpoints' )
90
- async def spawn_points (request ):
91
- return json ([dict (x ) for x in await get_spawnpoints_async ()])
92
-
93
-
94
- @app .route ('/pokestops' )
95
- async def get_pokestops (request ):
96
- return json ([dict (x ) for x in await get_pokestops_async ()])
97
-
98
-
99
- @app .route ('/scan_coords' )
100
- async def scan_coords (request ):
101
- return json (get_scan_coords ())
102
-
103
-
104
78
if conf .MAP_WORKERS :
105
79
workers = Workers ()
106
80
107
81
108
- @app .route ('/workers_data' )
82
+ @app .get ('/workers_data' )
109
83
async def workers_data (request ):
110
84
return json (get_worker_markers (workers ))
111
85
112
86
113
- @app .route ('/workers' )
87
+ @app .get ('/workers' )
114
88
async def workers_map (request , html_map = render_worker_map ()):
115
89
return html_map
116
90
117
91
118
- def sighting_to_marker (pokemon , names = POKEMON , moves = MOVES , damage = DAMAGE ):
92
+ del env
93
+
94
+
95
+ @app .get ('/data' )
96
+ async def pokemon_data (request , _time = time ):
97
+ last_id = request .args .get ('last_id' , 0 )
98
+ async with app .pool .acquire () as conn :
99
+ results = await conn .fetch ('''
100
+ SELECT id, pokemon_id, expire_timestamp, lat, lon, atk_iv, def_iv, sta_iv, move_1, move_2
101
+ FROM sightings
102
+ WHERE expire_timestamp > {} AND id > {}
103
+ ''' .format (_time (), last_id ))
104
+ return json (list (map (sighting_to_marker , results )))
105
+
106
+
107
+ @app .get ('/gym_data' )
108
+ async def gym_data (request , names = POKEMON , _str = str ):
109
+ async with app .pool .acquire () as conn :
110
+ results = await conn .fetch ('''
111
+ SELECT
112
+ fs.fort_id,
113
+ fs.id,
114
+ fs.team,
115
+ fs.prestige,
116
+ fs.guard_pokemon_id,
117
+ fs.last_modified,
118
+ f.lat,
119
+ f.lon
120
+ FROM fort_sightings fs
121
+ JOIN forts f ON f.id=fs.fort_id
122
+ WHERE (fs.fort_id, fs.last_modified) IN (
123
+ SELECT fort_id, MAX(last_modified)
124
+ FROM fort_sightings
125
+ GROUP BY fort_id
126
+ )
127
+ ''' )
128
+ return json ([{
129
+ 'id' : 'fort-' + _str (fort ['fort_id' ]),
130
+ 'sighting_id' : fort ['id' ],
131
+ 'prestige' : fort ['prestige' ],
132
+ 'pokemon_id' : fort ['guard_pokemon_id' ],
133
+ 'pokemon_name' : names [fort ['guard_pokemon_id' ]],
134
+ 'team' : fort ['team' ],
135
+ 'lat' : fort ['lat' ],
136
+ 'lon' : fort ['lon' ]
137
+ } for fort in results ])
138
+
139
+
140
+ @app .get ('/spawnpoints' )
141
+ async def spawn_points (request , _dict = dict ):
142
+ async with app .pool .acquire () as conn :
143
+ results = await conn .fetch ('SELECT spawn_id, despawn_time, lat, lon, duration FROM spawnpoints' )
144
+ return json ([_dict (x ) for x in results ])
145
+
146
+
147
+ @app .get ('/pokestops' )
148
+ async def get_pokestops (request , _dict = dict ):
149
+ async with app .pool .acquire () as conn :
150
+ results = await conn .fetch ('SELECT external_id, lat, lon FROM pokestops' )
151
+ return json ([_dict (x ) for x in results ])
152
+
153
+
154
+ @app .get ('/scan_coords' )
155
+ async def scan_coords (request ):
156
+ return json (get_scan_coords ())
157
+
158
+
159
+ def sighting_to_marker (pokemon , names = POKEMON , moves = MOVES , damage = DAMAGE , trash = conf .TRASH_IDS , _str = str ):
119
160
pokemon_id = pokemon ['pokemon_id' ]
120
161
marker = {
121
- 'id' : 'pokemon-' + str (pokemon ['id' ]),
122
- 'trash' : pokemon_id in conf . TRASH_IDS ,
162
+ 'id' : 'pokemon-' + _str (pokemon ['id' ]),
163
+ 'trash' : pokemon_id in trash ,
123
164
'name' : names [pokemon_id ],
124
165
'pokemon_id' : pokemon_id ,
125
166
'lat' : pokemon ['lat' ],
@@ -139,70 +180,16 @@ def sighting_to_marker(pokemon, names=POKEMON, moves=MOVES, damage=DAMAGE):
139
180
return marker
140
181
141
182
142
- async def get_pokemarkers_async (after_id ):
143
- async with create_pool (** conf .DB ) as pool :
144
- async with pool .acquire () as conn :
145
- async with conn .transaction ():
146
- results = await conn .fetch ('''
147
- SELECT id, pokemon_id, expire_timestamp, lat, lon, atk_iv, def_iv, sta_iv, move_1, move_2
148
- FROM sightings
149
- WHERE expire_timestamp > {ts} AND id > {poke_id}
150
- ''' .format (ts = time (), poke_id = after_id ))
151
- return tuple (map (sighting_to_marker , results ))
152
-
153
-
154
- async def get_gyms_async (names = POKEMON ):
155
- async with create_pool (** conf .DB ) as pool :
156
- async with pool .acquire () as conn :
157
- async with conn .transaction ():
158
- results = await conn .fetch ('''
159
- SELECT
160
- fs.fort_id,
161
- fs.id,
162
- fs.team,
163
- fs.prestige,
164
- fs.guard_pokemon_id,
165
- fs.last_modified,
166
- f.lat,
167
- f.lon
168
- FROM fort_sightings fs
169
- JOIN forts f ON f.id=fs.fort_id
170
- WHERE (fs.fort_id, fs.last_modified) IN (
171
- SELECT fort_id, MAX(last_modified)
172
- FROM fort_sightings
173
- GROUP BY fort_id
174
- )
175
- ''' )
176
- return [{
177
- 'id' : 'fort-' + str (fort ['fort_id' ]),
178
- 'sighting_id' : fort ['id' ],
179
- 'prestige' : fort ['prestige' ],
180
- 'pokemon_id' : fort ['guard_pokemon_id' ],
181
- 'pokemon_name' : names [fort ['guard_pokemon_id' ]],
182
- 'team' : fort ['team' ],
183
- 'lat' : fort ['lat' ],
184
- 'lon' : fort ['lon' ]
185
- } for fort in results ]
186
-
187
-
188
- async def get_spawnpoints_async ():
189
- async with create_pool (** conf .DB ) as pool :
190
- async with pool .acquire () as conn :
191
- async with conn .transaction ():
192
- return await conn .fetch ('SELECT spawn_id, despawn_time, lat, lon, duration FROM spawnpoints' )
193
-
194
-
195
- async def get_pokestops_async ():
196
- async with create_pool (** conf .DB ) as pool :
197
- async with pool .acquire () as conn :
198
- async with conn .transaction ():
199
- return await conn .fetch ('SELECT external_id, lat, lon FROM pokestops' )
183
+ @app .listener ('before_server_start' )
184
+ async def register_db (app , loop ):
185
+ app .pool = await create_pool (** conf .DB , loop = loop )
200
186
201
187
202
188
def main ():
203
189
args = get_args ()
204
190
app .run (debug = args .debug , host = args .host , port = args .port )
205
191
192
+
206
193
if __name__ == '__main__' :
207
194
main ()
208
195
0 commit comments