Пример #1
0
    def get(self):
      
      # validate the request parameters
      devStoreKey = validateRequest(self.request,utils.GETROUTES)
      if devStoreKey is None:
          logging.debug("unable to validate the request parameters")
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Illegal request parameters')))
          return
      
      logging.debug('getroutes request...  ')
      if self.request.get('force') is not '':
          refresh = True
      else:
          refresh = False
      
      if utils.afterHours() is True:
          # don't run these jobs during "off" hours
	      json_response = utils.buildErrorResponse('-1','The Metro service is not currently running')
      else:
          if refresh is True:
              json_response = getRoutes(refresh)

              # drop it into the memcache again
              memcache.set(utils.GETROUTES, json_response)
              logging.debug('---> storing in memcache');
          else:
              logging.debug('---> memcache hit');
              json_response = memcache.get(utils.GETROUTES)
              if json_response is None:
                  json_response = getRoutes(refresh)
        
                  # drop it into the memcache again
                  memcache.set(utils.GETROUTES, json_response)
                  logging.debug('---> storing in memcache');
                  

          # record the API call for this devkey              
          utils.recordDeveloperRequest(devStoreKey,utils.GETROUTES,self.request.query_string,self.request.remote_addr);

      #logging.debug('API: json response %s' % json_response);    
      callback = self.request.get('callback')
      if callback is not '':
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.headers['Access-Control-Allow-Origin'] = '*'
          self.response.headers['Access-Control-Allow-Methods'] = 'GET'
          response = callback + '(' + simplejson.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = json_response
      
      self.response.out.write(response)
Пример #2
0
    def get(self):
      
      if utils.afterHours() is True:
          # don't run these jobs during "off" hours
	      json_response = utils.buildErrorResponse('-1','The Metro service is not currently running')

      # validate the request parameters
      devStoreKey = validateRequest(self.request)
      if devStoreKey is None:
          logging.error("failed to validate the request paramters")
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Unable to validate the request. There may be an illegal developer key.')))
          return

      # snare the inputs
      stopID = utils.conformStopID(self.request.get('stopID'))
      routeID = self.request.get('routeID')
      vehicleID = self.request.get('vehicleID')
      logging.debug('getarrivals request parameters...  stopID %s routeID %s vehicleID %s' % (stopID,routeID,vehicleID))
      
      if stopID is not '' and routeID is '':
          json_response = stopRequest(stopID, devStoreKey)
          utils.recordDeveloperRequest(devStoreKey,utils.GETARRIVALS,self.request.query_string,self.request.remote_addr);
      elif stopID is not '' and routeID is not '':
          json_response = stopRouteRequest(stopID, routeID, devStoreKey)
          utils.recordDeveloperRequest(devStoreKey,utils.GETARRIVALS,self.request.query_string,self.request.remote_addr);
      elif routeID is not '' and vehicleID is not '':
          json_response = routeVehicleRequest(routeID, vehicleID, devStoreKey)
          utils.recordDeveloperRequest(devStoreKey,utils.GETVEHICLE,self.request.query_string,self.request.remote_addr);
      else:
          logging.debug("API: invalid request")
          utils.recordDeveloperRequest(devStoreKey,utils.GETARRIVALS,self.request.query_string,self.request.remote_addr,'illegal query string combination');
          json_response = utils.buildErrorResponse('-1','Invalid Request parameters')

      # encapsulate response in json or jsonp
      logging.debug('API: json response %s' % json_response);

      callback = self.request.get('callback')
      if callback is not '':
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.headers['Access-Control-Allow-Origin'] = '*'
          self.response.headers['Access-Control-Allow-Methods'] = 'GET'
          response = callback + '(' + simplejson.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = simplejson.dumps(json_response)
      
      self.response.out.write(response)
Пример #3
0
 def get(self):
   
   # validate the request parameters
   devStoreKey = validateRequest(self.request)
   if devStoreKey is None:
       logging.warning("API: unsupported method")
       self.response.headers['Content-Type'] = 'application/javascript'
       self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','This method is not yet enabled')))
       return
