Iterating over lists of input params for use in Yelp API code

198 views Asked by At

this is my first time using stack overflow, and my first time using Python, so please forgive me if I provided more code than was necessary, or I'm asking the wrong questions. I'm still figuring out how the calls to the API work, so I provided all of the code that interacted with it. This code works, but only for a single string input in the params 'CATEGORY_1' and 'LOCATION'. I'm trying to figure out how to enter lists for each of these params so that it iterates through each of them, making many different url requests to the API, and printing out all of the results. I want the output to show each LOCATION input address from the list, then the corresponding API business search results for the categories 'bikes' and 'skateshops'. I'm uncertain where to put the for loops, and also it seems that the .replace attribute that I used does not work for list objects. Any help is appreciated!

Sincerely, Programming n00b

#Oauth credentials
CLIENT_ID = XXX
CLIENT_SECRET = XXX

# Yelp API Constants
API_HOST = 'https://api.yelp.com'
SEARCH_PATH = '/v3/businesses/search'
BUSINESS_PATH = '/v3/businesses/'  
TOKEN_PATH = '/oauth2/token'
GRANT_TYPE = 'client_credentials'

# Desired Search parameters
CATEGORY_1 = ['bikes','skateshops']
LOCATION = ['123 Meadow St, Portland, OR', '222 Spring St, Provo, UT', '808          
Williams Ave, Walla Walla, Washington']
RADIUS = 8050
SEARCH_LIMIT = 50

def obtain_bearer_token(host, path):
    url = '{0}{1}'.format(host, quote(path.encode('utf8')))
    assert CLIENT_ID, "Please supply your client_id."
    assert CLIENT_SECRET, "Please supply your client_secret."
    data = urlencode({
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'grant_type': GRANT_TYPE,
    })
    headers = {
        'content-type': 'application/x-www-form-urlencoded',
    }
    response = requests.request('POST', url, data=data, headers=headers)
    bearer_token = response.json()['access_token']
    return bearer_token

def request(host, path, bearer_token, url_params=None):
    url_params = url_params or {}
    url = '{0}{1}'.format(host, quote(path.encode('utf8')))
    headers = {
        'Authorization': 'Bearer %s' % bearer_token,
    }

    print(u'Querying {0} ...'.format(url))

    response = requests.request('GET', url, headers=headers,    
    params=url_params)

    return response.json()

def search(bearer_token, term, location):
    url_params = {
        'categories': term.replace(' ', '+'),
        'location': location.replace(' ', '+'),
        'radius': RADIUS,
        'limit': SEARCH_LIMIT
    }
    return request(API_HOST, SEARCH_PATH, bearer_token, url_params=url_params)

def get_business(bearer_token, business_id):
        business_path = BUSINESS_PATH + business_id
        return request(API_HOST, business_path, bearer_token)

def query_api(term, location):
    bearer_token = obtain_bearer_token(API_HOST, TOKEN_PATH)
    response = search(bearer_token, term, location)
    businesses = response.get('businesses')
    for x in businesses[:]:
    location=x['location']
    print('{0},{1},{2},{3},{4},{5},{6},{7}'.format(x['name'],location['addre
    ss1'],location['city'],location['state'],location['zip_code'],
    x['phone'],x['url'],term))
0

There are 0 answers