-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathmtapi.py
More file actions
229 lines (174 loc) · 7.5 KB
/
mtapi.py
File metadata and controls
229 lines (174 loc) · 7.5 KB
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
import urllib.request, urllib.error
import contextlib, datetime, copy
from collections import defaultdict
from itertools import islice
from operator import itemgetter
import csv, math, json
import threading
import logging
import google.protobuf.message
from mtaproto.feedresponse import FeedResponse, Trip, TripStop, TZ
from mtapi._mtapithreader import _MtapiThreader
from typing import TypeAlias
logger = logging.getLogger(__name__)
point: TypeAlias = tuple[float, float] | list[float]
def distance(p1: point, p2: point) -> float:
return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
class Mtapi(object):
class _Station(object):
last_update = None
def __init__(self, json):
self.json = json
self.trains = {}
self.clear_train_data()
def __getitem__(self, key):
return self.json[key]
def add_train(self, route_id, direction, train_time, feed_time):
self.routes.add(route_id)
self.trains[direction].append({
'route': route_id,
'time': train_time
})
self.last_update = feed_time
def clear_train_data(self):
self.trains['N'] = []
self.trains['S'] = []
self.routes = set()
self.last_update = None
def sort_trains(self, max_trains):
self.trains['S'] = sorted(self.trains['S'], key=itemgetter('time'))[:max_trains]
self.trains['N'] = sorted(self.trains['N'], key=itemgetter('time'))[:max_trains]
def serialize(self):
out = {
'N': self.trains['N'],
'S': self.trains['S'],
'routes': self.routes,
'last_update': self.last_update
}
out.update(self.json)
return out
_FEED_URLS = [
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs', # 123456S
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-l', # L
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-nqrw', # NRQW
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-bdfm', # BDFM
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-ace', # ACE
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-7', # 7
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-jz', # JZ
'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-g' # G
]
def __init__(self, key, stations_file, expires_seconds=60, max_trains=10, max_minutes=30, threaded=False):
self._KEY = key
self._MAX_TRAINS = max_trains
self._MAX_MINUTES = max_minutes
self._EXPIRES_SECONDS = expires_seconds
self._THREADED = threaded
self._stations = {}
self._stops_to_stations = {}
self._routes = {}
self._read_lock = threading.RLock()
# initialize the stations database
try:
with open(stations_file, 'r') as f:
self._stations = json.load(f)
for id in self._stations:
self._stations[id] = self._Station(self._stations[id])
self._stops_to_stations = self._build_stops_index(self._stations)
except IOError as e:
print('Couldn\'t load stations file '+stations_file)
exit()
self._update()
if threaded:
self.threader = _MtapiThreader(self, expires_seconds)
self.threader.start_timer()
@staticmethod
def _build_stops_index(stations):
stops = {}
for station_id in stations:
for stop_id in stations[station_id]['stops'].keys():
stops[stop_id] = station_id
return stops
def _load_mta_feed(self, feed_url):
try:
request = urllib.request.Request(feed_url)
request.add_header('x-api-key', self._KEY)
with contextlib.closing(urllib.request.urlopen(request)) as r:
data = r.read()
return FeedResponse(data)
except (urllib.error.URLError, google.protobuf.message.DecodeError, ConnectionResetError) as e:
logger.error('Couldn\'t connect to MTA server: ' + str(e))
return False
def _update(self):
logger.info('updating...')
self._last_update = datetime.datetime.now(TZ)
# create working copy for thread safety
stations = copy.deepcopy(self._stations)
# clear old times
for id in stations:
stations[id].clear_train_data()
routes = defaultdict(set)
for i, feed_url in enumerate(self._FEED_URLS):
mta_data = self._load_mta_feed(feed_url)
if not mta_data:
continue
max_time = self._last_update + datetime.timedelta(minutes = self._MAX_MINUTES)
for entity in mta_data.entity:
trip = Trip(entity)
if not trip.is_valid():
continue
direction = trip.direction[0]
route_id = trip.route_id.upper()
for update in entity.trip_update.stop_time_update:
trip_stop = TripStop(update)
if trip_stop.time < self._last_update or trip_stop.time > max_time:
continue
stop_id = trip_stop.stop_id
if stop_id not in self._stops_to_stations:
logger.info('Stop %s not found', stop_id)
continue
station_id = self._stops_to_stations[stop_id]
stations[station_id].add_train(route_id,
direction,
trip_stop.time,
mta_data.timestamp)
routes[route_id].add(stop_id)
# sort by time
for id in stations:
stations[id].sort_trains(self._MAX_TRAINS)
with self._read_lock:
self._routes = routes
self._stations = stations
def last_update(self):
return self._last_update
def get_by_point(self, point, limit=5):
if self.is_expired():
self._update()
with self._read_lock:
sortable_stations = copy.deepcopy(self._stations).values()
sorted_stations = sorted(sortable_stations, key=lambda s: distance(s['location'], point))
serialized_stations = map(lambda s: s.serialize(), sorted_stations)
return list(islice(serialized_stations, limit))
def get_routes(self):
return self._routes.keys()
def get_by_route(self, route):
route = route.upper()
if self.is_expired():
self._update()
with self._read_lock:
out = [ self._stations[self._stops_to_stations[k]].serialize() for k in self._routes[route] ]
out.sort(key=lambda x: x['name'])
return out
def get_by_id(self, ids):
if self.is_expired():
self._update()
with self._read_lock:
out = [ self._stations[k].serialize() for k in ids ]
return out
def is_expired(self):
if self._THREADED and self.threader and self.threader.restart_if_dead():
return False
elif self._EXPIRES_SECONDS:
age = datetime.datetime.now(TZ) - self._last_update
return age.total_seconds() > self._EXPIRES_SECONDS
else:
return False