def _route_info(self, route, number): url = '{0}{1}'.format(Bart.url, 'route.aspx') params = dict(Bart.params) params['cmd'] = 'routeinfo' params['route'] = number resp = self.session.get(url, params=params) data = parse(resp.content)['root']['routes']['route'] # list of stations this route passes abbrs = data['config']['station'] # will block if we don't already have an answer all_stops = self._all_stops() stop_ids = [] stops = {} # dest dest = abbrs[-1] for abbr in abbrs: # copy over the relevant stations stop = all_stops[abbr] stop = Stop(agency=route.agency, id=Stop.create_id(route.agency.id, '{0}-{1}'.format(abbr, dest)), name=stop.name, lat=stop.lat, lon=stop.lon, type=stop.type) stop_ids.append(stop.id) stops[stop.id] = stop direction = Direction(route=route, id=Direction.create_id(route.id, number), name=data['name'].split(' - ')[1]) direction.stop_ids = stop_ids return (direction, stops)
def stops(self, route): params = {'command': 'routeConfig', 'a': route.agency.get_id(), 'r': route.get_id()} resp = self.session.get(self.url, params=params) data = parse(resp.content)['body']['route'] stops = {} for stop in data['stop']: tag = stop['@tag'] code = stop.get('@stopId', None) id = Stop.create_id(route.agency.id, tag) stop = Stop(agency=route.agency, id=id, name=stop['@title'], code=code, lat=stop['@lat'], lon=stop['@lon']) stops[stop.id] = stop directions = [] ds = data['direction'] if isinstance(ds, OrderedDict): # there's only one direction, xmltodict doesn't return an array ds = [ds] for direction in ds: if direction['@useForUI'] != 'true': continue stop_ids = [Stop.create_id(route.agency.id, stop['@tag']) for stop in direction['stop']] id = Direction.create_id(route.id, direction['@tag']) direction = Direction(route=route, id=id, name=direction['@title']) direction.stop_ids = stop_ids directions.append(direction) return (directions, stops)
def stops(self, route): params = {"command": "routeConfig", "a": route.agency.get_id(), "r": route.get_id()} resp = self.session.get(self.url, params=params) data = parse(resp.content)["body"]["route"] stops = {} for stop in data["stop"]: tag = stop["@tag"] code = stop.get("@stopId", None) id = Stop.create_id(route.agency.id, tag) stop = Stop(agency=route.agency, id=id, name=stop["@title"], code=code, lat=stop["@lat"], lon=stop["@lon"]) stops[stop.id] = stop directions = [] ds = data["direction"] if isinstance(ds, OrderedDict): # there's only one direction, xmltodict doesn't return an array ds = [ds] for direction in ds: if direction["@useForUI"] != "true": continue stop_ids = [Stop.create_id(route.agency.id, stop["@tag"]) for stop in direction["stop"]] id = Direction.create_id(route.id, direction["@tag"]) direction = Direction(route=route, id=id, name=direction["@title"]) direction.stop_ids = stop_ids directions.append(direction) return (directions, stops)
def stops(self, route): 'returns a tuple, with a list of directions, and a map of stops' url = '{0}/stops-for-route/{1}.json' \ .format(self.url, self._decode_id(route.get_id())) params = dict(self.params) params['version'] = 2 resp = self.session.get(url, params=params) # TODO: stops can be shared by agencies, but the first one to see it # will get it here :( data = resp.json()['data'] stops = {} for stop in data['references']['stops']: id = self._encode_id(Stop.create_id(route.agency.id, stop['id'])) stop = Stop(agency=route.agency, id=id, name=stop['name'], lat=stop['lat'], lon=stop['lon'], code=stop['code'], type=stop_types[int(stop['locationType'])]) stops[stop.id] = stop directions = [] for stop_groupings in data['entry']['stopGroupings']: for stop_group in stop_groupings['stopGroups']: id = self._encode_id(Direction.create_id(route.id, stop_group['id'])) direction = Direction(route=route, id=id, name=stop_group['name']['name']) direction.stop_ids = \ [self._encode_id(Stop.create_id(route.agency.id, sid)) for sid in stop_group['stopIds']] directions.append(direction) return (directions, stops)
def _stop_arrivals(self, stop): params = {'command': 'predictions', 'a': stop.agency.get_id(), 'stopId': stop.code} resp = requests.get(self.url, params=params) arrivals = [] for predictions in parse(resp.content)['body']['predictions']: route_id = Route.create_id(stop.agency.id, predictions['@routeTag']) if 'direction' not in predictions: continue directions = predictions['direction'] if isinstance(directions, OrderedDict): directions = [directions] for direction in directions: predictions = direction['prediction'] if isinstance(predictions, OrderedDict): predictions = [predictions] for prediction in predictions: dir_id = Direction.create_id(route_id, prediction['@dirTag']) departure = prediction['@isDeparture'] == 'true' arrivals.append(Arrival(stop=stop, away=int(prediction['@seconds']), direction_id=dir_id)) arrivals.sort(key=attrgetter('away')) return arrivals
def _stop_arrivals(self, stop): url = 'http://bustime.mta.info/api/siri/stop-monitoring.json' # shares api keys with onebus params = dict(self.params) params['MonitoringRef'] = stop.get_id().split('_')[1] arrivals = [] resp = requests.get(url, params=params) data = resp.json()['Siri']['ServiceDelivery'] data = data['StopMonitoringDelivery'][0] for visit in data['MonitoredStopVisit']: visit = visit['MonitoredVehicleJourney'] did = Direction.create_id(Route.create_id(stop.agency.id, visit['LineRef']), visit['DirectionRef']) visit = visit['MonitoredCall'] away = visit['Extensions']['Distances']['DistanceFromCall'] arrivals.append(Arrival(stop=stop, away=int(away), unit=arrival_units[1], direction_id=did)) return arrivals
def _stop_arrivals(self, stop): url = 'http://bustime.mta.info/api/siri/stop-monitoring.json' # shares api keys with onebus params = dict(self.params) params['MonitoringRef'] = stop.get_id().split('_')[1] arrivals = [] resp = requests.get(url, params=params) data = resp.json()['Siri']['ServiceDelivery'] data = data['StopMonitoringDelivery'][0] for visit in data['MonitoredStopVisit']: visit = visit['MonitoredVehicleJourney'] did = Direction.create_id( Route.create_id(stop.agency.id, visit['LineRef']), visit['DirectionRef']) visit = visit['MonitoredCall'] away = visit['Extensions']['Distances']['DistanceFromCall'] arrivals.append( Arrival(stop=stop, away=int(away), unit=arrival_units[1], direction_id=did)) return arrivals
def stops(self, route): directions = defaultdict(list) trip_names = defaultdict(list) rid = route.get_id() directory = join('data', route.agency.id) trip_stops = self._trip_stops(directory) direction_stops = defaultdict(list) trips = filter(lambda c: rid == c['route_id'], self._trips(directory)) for i, t in enumerate(trips): tid = t['direction_id'] direction_stops[tid].append(trip_stops[t['trip_id']]) trip_names[tid].append(t['trip_headsign']) all_stops = self._stops(directory) directions = [] stops = {} for d, ts in direction_stops.items(): did = Direction.create_id(route.id, str(d)) # TODO: pick the most common name? direction = Direction(route=route, id=did, name=trip_names[d][0]) # pick the largest set of stops, hopefully that'll cover everything stop_ids = [] for sid in max(ts, key=len): stop = all_stops[sid] sid = Stop.create_id(route.agency.id, sid) stop_type = stop.get('location_type', None) if stop_type: stop_type = stop_types[int(stop_type)] stop = Stop(agency=route.agency, id=sid, name=stop['stop_name'], lat=stop['stop_lat'], lon=stop['stop_lon'], type=stop_type, code=stop.get('code', None)) stop_ids.append(stop.id) stops[stop.id] = stop direction.stop_ids = stop_ids directions.append(direction) return (directions, stops)
def stops(self, route): 'returns a tuple, with a list of directions, and a map of stops' url = '{0}/stops-for-route/{1}.json' \ .format(self.url, self._decode_id(route.get_id())) params = dict(self.params) params['version'] = 2 resp = self.session.get(url, params=params) # TODO: stops can be shared by agencies, but the first one to see it # will get it here :( data = resp.json()['data'] stops = {} for stop in data['references']['stops']: id = self._encode_id(Stop.create_id(route.agency.id, stop['id'])) stop = Stop(agency=route.agency, id=id, name=stop['name'], lat=stop['lat'], lon=stop['lon'], code=stop['code'], type=stop_types[int(stop['locationType'])]) stops[stop.id] = stop directions = [] for stop_groupings in data['entry']['stopGroupings']: for stop_group in stop_groupings['stopGroups']: id = self._encode_id( Direction.create_id(route.id, stop_group['id'])) direction = Direction(route=route, id=id, name=stop_group['name']['name']) direction.stop_ids = \ [self._encode_id(Stop.create_id(route.agency.id, sid)) for sid in stop_group['stopIds']] directions.append(direction) return (directions, stops)
def _stop_arrivals(self, stop): params = {"command": "predictions", "a": stop.agency.get_id(), "stopId": stop.code} resp = requests.get(self.url, params=params) arrivals = [] for predictions in parse(resp.content)["body"]["predictions"]: route_id = Route.create_id(stop.agency.id, predictions["@routeTag"]) if "direction" not in predictions: continue directions = predictions["direction"] if isinstance(directions, OrderedDict): directions = [directions] for direction in directions: predictions = direction["prediction"] if isinstance(predictions, OrderedDict): predictions = [predictions] for prediction in predictions: dir_id = Direction.create_id(route_id, prediction["@dirTag"]) departure = prediction["@isDeparture"] == "true" arrivals.append(Arrival(stop=stop, away=int(prediction["@seconds"]), direction_id=dir_id)) arrivals.sort(key=attrgetter("away")) return arrivals