Пример #1
0
    def get_api_result(self, *args, **kwargs):
        try:
            stop = Stop.objects.get(pk=int(kwargs['id']))
        except Stop.DoesNotExist:
            raise Http404
        
        if not stop.has_predictions:
            return stop

        jd = stop.json_dict()
        
        try:
            apis = get_apis()
            logger = logging.getLogger('predictions')

            predictions = {}
            for prediction in stop.predictions.all():
                api = apis.get(prediction.api_name, None)
                if not api:
                    continue

                api_predictions = api.get_predictions(prediction)
                for api_prediction in api_predictions:
                    
                    logdata = {
                        'stop_id': stop.id,
                        'api_name': prediction.api_name,
                        'api_stop_id': prediction.id,
                        'stop_name': prediction.name,
                        'route': api_prediction.route,
                        'destination': api_prediction.destination,
                        'wait': api_prediction.wait
                    }
                    logger.info('%(stop_id)s %(api_stop_id)s "%(stop_name)s" "%(route)s" "%(destination)s" "%(wait)s"' % logdata) 

                    route_dest_pair = (api_prediction.route, api_prediction.destination)
                    if route_dest_pair not in predictions:
                        predictions[route_dest_pair] = []
                    predictions[route_dest_pair] += [api_prediction.wait]
           
            if predictions:
                jd['predictions'] = [{
                    'route': k[0],
                    'destination': k[1],
                    'waits': v} for (k,v) in predictions.iteritems()]
                    
        except Exception as ex:
            pass
                
        return jd
Пример #2
0
    def handle_noargs(self, **options):
        self.warning = options['warning']
        self.dry_run = options['dry_run']

        if self.warning and not self.dry_run:
            confirm = raw_input(u"""
You have requested calling available transit APIs to get a list of stops.

Doing this will erase all existing stops stored for these APIs.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """)

            if confirm != 'yes':
                raise CommandError("Stop refreshing cancelled.")

        if not self.dry_run:
            Stop.objects.all().delete()

        apis = get_apis()
        for api in apis.values():
            self.stdout.write("Calling API '%s'.\n" % api.name)
            
            for k, v in api.options.iteritems():
                self.stdout.write("Using option: %s = %s\n" % (k, v))

            stops = api.get_all_stops()
            for stop in stops:
                self.stdout.write("Found stop: '%s'\n" % stop.name)
                    
                if not self.dry_run:
                    stop.save()

            self.stdout.write('\n')
        
        if self.dry_run:
            self.stdout.write("This was just a dry run!\n")
Пример #3
0
def predictions(request):
    stops_query = request.GET.get('stops', None)
    if stops_query is None:
        return HttpResponseBadRequest('Missing parameters')

    apis = get_apis() 

    stop_ids = [int(stop_id) for stop_id in stops_query.split(',')]
    stops = Stop.objects.filter(id__in=stop_ids)

    prediction_data = {}
    for stop in stops:
        api = apis.get(stop.api_name, None)
        if not api:
            continue
    
        predictions = api.get_predictions(stop)
        if not len(predictions):
            continue

        prediction_data[stop.id] = [p.json_dict() for p in predictions]
        
    data = json.dumps(prediction_data)
    return HttpResponse(data, content_type='application/json')