2016-10-03 16:16:09 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import MySQLdb
|
|
|
|
from flask import Flask
|
|
|
|
from flask import abort
|
|
|
|
from flask import jsonify
|
|
|
|
from flask import make_response
|
|
|
|
from flask import redirect
|
|
|
|
from flask import request
|
|
|
|
from flask import send_file
|
|
|
|
from flask import send_from_directory
|
|
|
|
from flask import url_for
|
2016-10-27 12:40:58 +02:00
|
|
|
from flask_cache import Cache
|
2016-10-03 16:16:09 +02:00
|
|
|
|
|
|
|
from config import database
|
2016-10-05 14:25:28 +02:00
|
|
|
import top
|
2016-10-03 16:16:09 +02:00
|
|
|
|
|
|
|
db = MySQLdb.connect(database["host"],
|
|
|
|
database["user"],
|
|
|
|
database["passwd"],
|
|
|
|
database["db"])
|
|
|
|
cur = db.cursor()
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2016-10-27 12:40:58 +02:00
|
|
|
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
|
2016-10-03 16:16:09 +02:00
|
|
|
app.config.update(
|
|
|
|
JSONIFY_PRETTYPRINT_REGULAR=False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/connection/', methods=["GET"])
|
2016-10-05 14:25:28 +02:00
|
|
|
def getConnection():
|
2016-10-03 16:16:09 +02:00
|
|
|
if not request.args \
|
|
|
|
or 'from' not in request.args or 'to' not in request.args \
|
|
|
|
or not request.args["from"].isdigit() or not request.args["to"].isdigit():
|
|
|
|
abort(400)
|
|
|
|
cur.execute("SELECT id FROM connections WHERE start = %s AND goal = %s", [request.args["from"], request.args["to"]])
|
|
|
|
result = cur.fetchone()
|
2016-10-03 18:03:24 +02:00
|
|
|
if not result:
|
|
|
|
abort(404)
|
2016-10-03 16:16:09 +02:00
|
|
|
return redirect(url_for("get_connection", connection_id=int(result[0])))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/connection/<int:connection_id>', methods=['GET'])
|
|
|
|
def get_connection(connection_id):
|
|
|
|
try:
|
|
|
|
print("HIT")
|
|
|
|
with open("file/" + str(connection_id) + ".json") as f:
|
|
|
|
data = json.load(f)
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
except EnvironmentError:
|
|
|
|
print("MISS")
|
|
|
|
cur.execute("SELECT start, goal, length FROM connections WHERE id=%s", [connection_id])
|
|
|
|
result = cur.fetchone()
|
|
|
|
geojson = []
|
|
|
|
try:
|
|
|
|
geojsonstring = subprocess.check_output(["togeojson", "file/" + str(connection_id) + ".gpx"]).decode(
|
|
|
|
'UTF-8')
|
|
|
|
geojson = json.loads(geojsonstring)
|
|
|
|
feature = geojson["features"][0]
|
|
|
|
del feature["properties"]["desc"], feature["properties"]["name"] # Überreste von GPX entfernen
|
|
|
|
feature["properties"]["wayLength"] = result[2]
|
|
|
|
feature["properties"]["nodes"] = [result[0], result[1]]
|
|
|
|
feature["properties"]["id"] = connection_id
|
|
|
|
geojson["features"][0] = feature
|
|
|
|
with open("file/" + str(connection_id) + ".json", 'w') as outfile:
|
|
|
|
json.dump(geojson, outfile, indent=4)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
abort(404)
|
|
|
|
return jsonify(geojson)
|
|
|
|
|
|
|
|
|
2016-10-27 12:40:58 +02:00
|
|
|
def make_cache_key(*args, **kwargs): return request.url
|
|
|
|
|
|
|
|
|
2016-10-05 14:25:28 +02:00
|
|
|
@app.route('/api/top/', methods=["GET"])
|
2016-10-27 12:40:58 +02:00
|
|
|
@cache.cached(timeout=50, key_prefix=make_cache_key)
|
2016-10-05 14:25:28 +02:00
|
|
|
def get_top():
|
|
|
|
if not request.args \
|
2016-10-27 12:40:58 +02:00
|
|
|
or 'type' not in request.args \
|
|
|
|
or 'pageSize' not in request.args \
|
|
|
|
or 'pageNumber' not in request.args:
|
|
|
|
print(request.args)
|
2016-10-05 14:25:28 +02:00
|
|
|
abort(400)
|
2016-10-27 12:40:58 +02:00
|
|
|
return jsonify(top.helloworld(cur, request.args))
|
2016-10-05 14:25:28 +02:00
|
|
|
|
|
|
|
|
2016-10-03 16:16:09 +02:00
|
|
|
@app.route('/')
|
|
|
|
def send_main_html():
|
|
|
|
return send_file('www/map.html')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/<path:path>')
|
|
|
|
def send_static(path):
|
|
|
|
return send_from_directory('www', path)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def not_found(error):
|
|
|
|
return make_response(jsonify({'error': str(error)}), 404)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(400)
|
|
|
|
def bad_request(error):
|
|
|
|
return make_response(jsonify({'error': str(error)}), 400)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True)
|