From 47505a608bc55ccdf9d02bbf98f37f0b117ab110 Mon Sep 17 00:00:00 2001 From: Modrzew Date: Fri, 22 Jul 2016 18:57:35 +0200 Subject: [PATCH 1/7] Support for trash pokemon, cleaning up web.py --- db.py | 2 + templates/map.html | 240 +++++++++++++++++++++++++++++++++++++++++++++ utils.py | 20 ++-- web.py | 77 +++++---------- 4 files changed, 278 insertions(+), 61 deletions(-) create mode 100644 templates/map.html diff --git a/db.py b/db.py index fdcbab061e..f836093d25 100644 --- a/db.py +++ b/db.py @@ -4,6 +4,7 @@ from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String +from sqlalchemy.sql import not_ try: @@ -63,5 +64,6 @@ def add_sighting(session, spawn_id, pokemon): def get_sightings(session): return session.query(Sighting) \ + .filter(not_(Sighting.pokemon_id.in_(config.TRASH_IDS))) \ .filter(Sighting.expire_timestamp > time.time()) \ .all() diff --git a/templates/map.html b/templates/map.html new file mode 100644 index 0000000000..8c49e5fe43 --- /dev/null +++ b/templates/map.html @@ -0,0 +1,240 @@ + + + + + + + Flask Google Maps Full Map Example + + +

Flask Google Maps Full Map Example

+
+ {{ fullmap.html }} + +
+ + + +{{ fullmap.js }} + + + + + + {% if auto_refresh %} + + {% endif %} + diff --git a/utils.py b/utils.py index 3f2de148af..758cf5c33e 100644 --- a/utils.py +++ b/utils.py @@ -1,16 +1,22 @@ import config +def get_map_center(): + """Returns center of the map""" + lat = (config.MAP_END[0] + config.MAP_START[0]) / 2 + lon = (config.MAP_END[1] + config.MAP_START[1]) / 2 + return lat, lon + + def get_start_coords(worker_no): - """ - Returns center of square for given worker - """ - total_workers = config.GRID[0] * config.GRID[1] - per_column = total_workers / config.GRID[0] + """Returns center of square for given worker""" + grid = config.GRID + total_workers = grid[0] * grid[1] + per_column = total_workers / grid[0] column = worker_no % per_column row = worker_no / per_column - part_lat = (config.MAP_END[0] - config.MAP_START[0]) / float(config.GRID[0]) - part_lon = (config.MAP_END[1] - config.MAP_START[1]) / float(config.GRID[1]) + part_lat = (config.MAP_END[0] - config.MAP_START[0]) / float(grid[0]) + part_lon = (config.MAP_END[1] - config.MAP_START[1]) / float(grid[1]) start_lat = config.MAP_START[0] + part_lat * row + part_lat / 2 start_lon = config.MAP_START[1] + part_lon * column + part_lon / 2 return start_lat, start_lon diff --git a/web.py b/web.py index 54f084cbe6..e5d9ed1875 100644 --- a/web.py +++ b/web.py @@ -1,11 +1,9 @@ -#!/usr/bin/python # -*- coding: utf-8 -*- from datetime import datetime import argparse import json import requests -import flask from flask import Flask, render_template from flask_googlemaps import GoogleMaps from flask_googlemaps import Map @@ -18,23 +16,16 @@ requests.packages.urllib3.disable_warnings(InsecureRequestWarning) + with open('credentials.json') as f: credentials = json.load(f) with open('locales/pokemon.en.json') as f: pokemon_names = json.load(f) -GOOGLEMAPS_KEY = credentials.get('gmaps_key', None) - -DEBUG = True -origin_lat = (app_config.MAP_START[0] + app_config.MAP_END[0]) / 2.0 -origin_lon = (app_config.MAP_START[1] + app_config.MAP_END[1]) / 2.0 -auto_refresh = 60000 - -def debug(message): - if DEBUG: - print '[-] {}'.format(message) +GOOGLEMAPS_KEY = credentials.get('gmaps_key', None) +AUTO_REFRESH = 45000 # refresh map every X s def get_args(): @@ -43,15 +34,18 @@ def get_args(): '-H', '--host', help='Set web server listening host', - default='127.0.0.1') + default='127.0.0.1' + ) parser.add_argument( '-P', '--port', type=int, help='Set web server listening port', - default=5000) + default=5000 + ) parser.add_argument( - '-d', '--debug', help='Debug Mode', action='store_true') + '-d', '--debug', help='Debug Mode', action='store_true' + ) parser.set_defaults(DEBUG=True) return parser.parse_args() @@ -67,58 +61,32 @@ def create_app(): @app.route('/data') def data(): - """ Gets all the PokeMarkers via REST """ + """Gets all the PokeMarkers via REST""" return json.dumps(get_pokemarkers()) -@app.route('/raw_data') -def raw_data(): - """ Gets raw data for pokemons/gyms/pokestops via REST """ - return flask.jsonify(pokemons=pokemons, gyms=gyms, pokestops=pokestops) - - @app.route('/config') def config(): - """ Gets the settings for the Google Maps via REST""" - center = { - 'lat': FLOAT_LAT, - 'lng': FLOAT_LONG, + """Gets the settings for the Google Maps via REST""" + map_center = utils.get_map_center() + return json.dumps({ + 'lat': map_center[0], + 'lng': map_center[1], 'zoom': 15, - 'identifier': "fullmap" - } - return json.dumps(center) + 'identifier': 'fullmap' + }) @app.route('/') def fullmap(): return render_template( - 'example_fullmap.html', + 'map.html', key=GOOGLEMAPS_KEY, fullmap=get_map(), - auto_refresh=auto_refresh + auto_refresh=AUTO_REFRESH * 1000 ) -@app.route('/next_loc') -def next_loc(): - global NEXT_LAT, NEXT_LONG - - location_name = flask.request.args.get('name', '') - if not location_name: - return 'no location_name' - # TODO: - # coords = guess_coords(location_name) - # NEXT_LAT = float(coords[0]) - # NEXT_LONG = float(coords[1]) - # print('[+] Saved next location as %s,%s' % (NEXT_LAT, NEXT_LONG)) - return 'ok' - - -@app.route('/gps') -def next_loc_gps(): - return render_template('nextloc_js.html') - - def get_pokemarkers(): markers = [] @@ -175,11 +143,12 @@ def get_pokemarkers(): def get_map(): + map_center = utils.get_map_center() fullmap = Map( - identifier="fullmap2", + identifier='fullmap2', style='height:100%;width:100%;top:0;left:0;position:absolute;z-index:200;', - lat=origin_lat, - lng=origin_lon, + lat=map_center[0], + lng=map_center[1], markers=get_pokemarkers(), zoom='15', ) From 35d52d729659ac878f0580c73b5f98013d4bfbd0 Mon Sep 17 00:00:00 2001 From: Modrzew Date: Fri, 22 Jul 2016 18:59:27 +0200 Subject: [PATCH 2/7] More cleaning! This time, map.html --- templates/map.html | 105 +++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 62 deletions(-) diff --git a/templates/map.html b/templates/map.html index 8c49e5fe43..3d5310d0ce 100644 --- a/templates/map.html +++ b/templates/map.html @@ -1,57 +1,52 @@ - - - - Flask Google Maps Full Map Example + + + + Pokeminer -

Flask Google Maps Full Map Example

+

Loading!

{{ fullmap.html }} -
- - -{{ fullmap.js }} - - - - - + + - - {% if auto_refresh %}