Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stlk committed Sep 27, 2019
0 parents commit ea98aeb
Show file tree
Hide file tree
Showing 15 changed files with 6,216 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
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=
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
.pytest_cache
.next
node_modules
3 changes: 3 additions & 0 deletions .nowignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
.env
.pytest_cache
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
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
19 changes: 19 additions & 0 deletions Pipfile
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"
350 changes: 350 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions README.md
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.
59 changes: 59 additions & 0 deletions api/departures.py
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)
13 changes: 13 additions & 0 deletions app_generator.py
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
8 changes: 8 additions & 0 deletions now.json
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"
}
}
Loading

0 comments on commit ea98aeb

Please sign in to comment.