-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ea98aeb
Showing
15 changed files
with
6,216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FLASK_ENV=development | ||
FLASK_APP=index.py | ||
ZOMATO_API_KEY= | ||
OPENCAGEDATA_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.env | ||
.pytest_cache | ||
.next | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode | ||
.env | ||
.pytest_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
repos: | ||
- repo: https://github.com/ambv/black | ||
rev: stable | ||
hooks: | ||
- id: black |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[[source]] | ||
url = "https://pypi.python.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
flask = "*" | ||
"rfc3339" = "*" | ||
ansicolors = "*" | ||
requests = "*" | ||
psycopg2-binary = "*" | ||
|
||
[dev-packages] | ||
pytest = "*" | ||
black = "==19.3b0" | ||
pre-commit = "*" | ||
|
||
[scripts] | ||
dev = "flask run" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Prague public transport departures | ||
|
||
Next.js app which uses flask lambda function to query PostGIS for departures from stops near me. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
from flask import request, jsonify | ||
import psycopg2 | ||
from app_generator import create_app | ||
|
||
app = create_app() | ||
|
||
database_url = os.getenv('DATABASE_URL') | ||
|
||
QUERY = """ | ||
set timezone TO 'Europe/Prague'; | ||
WITH stops_nearby AS ( | ||
SELECT stops.stop_id | ||
FROM gtfs.stops | ||
ORDER BY gtfs.stops.the_geom <-> ST_GeomFromText(%(location)s, 4326) | ||
LIMIT 10 | ||
) | ||
SELECT r.route_short_name, trip_headsign, departure_time, s.stop_name | ||
FROM gtfs.stops s | ||
left join gtfs.stop_times st on s.stop_id = st.stop_id | ||
left join gtfs.trips t on t.trip_id = st.trip_id | ||
left join gtfs.calendar c on c.service_id = t.service_id | ||
left join gtfs.routes r on r.route_id = t.route_id | ||
WHERE departure_time::time > current_time and departure_time::time < current_time + interval '15 minutes' | ||
and c.start_date <= current_date | ||
and c.end_date >= current_date | ||
and s.stop_id in (select * from stops_nearby) | ||
and ( | ||
CASE | ||
WHEN extract(dow from current_date) = 1 THEN c.monday::boolean = true | ||
WHEN extract(dow from current_date) = 2 THEN c.tuesday::boolean = true | ||
WHEN extract(dow from current_date) = 3 THEN c.wednesday::boolean = true | ||
WHEN extract(dow from current_date) = 4 THEN c.thursday::boolean = true | ||
WHEN extract(dow from current_date) = 5 THEN c.friday::boolean = true | ||
WHEN extract(dow from current_date) = 6 THEN c.saturday::boolean = true | ||
WHEN extract(dow from current_date) = 0 THEN c.sunday::boolean = true | ||
END | ||
) | ||
order by departure_time; | ||
""" | ||
|
||
def convert(item): | ||
return (item[0], item[1], str(item[2]), item[3]) | ||
|
||
@app.route("/api/departures", methods=["GET"]) | ||
def api(): | ||
latitude = request.args.get('latitude') | ||
longitude = request.args.get('longitude') | ||
|
||
connection = psycopg2.connect(database_url) | ||
cursor = connection.cursor() | ||
try: | ||
cursor.execute(QUERY, {"location": f"POINT({longitude} {latitude})"}) | ||
data = cursor.fetchall() | ||
finally: | ||
cursor.close() | ||
connection.close() | ||
data = [convert(item) for item in data] | ||
return jsonify(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import logging | ||
from flask import Flask | ||
from request_logger import attach_logger | ||
|
||
|
||
def create_app(): | ||
app = Flask(__name__) | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.DEBUG) | ||
|
||
attach_logger(app) | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"version": 2, | ||
"name": "departures", | ||
"alias": "departures", | ||
"env": { | ||
"DATABASE_URL": "@departures-database-url" | ||
} | ||
} |
Oops, something went wrong.