Skip to content

Commit

Permalink
include authentication header + oulu cleanup DT-5774
Browse files Browse the repository at this point in the history
  • Loading branch information
Topi Koivunen authored and Topi Koivunen committed Mar 3, 2023
1 parent de608e5 commit 259d5c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 57 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ You need to configure at least the following env variables that are marked as ma
* (mandatory) "FEED_TYPE" which type of a GTFS RT data is provided (for example vp for vehicle position), used in MQTT topic
* (mandatory) "FEED_NAME" name for the data feed, used in MQTT topic
* (mandatory) "FEED_URL" URL for the HTTP(S) GTFS RT data source
* (mandatory as of 4.3.2023 if using Digitransit API) "AUTHENTICATION_HEADER" Authentication header name
* (mandatory as of 4.3.2023 if using Digitransit API) "AUTHENTICATION_TOKEN" Authentication header secret
* (optional) "USERNAME" username for publishing to a MQTT broker
* (optional) "PASSWORD" password for publishing to a MQTT broker
* (optional, default 5) "INTERVAL" how long to wait in seconds between fetching new data from HTTP(S) data feed
Expand Down
7 changes: 5 additions & 2 deletions gtfsrthttp2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def doGTFSRTPolling(self):

trip_id = entity.vehicle.trip.trip_id
route_id = utils.parse_route_id(self.feedName, entity.vehicle.trip.route_id, trip_id, self.OTPData)
direction_id = utils.parse_direction_id(self.feedName, entity.vehicle.trip.direction_id, trip_id, self.OTPData)
direction_id = entity.vehicle.trip.direction_id
trip_headsign = entity.vehicle.vehicle.label
# headsigns with / cause problems in topics
if '/' in trip_headsign:
Expand Down Expand Up @@ -144,9 +144,12 @@ def doOTPPolling(self):
adapter = HTTPAdapter(max_retries=retry)
otp_polling_session.mount(OTP_URL, adapter)
query = utils.get_OTP_query(self.feedName)
headers = {}
if "AUTHENTICATION_HEADER" in os.environ and "AUTHENTICATION_TOKEN" in os.environ:
headers[os.environ["AUTHENTICATION_HEADER"]] = os.environ["AUTHENTICATION_TOKEN"]

try:
response = otp_polling_session.post(OTP_URL, json={'query': query})
response = otp_polling_session.post(OTP_URL, headers=headers, json={'query': query})
except Exception as x:
print('Failed to fetch OTP data :(', x.__class__.__name__)
else:
Expand Down
64 changes: 9 additions & 55 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,11 @@ def parse_route_id(feed, route_id, trip_id, otp_data):
if len(route_id) > 5 and (route_id[-5:] == "47374" or route_id[-5:] == "56920"):
return route_id[0:-5]
return route_id[0:-4]
elif feed == "OULU":
feed_scoped_id = "OULU:" + trip_id
if otp_data == None or feed_scoped_id not in otp_data:
return ""
return otp_data[feed_scoped_id]["route"]["gtfsId"].split(':')[1]
return route_id

def parse_direction_id(feed, direction_id, trip_id, otp_data):
if feed == "OULU":
feed_scoped_id = "OULU:" + trip_id
if otp_data == None or feed_scoped_id not in otp_data:
return ""
return str(otp_data[feed_scoped_id]["pattern"]["directionId"])
return direction_id

def parse_short_name(feed, trip_id, route_id, otp_data):
if otp_data == None:
return ""
elif feed == "OULU":
feed_scoped_id = "OULU:" + trip_id
if feed_scoped_id not in otp_data:
return ""
return otp_data[feed_scoped_id]["route"]["shortName"]

feed_scoped_id = feed + ":" + route_id
if feed_scoped_id not in otp_data:
Expand All @@ -37,11 +19,6 @@ def parse_short_name(feed, trip_id, route_id, otp_data):
def parse_color(feed, trip_id, route_id, otp_data):
if otp_data == None:
return ""
elif feed == "OULU":
feed_scoped_id = "OULU:" + trip_id
if feed_scoped_id not in otp_data:
return ""
return otp_data[feed_scoped_id]["route"]["color"] or ""

feed_scoped_id = feed + ":" + route_id
if feed_scoped_id not in otp_data:
Expand All @@ -51,44 +28,21 @@ def parse_color(feed, trip_id, route_id, otp_data):
def parse_mode(feed, trip_id, route_id, otp_data):
if otp_data == None:
return ""
elif feed == "OULU":
feed_scoped_id = "OULU:" + trip_id
if feed_scoped_id not in otp_data:
return ""
return otp_data[feed_scoped_id]["route"]["mode"] or ""

feed_scoped_id = feed + ":" + route_id
if feed_scoped_id not in otp_data:
return ""
return otp_data[feed + ":" + route_id]["mode"] or ""

def get_OTP_query(feed):
if feed == "OULU":
return """
{
trips(feeds: [\"OULU\"]) {
route {
shortName
gtfsId
color
mode
}
gtfsId
pattern {
directionId
}
}
}
"""
else:
return """
{
routes(feeds: [\"%s\"]) {
gtfsId
shortName
color
mode
}
return """
{
routes(feeds: [\"%s\"]) {
gtfsId
shortName
color
mode
}
""" % feed
}
""" % feed

0 comments on commit 259d5c2

Please sign in to comment.