Skip to content

Instantly share code, notes, and snippets.

@mango314

mango314/subway.py

Last active Feb 20, 2017
Embed
What would you like to do?
parse subway gtfs
import requests
key = ''
r = requests.get('http://datamine.mta.info/mta_esi.php?key=%s&feed_id=1'%(key))
# http://datamine.mta.info/sites/all/files/pdfs/nyct-subway.proto.txt
# https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto
# https://github.com/google/protobuf
import gtfs_realtime_pb2, nyct_subway_pb2
# http://datamine.mta.info/sites/all/files/pdfs/GTFS-Realtime-NYC-Subway%20version%201%20dated%207%20Sep.pdf
# https://groups.google.com/forum/#!topic/mtadeveloperresources/PeWNe4WG890
subways = gtfs_realtime_pb2.FeedMessage()
subways.ParseFromString(r.content)
subways.entity
for T in subways.entity:
print T
# stick into cron, hopefully doesn't crash!
[(
str(E.trip_update.trip.route_id),
len(E.trip_update.stop_time_update),
datetime.datetime.fromtimestamp(
int(E.trip_update.stop_time_update[0].arrival.time)
).strftime('%H:%M:%S')
,
str(E.trip_update.stop_time_update[0].stop_id),
str(E.trip_update.trip.trip_id)
) for E in subways.entity if len(E.trip_update.stop_time_update) > 0]
"""
[('1', 1, '10:00:33', '140S', '047600_1..S'),
('1', 25, '10:02:19', '116S', '051600_1..S'),
('1', 2, '09:58:31', '139S', '048150_1..S07R'),
('1', 4, '10:01:45', '137S', '048400_1..S'),
('1', 6, '10:00:28', '135S', '048600_1..S02R'),
('1', 5, '10:01:54', '107N', '048950_1..N02R'),
('1', 8, '10:00:48', '133S', '049100_1..S02R'),
('1', 9, '10:02:21', '111N', '049400_1..N02R'),
..."""
id: "000002"
vehicle {
trip {
trip_id: "078200_1..N02R"
start_date: "20141217"
route_id: "1"
[nyct_trip_descriptor] {
train_id: "01 1302 SFY/242"
is_assigned: true
direction: NORTH
}
}
current_stop_sequence: 38
current_status: IN_TRANSIT_TO
timestamp: 1418842977
stop_id: "101N"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.