Skip to content

Commit

Permalink
Find transport data by city and format it (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome1337 authored Sep 28, 2020
1 parent 4f5e59b commit 740a941
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

pip3 install --proxy"=193.56.47.20:8080" -r requirements.txt
pip3 install -r requirements.txt

flask run --host=0.0.0.0
52 changes: 51 additions & 1 deletion routes/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ def get(self):
return jsonify(response.json())


transport = api.schema_model(
'transport', {
'bus': [{
'id': fields.Integer,
'trip_id': fields.String,
'vehicle_id': fields.Integer,
'position': {
'latitude': fields.Integer,
'longitude': fields.Integer,
},
'speed': fields.String,
}],
'metro': [{
'id': fields.Integer,
}],
})


@api.route('/<city>')
@api.param('city', 'City of transport data')
@api.response(200, transport)
@api.response(404, 'City not found')
class CityTransportsList(Resource):

Expand All @@ -27,4 +46,35 @@ def get(self, city):
response = requests.get(url)
if response.status_code == 404:
api.abort(404)
return jsonify(response.json())

transportList = {}
busList = []
metroList = []
for entity in response.json()['entity']:
if entity['vehicle'] is not None:
busList.append({
'id':
entity['id'],
'trip_id':
entity['vehicle']['trip']['tripId'],
'vehicle_id':
entity['vehicle']['vehicle']['id'],
'position': {
'latitude':
entity['vehicle']['position']['latitude']
if entity['vehicle']['position'] is not None else
'N/A',
'longitude':
entity['vehicle']['position']['longitude']
if entity['vehicle']['position'] is not None else
'N/A',
},
'speed':
entity['vehicle']['position']['speed']
if entity['vehicle']['position'] is not None else 0
})

transportList['bus'] = busList
transportList['metro'] = metroList

return transportList
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ omit =

[coverage:report]
show_missing = True

[pycodestyle]
ignore = E122
max-line-length = 120

0 comments on commit 740a941

Please sign in to comment.