-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Mta Subway translator & and realtime ext 'stop_name' field (#4)
* initial translator & tests for mta subway * parses date to GMT, adjustments to stop id, removes track * removes DS_store * added mtasubway to the registry * updates tests, handles multiple stops * added stop name to realtime extension * remove print statement and unused import
- Loading branch information
1 parent
4af762d
commit 5b97a1f
Showing
8 changed files
with
363 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .la_metro import LaMetroGtfsRealtimeTranslator | ||
from .septa_regional_rail import SeptaRegionalRailTranslator | ||
from .mta_subway import MtaSubwayGtfsRealtimeTranslator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import pendulum | ||
|
||
from gtfs_realtime_translators.factories import TripUpdate, FeedMessage | ||
|
||
|
||
class MtaSubwayGtfsRealtimeTranslator: | ||
def __init__(self, data): | ||
entities = [] | ||
for stop in data: | ||
for group in stop["groups"]: | ||
for idx, arrival in enumerate(group["times"]): | ||
route_id = group['route']['id'] | ||
stop_name = stop['stop']['name'] | ||
entities.append(self.__make_trip_update(idx, route_id, stop_name, arrival)) | ||
|
||
self.feed_message = FeedMessage.create(entities=entities) | ||
|
||
@classmethod | ||
def get_stop_id(cls, stop_id): | ||
""" | ||
Stop IDs from MTA Subway come in the form MTASBY:<stop id>. | ||
We must parse the stop_id only. | ||
""" | ||
try: | ||
return stop_id.split(':')[1] | ||
except Exception: | ||
return stop_id | ||
|
||
|
||
@classmethod | ||
def to_gmt_timestamp(cls, timestamp): | ||
return int(pendulum.from_timestamp(timestamp).subtract(hours=4).timestamp()) | ||
|
||
@classmethod | ||
def __make_trip_update(cls, _id, route_id, stop_name, arrival): | ||
entity_id = str(_id + 1) | ||
arrival_time = cls.to_gmt_timestamp(arrival['serviceDay'] + arrival['realtimeArrival']) | ||
departure_time = cls.to_gmt_timestamp(arrival['serviceDay'] + arrival['realtimeDeparture']) | ||
trip_id = arrival['tripId'] | ||
stop_id = cls.get_stop_id(arrival['stopId']) | ||
|
||
##### Intersection Extensions | ||
headsign = arrival['tripHeadsign'] | ||
scheduled_arrival_time = cls.to_gmt_timestamp(arrival['serviceDay'] + arrival['scheduledArrival']) | ||
scheduled_departure_time = cls.to_gmt_timestamp(arrival['serviceDay'] + arrival['scheduledDeparture']) | ||
return TripUpdate.create(entity_id=entity_id, | ||
arrival_time=arrival_time, | ||
departure_time=departure_time, | ||
trip_id=trip_id, | ||
route_id=route_id, | ||
stop_id=stop_id, | ||
stop_name=stop_name, | ||
headsign=headsign, | ||
scheduled_arrival_time=scheduled_arrival_time, | ||
scheduled_departure_time=scheduled_departure_time) | ||
|
||
|
||
def serialize(self): | ||
return self.feed_message.SerializeToString() |
Oops, something went wrong.