Ejemplo n.º 1
0
    def handle(self, *args, **options):
        nb = NextBus()
        for route_info in nb.get_route_list(settings.AGENCY_TAG):
            route_config = nb.get_route_config(settings.AGENCY_TAG, route_info['tag'])
            route_attributes = route_config['route_attributes']
            route = Route(
                tag=route_attributes['tag'],
                title=route_attributes['title'],
                color=route_attributes['color'],
                opposite_color=route_attributes['oppositeColor'],
                lat_min=route_attributes['latMin'],
                lat_max=route_attributes['latMax'],
                lon_min=route_attributes['lonMin'],
                lon_max=route_attributes['lonMax'],
            )
            route.save()

            for stop_info in route_config['stops']:
                if 'stopId' in stop_info:  # stopId can be absent from stop
                    stop_id = stop_info['stopId']
                else:
                    stop_id = None
                stop = Stop(
                    tag=stop_info['tag'],
                    stop_id=stop_id,
                    title=stop_info['title'],
                    lat=stop_info['lat'],
                    lon=stop_info['lon'],
                    route=route,
                )
                stop.save()

            for direction_info in route_config['directions']:
                direction = Direction(
                    name=direction_info['name'],
                    title=direction_info['title'],
                    tag=direction_info['tag'],
                    route=route,
                )
                direction.save()

                # Saving stops order
                position = 0
                for stop_tag in direction_info['stops']:
                    direction_stop = DirectionStop(
                        direction=direction,
                        stop=Stop.objects.filter(tag=stop_tag).first(),
                        position=position
                    )
                    direction_stop.save()
                    position += 1

            self.stdout.write("Loading route: %s" % route.title)
Ejemplo n.º 2
0
def collect_predictions():
    """Retrieve predictions for the routes that are monitored
    Be aware of NextBus API rate limits:
      - 2MB/20sec,
      - 150 stops per prediction
    """
    nb = NextBus()
    monitored_routes = Route.objects.filter(monitored=True)
    for route in monitored_routes:
        all_stops = route.stops.all()
        p = Paginator(all_stops, 150)  # Paginate routes with more than 150 stops
        for page_number in p.page_range:
            page = p.page(page_number)
            stops = page.object_list
            stop_tags = ['%s|%s' % (stop.route.tag, stop.tag) for stop in stops]
            predictions = nb.get_first_prediction_multi_stops(settings.AGENCY_TAG, stop_tags)
            for prediction in predictions:
                new_prediction = Prediction(
                    seconds=prediction[1],
                    stop=Stop.objects.filter(tag=prediction[0]).first()
                )
                new_prediction.save()
Ejemplo n.º 3
0
 def test_get_first_prediction_multi_stops(self):
     with HTTMock(nextbus_multistops_predictions_mock):
         nb = NextBus()
         predictions = nb.get_first_prediction_multi_stops("ttc", ["512|14295", "512|14688"])
         self.assertEqual(int(predictions[0][1]), 502)  # first prediction for first stop in fake dataset
Ejemplo n.º 4
0
 def test_get_predictions(self):
     with HTTMock(nextbus_predictions_mock):
         nb = NextBus()
         predictions = nb.get_predictions("ttc", "512", "14295")
         self.assertEqual(int(predictions[0]["seconds"]), 8)  # Fake dataset, fixed value
Ejemplo n.º 5
0
 def test_get_route_config(self):
     with HTTMock(nextbus_route_config_mock):
         nb = NextBus()
         route_config = nb.get_route_config("ttc", "512")
         self.assertEqual(len(route_config["directions"]), 2)
         self.assertEqual(len(route_config["stops"]), 6)
Ejemplo n.º 6
0
 def test_get_route_list(self):
     with HTTMock(nextbus_route_list_mock):
         nb = NextBus()
         routes = nb.get_route_list("ttc")
         self.assertEqual(len(routes), 6)
Ejemplo n.º 7
0
 def test_get_agency_list(self):
     with HTTMock(nextbus_agency_list_mock):
         nb = NextBus()
         agencies = nb.get_agency_list()
         self.assertEqual(len(agencies), 2)