Skip to content

Commit 280d3e4

Browse files
authored
Merge pull request #26 from HSLdevcom/DT-5774
include authentication header + oulu cleanup DT-5774
2 parents de608e5 + 259d5c2 commit 280d3e4

File tree

3 files changed

+16
-57
lines changed

3 files changed

+16
-57
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ You need to configure at least the following env variables that are marked as ma
1111
* (mandatory) "FEED_TYPE" which type of a GTFS RT data is provided (for example vp for vehicle position), used in MQTT topic
1212
* (mandatory) "FEED_NAME" name for the data feed, used in MQTT topic
1313
* (mandatory) "FEED_URL" URL for the HTTP(S) GTFS RT data source
14+
* (mandatory as of 4.3.2023 if using Digitransit API) "AUTHENTICATION_HEADER" Authentication header name
15+
* (mandatory as of 4.3.2023 if using Digitransit API) "AUTHENTICATION_TOKEN" Authentication header secret
1416
* (optional) "USERNAME" username for publishing to a MQTT broker
1517
* (optional) "PASSWORD" password for publishing to a MQTT broker
1618
* (optional, default 5) "INTERVAL" how long to wait in seconds between fetching new data from HTTP(S) data feed

gtfsrthttp2mqtt.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def doGTFSRTPolling(self):
9393

9494
trip_id = entity.vehicle.trip.trip_id
9595
route_id = utils.parse_route_id(self.feedName, entity.vehicle.trip.route_id, trip_id, self.OTPData)
96-
direction_id = utils.parse_direction_id(self.feedName, entity.vehicle.trip.direction_id, trip_id, self.OTPData)
96+
direction_id = entity.vehicle.trip.direction_id
9797
trip_headsign = entity.vehicle.vehicle.label
9898
# headsigns with / cause problems in topics
9999
if '/' in trip_headsign:
@@ -144,9 +144,12 @@ def doOTPPolling(self):
144144
adapter = HTTPAdapter(max_retries=retry)
145145
otp_polling_session.mount(OTP_URL, adapter)
146146
query = utils.get_OTP_query(self.feedName)
147+
headers = {}
148+
if "AUTHENTICATION_HEADER" in os.environ and "AUTHENTICATION_TOKEN" in os.environ:
149+
headers[os.environ["AUTHENTICATION_HEADER"]] = os.environ["AUTHENTICATION_TOKEN"]
147150

148151
try:
149-
response = otp_polling_session.post(OTP_URL, json={'query': query})
152+
response = otp_polling_session.post(OTP_URL, headers=headers, json={'query': query})
150153
except Exception as x:
151154
print('Failed to fetch OTP data :(', x.__class__.__name__)
152155
else:

utils.py

+9-55
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,11 @@ def parse_route_id(feed, route_id, trip_id, otp_data):
55
if len(route_id) > 5 and (route_id[-5:] == "47374" or route_id[-5:] == "56920"):
66
return route_id[0:-5]
77
return route_id[0:-4]
8-
elif feed == "OULU":
9-
feed_scoped_id = "OULU:" + trip_id
10-
if otp_data == None or feed_scoped_id not in otp_data:
11-
return ""
12-
return otp_data[feed_scoped_id]["route"]["gtfsId"].split(':')[1]
138
return route_id
149

15-
def parse_direction_id(feed, direction_id, trip_id, otp_data):
16-
if feed == "OULU":
17-
feed_scoped_id = "OULU:" + trip_id
18-
if otp_data == None or feed_scoped_id not in otp_data:
19-
return ""
20-
return str(otp_data[feed_scoped_id]["pattern"]["directionId"])
21-
return direction_id
22-
2310
def parse_short_name(feed, trip_id, route_id, otp_data):
2411
if otp_data == None:
2512
return ""
26-
elif feed == "OULU":
27-
feed_scoped_id = "OULU:" + trip_id
28-
if feed_scoped_id not in otp_data:
29-
return ""
30-
return otp_data[feed_scoped_id]["route"]["shortName"]
3113

3214
feed_scoped_id = feed + ":" + route_id
3315
if feed_scoped_id not in otp_data:
@@ -37,11 +19,6 @@ def parse_short_name(feed, trip_id, route_id, otp_data):
3719
def parse_color(feed, trip_id, route_id, otp_data):
3820
if otp_data == None:
3921
return ""
40-
elif feed == "OULU":
41-
feed_scoped_id = "OULU:" + trip_id
42-
if feed_scoped_id not in otp_data:
43-
return ""
44-
return otp_data[feed_scoped_id]["route"]["color"] or ""
4522

4623
feed_scoped_id = feed + ":" + route_id
4724
if feed_scoped_id not in otp_data:
@@ -51,44 +28,21 @@ def parse_color(feed, trip_id, route_id, otp_data):
5128
def parse_mode(feed, trip_id, route_id, otp_data):
5229
if otp_data == None:
5330
return ""
54-
elif feed == "OULU":
55-
feed_scoped_id = "OULU:" + trip_id
56-
if feed_scoped_id not in otp_data:
57-
return ""
58-
return otp_data[feed_scoped_id]["route"]["mode"] or ""
5931

6032
feed_scoped_id = feed + ":" + route_id
6133
if feed_scoped_id not in otp_data:
6234
return ""
6335
return otp_data[feed + ":" + route_id]["mode"] or ""
6436

6537
def get_OTP_query(feed):
66-
if feed == "OULU":
67-
return """
68-
{
69-
trips(feeds: [\"OULU\"]) {
70-
route {
71-
shortName
72-
gtfsId
73-
color
74-
mode
75-
}
76-
gtfsId
77-
pattern {
78-
directionId
79-
}
80-
}
81-
}
82-
"""
83-
else:
84-
return """
85-
{
86-
routes(feeds: [\"%s\"]) {
87-
gtfsId
88-
shortName
89-
color
90-
mode
91-
}
38+
return """
39+
{
40+
routes(feeds: [\"%s\"]) {
41+
gtfsId
42+
shortName
43+
color
44+
mode
9245
}
93-
""" % feed
46+
}
47+
""" % feed
9448

0 commit comments

Comments
 (0)