Пример #4
0
    def get(self):
      
      # validate the request parameters
      devStoreKey = validateRequest(self.request,utils.GETSTOPS)
      if devStoreKey is None:
          logging.debug("unable to validate the request parameters")
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Illegal request parameters')))
          return
      
      # snare the inputs
      stopID = utils.conformStopID(self.request.get('stopID'))
      routeID = self.request.get('routeID')
      destination = self.request.get('destination').upper()
      logging.debug('getstops request parameters...  routeID %s destination %s' % (routeID,destination))
      
      if utils.afterHours() is True:
          # don't run these jobs during "off" hours
	      json_response = utils.buildErrorResponse('-1','The Metro service is not currently running')
      elif routeID is not '' and destination is '':
          json_response = routeRequest(routeID, None)
          utils.recordDeveloperRequest(devStoreKey,utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      elif routeID is not '' and destination is not '':
          json_response = routeRequest(routeID, destination)
          utils.recordDeveloperRequest(devStoreKey,utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      else:
          logging.error("API: invalid request")
          json_response = utils.buildErrorResponse('-1','Invalid Request parameters. Did you forget to include a routeID?')
          utils.recordDeveloperRequest(devStoreKey,utils.GETSTOPS,self.request.query_string,self.request.remote_addr,'illegal query string combination');

      #logging.debug('API: json response %s' % json_response);    
      # encapsulate response in json
      callback = self.request.get('callback')
      if callback is not '':
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.headers['Access-Control-Allow-Origin'] = '*'
          self.response.headers['Access-Control-Allow-Methods'] = 'GET'
          response = callback + '(' + simplejson.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = simplejson.dumps(json_response)
      
      self.response.out.write(response)
Пример #5
0
def routeRequest(routeID):
    loop = 0
    done = False
    result = None
    while not done and loop < 3:
        try:
          url = VEHICLE_URL_BASE + routeID
          result = urlfetch.fetch(url)
          done = True;
        except urlfetch.DownloadError:
          logging.error("Error loading page (%s)... sleeping" % loop)
          if result:
            logging.debug("Error status: %s" % result.status_code)
            logging.debug("Error header: %s" % result.headers)
            logging.debug("Error content: %s" % result.content)
          time.sleep(6)
          loop = loop+1
           
    if result is None or result.status_code != 200:
        logging.error("Exiting early: error fetching URL: " + result.status_code)
        return utils.buildErrorResponse('-1','Error reading live Metro feed')
    
    dataArray = result.content.split('*')
    logging.debug('timestamp is %s' % dataArray[0])
    timestamp = dataArray[0]
    
    vehicles = dataArray[2].split(';')
    results = dict({'status' : 0,
                    'routeID' : routeID,
                    'count' : len(vehicles)-1, 
                    'timestamp' : timestamp, 
                    'vehicles' : list()
                    })
    for v in vehicles:
        if v == vehicles[-1]:
            break
        location = v.split('|')
        next = location[3].split('<br>')
        spot = dict({'lat':location[0],
                     'lon':location[1],
                     'direction':re.sub('<[^>]*>', '', next[0]),
                     'vehicleID':next[1].split(':')[1].lstrip(),
                     'nextStop':next[2].split(':')[1].lstrip()
                   })
        results['vehicles'].append(spot)
    
    return results   
Пример #6
0
    def get(self):
      
      # validate the request parameters
      devStoreKey = validateRequest(self.request,utils.GETNEARBYSTOPS)
      if devStoreKey is None:
          logging.error("unable to validate the request parameters")
          self.response.headers['Content-Type'] = 'application/json'
          self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Illegal request parameters')))
          return
      
      # snare the inputs
      lat = float(self.request.get('lat'))
      lon = float(self.request.get('lon'))
      radius = self.request.get('radius')
      if radius == '':
          radius = 500
      else:
          radius = int(radius)
      routeID = self.request.get('routeID')
      destination = self.request.get('destination')
      
      # stop location requests...
      json_response = nearbyStops(lat,lon,radius,routeID)

      # encapsulate response in json
      #logging.debug('API: json response %s' % response);
      callback = self.request.get('callback')
      if callback is not '':
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.headers['Access-Control-Allow-Origin'] = '*'
          self.response.headers['Access-Control-Allow-Methods'] = 'GET'
          response = callback + '(' + simplejson.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = simplejson.dumps(json_response)

      self.response.out.write(response)
Пример #7
0
 def post(self):
     self.response.headers['Content-Type'] = 'application/javascript'
     self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','The API does not support POST requests')))
     return