-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_data_downloader.py
307 lines (267 loc) · 9.96 KB
/
bus_data_downloader.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import time
import argparse
import logging
import json
import gzip
import xml.etree.ElementTree
import xml.etree.ElementTree as ET
from io import BytesIO
from pathlib import Path
from datetime import datetime, timezone
import dateutil.parser
import requests
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
import pandas as pd
import boto3
from bus_data_models import Base, BusLocation
import credentials
BODS_LOCATION_API_URL = (
"https://data.bus-data.dft.gov.uk/api/v1/datafeed?operatorRef={}&api_key={}"
)
def convert_activity_to_dict(activity: xml.etree.ElementTree.Element) -> dict:
"""
Helper function to unpack activities into JSON.
Parameters
----------
activity : xml.etree.ElementTree.Element
An XML element describing the location and associated information of a bus.
Returns
-------
dict
Dictionary descriving the location and associated information of a bus.
"""
vehicle_journey = activity.find(
"{http://www.siri.org.uk/siri}MonitoredVehicleJourney"
)
vehicle_location = vehicle_journey.find(
"{http://www.siri.org.uk/siri}VehicleLocation"
)
return {
"entry_id": activity.find("{http://www.siri.org.uk/siri}ItemIdentifier").text,
"timestamp": activity.find("{http://www.siri.org.uk/siri}RecordedAtTime").text,
"line_ref": vehicle_journey.find("{http://www.siri.org.uk/siri}LineRef").text,
"direction_ref": vehicle_journey.find(
"{http://www.siri.org.uk/siri}DirectionRef"
).text,
"line_name": vehicle_journey.find(
"{http://www.siri.org.uk/siri}PublishedLineName"
).text,
"operator_ref": vehicle_journey.find(
"{http://www.siri.org.uk/siri}OperatorRef"
).text,
"origin_ref": vehicle_journey.find(
"{http://www.siri.org.uk/siri}OriginRef"
).text,
"origin_name": vehicle_journey.find(
"{http://www.siri.org.uk/siri}OriginName"
).text,
"destination_ref": vehicle_journey.find(
"{http://www.siri.org.uk/siri}DestinationRef"
).text,
"destination_name": vehicle_journey.find(
"{http://www.siri.org.uk/siri}DestinationName"
).text,
"origin_aimed_departure_time": vehicle_journey.find(
"{http://www.siri.org.uk/siri}OriginAimedDepartureTime"
).text,
"vehicle_lat": float(
vehicle_location.find("{http://www.siri.org.uk/siri}Latitude").text
),
"vehicle_lon": float(
vehicle_location.find("{http://www.siri.org.uk/siri}Longitude").text
),
"vehicle_bearing": float(
vehicle_journey.find("{http://www.siri.org.uk/siri}Bearing").text
),
"vehicle_journey_ref": vehicle_journey.find(
"{http://www.siri.org.uk/siri}VehicleJourneyRef"
).text,
"vehicle_ref": vehicle_journey.find(
"{http://www.siri.org.uk/siri}VehicleRef"
).text,
}
def output_json(bus_loc_list: list, output_path: Path):
"""
Takes a list of bus location dictionaries, prepares them for export then
dumps them as JSON.
Parameters
---------
bus_loc_list: list
A list of bus location report dictionaries.
output_path: Path
Path to save JSON to.
Returns
-------
json_str : str
A string representation of the JSON object.
"""
# Convert to a DF and get rid of some of the columns not useful on the
# front end
output_df = pd.DataFrame(json_output_list).drop(
["entry_id", "origin_ref", "destination_ref", "line_ref"], axis=1
)
# Remove some unnecessary charaters
output_df.loc[output_df["direction_ref"] == "INBOUND", ["direction_ref"]] = "I"
output_df.loc[output_df["direction_ref"] == "OUTBOUND", ["direction_ref"]] = "O"
# We want to write and have the option to put it on S3, so we do it
# this way
df_str = output_df.to_dict(orient="records")
json_str = json.dumps({
"timestamp": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"),
"data": df_str
})
with open(output_path, "w") as f:
f.write(json_str)
return json_str
def add_bus_location_to_db_session(bus_loc_report: dict, db_session: Session):
"""
Simply adds a bus location report to the current database session. Note that this function converts string ISO timestamps to database objects.
Parameters
----------
bus_loc_report : dict
A bus location report, as prepared by convert_activity_to_dict.
db_session : Session
An SQLAlchemy database session.
Returns
-------
Nothing.
"""
bus_location = BusLocation(
entry_id=bus_loc_report["entry_id"],
timestamp=dateutil.parser.isoparse(bus_loc_report["timestamp"]),
line_ref=bus_loc_report["line_ref"],
direction_ref=bus_loc_report["direction_ref"],
line_name=bus_loc_report["line_name"],
operator_ref=bus_loc_report["operator_ref"],
origin_ref=bus_loc_report["origin_ref"],
origin_name=bus_loc_report["origin_name"],
destination_ref=bus_loc_report["destination_ref"],
destination_name=bus_loc_report["destination_name"],
origin_aimed_departure_time=dateutil.parser.isoparse(
bus_loc_report["origin_aimed_departure_time"]
),
vehicle_lat=bus_loc_report["vehicle_lat"],
vehicle_lon=bus_loc_report["vehicle_lon"],
vehicle_bearing=bus_loc_report["vehicle_bearing"],
vehicle_journey_ref=bus_loc_report["vehicle_journey_ref"],
vehicle_ref=bus_loc_report["vehicle_ref"],
)
session.add(bus_location)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Tool to collect and publish the latest BODS data for a given operator.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--db",
help="Save each update to a database.",
action="store_true",
default=False,
)
parser.add_argument(
"operator_code", help="The BODS operator code to grab.", type=str
)
parser.add_argument(
"output_path", help="Location to save each update to.", type=str
)
parser.add_argument(
"--aws",
help="Push to S3 Bucket on each update.",
action="store_true",
default=False,
)
parser.add_argument(
"--aws_filename",
help="Name to push to S3 bucket.",
type=str,
default="current_bus_locations.json",
)
parser.add_argument(
"--sleep_interval",
help="How many seconds to sleep between each pull from the API.",
type=int,
default=6,
)
parser.add_argument(
"--aws_push_interval",
help="The number of sleep cycles to wait between pushing data to AWS.",
type=int,
default=3
)
args = parser.parse_args()
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s: %(message)s",
handlers=[
logging.FileHandler("data_collector.log"),
logging.StreamHandler()
]
)
# Check output path validity
output_path = Path(args.output_path)
if output_path.is_dir():
raise ValueError("Output path cannot be a directory.")
if output_path.exists():
print("Path {} exists - will be overwritten.".format(output_path))
# Set up the DB
if args.db:
engine = create_engine(
"postgresql://{}:{}@{}:{}".format(
credentials.POSTGRES_USER,
credentials.POSTGRES_PASSWORD,
credentials.POSTGRES_HOST,
credentials.POSTGRES_PORT,
)
)
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# Set up AWS
if args.aws:
s3 = boto3.resource("s3")
# Set up operator code and URL to query
operator_ref = args.operator_code
location_url = BODS_LOCATION_API_URL.format(operator_ref, credentials.BODS_API_KEY)
# Loop to get latest data
aws_interval_counter = 0
while True:
try:
# Get the latest info
resp = requests.get(location_url)
tree = ET.fromstring(resp.text)
# Extract the activities
activities = tree.findall(
"./{http://www.siri.org.uk/siri}ServiceDelivery/{http://www.siri.org.uk/siri}VehicleMonitoringDelivery/{http://www.siri.org.uk/siri}VehicleActivity"
)
# Convert each to JSON
json_output_list = []
for activity in activities:
converted_activity = convert_activity_to_dict(activity)
json_output_list.append(converted_activity)
# Add to database if needed
if args.db:
add_bus_location_to_db_session(converted_activity, session)
json_str = output_json(json_output_list, output_path)
aws_interval_counter += 1
# if using AWS, push to bucket
if args.aws and aws_interval_counter >= args.aws_push_interval:
aws_interval_counter = 0
# Great snippet from https://gist.github.com/veselosky/9427faa38cee75cd8e27
upload_obj = BytesIO()
json_comp = gzip.GzipFile(None, 'w', 9, upload_obj)
json_comp.write(json_str.encode('utf-8'))
json_comp.close()
s3.Bucket(credentials.S3_BUCKET_NAME).put_object(
Key=args.aws_filename,
Body=upload_obj.getvalue(),
ACL="public-read",
ContentType='application/json',
ContentEncoding='gzip',
)
# Commit to Database
if args.db:
session.commit()
except Exception as e:
logging.error("Error getting data: {}".format(e))
time.sleep(args.sleep_interval)