def get_departure_data(self, relevant_stops, route_number, must_stop_at=None, direction=None): """ Fetch the JSON data from the TfL website, for a dictionary of relevant_stops (each a BusStop object) and a particular route_number, and returns a DepartureCollection containing Bus objects must_stop_at and direction are ignored; filtering by direction has already been done by process_individual_request() """ stop_directions = dict([(run, heading_to_direction(stop.heading)) for (run, stop) in relevant_stops.items()]) departures = DepartureCollection() for (run, stop) in relevant_stops.items(): tfl_url = self.urls.BUS_URL % stop.number bus_data = self.browser.fetch_json(tfl_url) departures[stop] = parse_bus_data(bus_data, route_number) if departures[stop]: logging.debug("Stop %s produced buses: %s", stop.get_clean_name(), ', '.join([str(bus) for bus in departures[stop]])) else: logging.debug("Stop %s produced no buses", stop.get_clean_name()) # If the number of runs is 3 or more, get rid of any without buses shown if len(departures) > 2: logging.debug("Number of runs is %s, removing any non-existent entries", len(departures)) for run in range(3, max(relevant_stops.keys()) + 1): if run in relevant_stops.keys() and not departures[relevant_stops[run]]: del departures[relevant_stops[run]] null_constructor = lambda stop: NullDeparture(stop_directions[stop.run]) departures.cleanup(null_constructor) return departures
def test_dataparsers(self): """ Unit tests for Data parsers objects """ # Check against our test data and make sure we are correctly parsing & fetching the right objects from the data bus_data = parse_bus_data(self.bot.browser.fetch_json(self.bot.urls.BUS_URL % "53410"), '15') self.assertEqual(bus_data[0], Bus("Regent Street", gmt_to_localtime("1831"))) tube_data = parse_tube_data(self.bot.browser.fetch_xml_tree(self.bot.urls.TUBE_URL % ("D", "ECT")), RailStation("Earl's Court"), "D") self.assertEqual(tube_data["Eastbound"][0], TubeTrain("Edgware Road", "Eastbound", "2139", "D", "075")) dlr_data = parse_dlr_data(self.bot.browser.fetch_xml_tree(self.bot.urls.DLR_URL % "pop"), RailStation("Poplar")) self.assertEqual(dlr_data['P1'][0], Train("Beckton", "2107"))