-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.py
executable file
·43 lines (35 loc) · 1.14 KB
/
http.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
from flask import Flask, render_template, request
from math import sin, cos, sqrt, atan2, radians
import stations
import sqlite3
import operator
import json
app = Flask(__name__)
@app.route("/")
def hello():
return "OK"
@app.route("/nearest/")
def nearest():
db = sqlite3.connect('data/db.sqlite')
lat = request.args.get('lat')
lon = request.args.get('lon')
max_stations = request.args.get('max_entries')
if max_stations is None:
max_stations = 5
try:
lat = float(lat)
lon = float(lon)
except ValueError:
return json.dumps({"status": "NOK", "error": "value_coord"})
try:
max_stations = int(max_stations)
except ValueError:
return json.dumps({"status": "NOK", "error": "value_max"})
return json.dumps(stations.nearest_stations(db, lat, lon, max_stations))
@app.route("/station/<station>/")
def getstation(station):
db = sqlite3.connect('data/db.sqlite')
station = stations.get_station(db, station)
if station is False:
return json.dumps({"status": "NOK", "error": "NOT_FOUND"})
return json.dumps({"status": "OK", "station": station})