def generate_categories_file(): """ Accesses Foursquare, reads the hierarchical list of categories and outputs the deserialised python objects to the file 'categories.json' in the working directory """ import _credentials from api import APIGateway, APIWrapper client_id = _credentials.client_id client_secret = _credentials.client_secret client_tuples = [(client_id, client_secret)] access_tokens = _credentials.access_tokens gateway = APIGateway( access_tokens, 500, client_tuples, 5000 ) api = APIWrapper( gateway ) # read the category list from Foursquare response = api.query_routine("venues", "categories") response = response['response'] # dump the json output to file f = open('categories.json', 'w') json.dump(response, f) f.close()
def __init__(self, db_name='fsqexp'): self.gateway = APIGateway(access_token, 500, [client_id, client_secret], 5000) self.wrapper = APIWrapper(self.gateway) self.params = {'v': 20140713} self.cache = MongoDBCache(db=db_name)
def callback( request ): request_token = request.GET.get( 'token' ) api_sig = hashlib.md5('api_key%smethodauth.getSessiontoken%s%s' % ( API_KEY, request_token, API_SECRET ) ).hexdigest( ) params = { 'method' : 'auth.getSession', 'api_key' : API_KEY, 'token' : request_token, 'api_sig' : api_sig, 'format' : 'json', } data = urllib.urlencode( params ) req = urllib2.Request( API_URL + '?' + data ) response = urllib2.urlopen( req ) session_key = json.loads( response.read( ) )['session']['key'] api = APIWrapper( session_key, API_KEY, API_SECRET ) data = api.query( 'user.getinfo', {}, authenticated=True ) print data try: user = User.objects.get( username = data['user']['name'] ) except User.DoesNotExist: user = User.objects.create_user( username = data['user']['name'], email='', password='' ) user.save() images = data['user']['image'] for image in images: if image['size'] == u'large': img = image['#text'] user, created = LastFMUser.objects.get_or_create( user=user, defaults={ 'url' : data['user']['url'], 'image' : img, 'playcount' : data['user']['playcount'], 'age' : data['user']['age']} ) user = authenticate( username=data['user']['name'], password='' ) login( request, user ) return HttpResponseRedirect( reverse( 'auth_main' ) )
def __init__(self): self.gateway = APIGateway(access_token, 500, [client_id, client_secret], 5000) self.wrapper = APIWrapper(self.gateway) self.params = { 'v' : 20140713 } self.cache = JSONFileCache(timedelta(days=1))
def __init__(self, db_name='fsqexp'): self.gateway = APIGateway(access_token, 500, [client_id, client_secret], 5000) self.wrapper = APIWrapper(self.gateway) self.params = { 'v' : 20140713 } self.cache = MongoDBCache(db=db_name)
class VenueSearcher: def __init__(self, db_name='fsqexp'): self.gateway = APIGateway(access_token, 500, [client_id, client_secret], 5000) self.wrapper = APIWrapper(self.gateway) self.params = { 'v' : 20140713 } self.cache = MongoDBCache(db=db_name) def venue_has_chain_property(self, venue): if venue.get('page', None) is not None: if venue['page'].get('user', None) is not None: if venue['page']['user'].get('type', None) is not None: return venue['page']['user']['type'] == 'chain' return False def global_search(self, query, check_fresh=False): params = {} params['v'] = self.params['v'] params['intent'] = 'global' params['limit'] = 50 params['query'] = query if self.cache.document_exists('global_searches', {'params': params}, check_fresh): results = self.cache.get_document('global_searches', {'params': params}, check_fresh) return results['response']['venues'] else: try: results = self.wrapper.query_routine('venues', 'search', params, True) if not results is None: results['params'] = params self.cache.put_document('global_searches', results) return results['response']['venues'] except urllib2.HTTPError, e: pass except urllib2.URLError, e: pass
class VenueSearcher: def __init__(self): self.gateway = APIGateway(access_token, 500, [client_id, client_secret], 5000) self.wrapper = APIWrapper(self.gateway) self.params = { 'v' : 20140713 } self.cache = JSONFileCache(timedelta(days=1)) def venue_has_chain_property(self, venue): if venue.get('page', None) is not None: if venue['page'].get('user', None) is not None: if venue['page']['user'].get('type', None) is not None: return venue['page']['user']['type'] == 'chain' return False def global_search(self, query): params = {} params['v'] = self.params['v'] params['intent'] = 'global' params['limit'] = 50 params['query'] = query if self.cache.file_exists('%s_global.json' % (query.replace('/', ''))): results = self.cache.get_json('%s_global.json' % (query.replace('/', ''))) return results['response']['venues'] else: try: results = self.wrapper.query_routine('venues', 'search', params, True) if not results is None: self.cache.put_json(results, '%s_global.json' % (query.replace('/', ''))) return results['response']['venues'] except urllib2.HTTPError, e: pass except urllib2.URLError, e: pass
class VenueSearcher: def __init__(self, db_name='fsqexp'): self.gateway = APIGateway(access_token, 500, [client_id, client_secret], 5000) self.wrapper = APIWrapper(self.gateway) self.params = {'v': 20140713} self.cache = MongoDBCache(db=db_name) def venue_has_chain_property(self, venue): if venue.get('page', None) is not None: if venue['page'].get('user', None) is not None: if venue['page']['user'].get('type', None) is not None: return venue['page']['user']['type'] == 'chain' return False def global_search(self, query, check_fresh=False): params = {} params['v'] = self.params['v'] params['intent'] = 'global' params['limit'] = 50 params['query'] = query if self.cache.document_exists('global_searches', {'params': params}, check_fresh): results = self.cache.get_document('global_searches', {'params': params}, check_fresh) return results['response']['venues'] else: try: results = self.wrapper.query_routine('venues', 'search', params, True) if not results is None: results['params'] = params self.cache.put_document('global_searches', results) return results['response']['venues'] except urllib2.HTTPError, e: pass except urllib2.URLError, e: pass
error_detail ) raise FoursquareRequestError( response_code, error_type, error_detail ) if self.query_interval is not None: self.earliest_query_time = time.time() + self.query_interval # # Fin return py_data if __name__ == "__main__": import _credentials from api import APIWrapper client_id = _credentials.client_id client_secret = _credentials.client_secret gateway = VenueAPIGateway( client_id=client_id, client_secret=client_secret ) api = APIWrapper( gateway ) print "Grab info about a given venue..." reply = api.query_resource( "venues", "591313" ) data = reply['response'] print data print print "Search for venues near a given location..." venues = api.find_venues_near( 51.4777, -3.1844 ) for v in venues: print v['name']