Skip to content

Commit

Permalink
#15: enable v2 implementation, this is just a copy of v1.py so fare
Browse files Browse the repository at this point in the history
  • Loading branch information
aschuma committed Mar 28, 2024
1 parent bdc554c commit b67ac55
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask.helpers import make_response
from waitress import serve
import service.v1 as v1
import service.v2 as v2


from_id = getenv("VVS_FROM", "5006333")
Expand Down Expand Up @@ -36,6 +37,14 @@ def get_trips_v1():
return jsonify({'message': str(e), 'status': 500}), 500


@app.route('/api/v2/', methods=['GET'])
def get_trips_v2():
try:
return jsonify({'trips': v2.find_trips(from_id, to_id, limit, check_time=check_time()), 'status': 200})
except Exception as e:
return jsonify({'message': str(e), 'status': 500}), 500


@app.after_request
def after_request_func(data):
response = make_response(data)
Expand Down
44 changes: 44 additions & 0 deletions service/v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

from os import getenv
from datetime import datetime, timedelta
from vvspy.trip import get_trips


def iso_ts(date):
return date.strftime('%Y-%m-%dT%H:%M:%SZ')


def extract_data(connection):
departure_planed = connection.origin.departure_time_planned
departure_estimated = connection.origin.departure_time_estimated
departure_delta = int(
(departure_estimated - departure_planed).total_seconds() / 60)
arrival_planed = connection.destination.arrival_time_planned
arrival_estimated = connection.destination.arrival_time_estimated
arrival_delta = int(
(arrival_estimated - arrival_planed).total_seconds() / 60)

travel_time = int(
(arrival_estimated - departure_planed).total_seconds() / 60)

return {
"from_id": connection.origin.id,
"to_id": connection.destination.id,
"from": connection.origin.name,
"to": connection.destination.name,
"departure_planned": iso_ts(departure_planed),
"departure_estimated": iso_ts(departure_estimated),
"departure_delay": departure_delta,
"arrival_planed": iso_ts(arrival_planed),
"arrival_estimated": iso_ts(arrival_estimated),
"arrival_delay": arrival_delta,
"travel_time": travel_time,
"number": connection.transportation.number
}


def find_trips(from_id, to_id, limit, check_time):
trips = get_trips(from_id, to_id, limit=limit, check_time=check_time)
trip_data_list = [extract_data(trip.connections[0])
for trip in trips if len(trip.connections) == 1]
return trip_data_list

0 comments on commit b67ac55

Please sign in to comment.