def search(self, term): """All stops and lines that match the search term. Arguments: term: serch term Returns: list of Stops & Lines """ path = "/v2/search/%s" % urllib.quote(term) data = self._api_request(path) stop_factory = StopFactory(self) line_factory = LineFactory(self) out = [] for result in data: if result['type'] == 'stop': out.append(stop_factory.create(**result['result'])) elif result['type'] == 'line': out.append(line_factory.create(**result['result'])) else: out.append(result) return out
def transport_pois_by_map(self, poi, location1, location2, griddepth, limit=20): """Return a list of points of interest within a map grid defined by location1 & location2 Arguments: poi: either a transport mode or outlet. A list of poi types can be passed in as a comma separated location1 & location2: - are one of (lat, lon), a Location object, or something that has a location property (which would be a Location object). - define the top left corner (location1) and bottom right corner (location2) of a rectangle on a map griddepth: number of cell blocks per cluster limit: minimum number of POIs required to create a cluster as well as the maximum number of POIs returned Returns: a dictionary in the format, the primary components being in the 'locations' value {'locations': [ Stop or Outlet objects ], '...' } """ lat1, lon1 = parse_location(location1) lat2, lon2 = parse_location(location2) base_path = "/v2/poi/{poi}/lat1/{lat1}/long1/{lon1}/" + \ "lat2/{lat2}/long2/{lon2}/" + \ "griddepth/{griddepth}/limit/{limit}" poi_ids = ','.join([str(self.MODES[p]) for p in poi.split(',')]) path = base_path.format(poi=poi_ids, lat1=lat1, lon1=lon1, lat2=lat2, lon2=lon2, griddepth=griddepth, limit=limit) data = self._api_request(path) stop_factory = StopFactory(self) outlet_factory = OutletFactory(self) out = {} for k, v in data.items(): if k == "locations": out['locations'] = [] for location in v: # either a Stop of an Outlet if 'transport_type' in location: item = stop_factory.create(**location) else: outlet_type = location.pop('outlet_type') item = outlet_factory.create(transport_type=outlet_type, **location) out['locations'].append(item) else: out[k] = v return out
def _process_departures(self, departures): """ common reponse parser for handling a list of departures """ line_factory = LineFactory(self) stop_factory = StopFactory(self) run_factory = RunFactory(self) out = [] for departure in departures: # - platform # -- direction # --- line platform_details = departure['platform'] direction_details = platform_details.pop('direction') line_details = direction_details.pop('line') line = line_factory.create(**line_details) direction_details['line'] = line direction = Direction(**direction_details) platform_details['direction'] = direction # --- stop stop_details = platform_details.pop('stop') stop = stop_factory.create(**stop_details) platform_details['stop'] = stop platform = Platform(**platform_details) # - run run_details = departure['run'] run = run_factory.create(**run_details) timetable = parse_datetime_tz(departure["time_timetable_utc"]) if departure["time_realtime_utc"] is not None: realtime = parse_datetime_tz(departure["time_realtime_utc"]) else: realtime = None if departure['flags']: flags = ', '.join([self.FLAGS[f] for f in departure['flags'].split('-') if f != 'E']) else: flags = None out.append({"platform": platform, "run": run, "flags": flags, "time_timetable_utc": timetable, "time_realtime_utc": realtime, }) return out
def stops_nearby(self, location, mode=None, limit=None, with_distance=False): """Return stops near a location. Args: location: one of (lat, lon), a Location object, or something that has a location property (which would be a Location object) mode: (optional) filter results for only this tramsport mode limit: (optional) only return this many results with_distance: (optional) return tuples of (Stop, distance) Returns: List of stops or a list of tuples in the form (stop, distance) if with_distance is True """ base_path = "/v2/nearme/latitude/{lat}/longitude/{lon}" lat, lon = parse_location(location) path = base_path.format(lat=lat, lon=lon) stops = self._api_request(path) stop_factory = StopFactory(self) out = [stop_factory.create(**stop['result']) for stop in stops] # only provide certain stop types if we are provided with a mode if mode is not None: out = [stop for stop in out if stop.transport_type == mode] # enforce limit if provided if limit is not None: out = out[:limit] # convert into tuple of (Stop, distance) if with_distance: out = [(stop, location.distance(stop.location)) for stop in out] return out
def stops_on_a_line(self, mode, line): """All stops for a particular transport mode on a given line Arguments: mode: transport mode line: the line_id of a particular line Returns: List of stops. """ base_path = "/v2/mode/{mode}/line/{line}/stops-for-line" mode_id = self.MODES[mode] path = base_path.format(mode=mode_id, line=line) data = self._api_request(path) stop_factory = StopFactory(self) out = [] for line in data: out.append(stop_factory.create(**line)) return out