Skip to content

Instantly share code, notes, and snippets.

Created November 14, 2015 21:05
Show Gist options
  • Save anonymous/42ffdf5deccde0746fd1 to your computer and use it in GitHub Desktop.
Save anonymous/42ffdf5deccde0746fd1 to your computer and use it in GitHub Desktop.
def request(host, path, url_params=None):
"""Prepares OAuth authentication and sends the request to the API.
Args:
host (str): The domain host of the API.
path (str): The path of the API after the domain.
url_params (dict): An optional set of query parameters in the request.
Returns:
dict: The JSON response from the request.
Raises:
urllib2.HTTPError: An error occurs from the HTTP request.
"""
url_params = url_params or {}
url = 'https://{0}{1}?'.format(host, urllib.quote(path.encode('utf8')))
consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
oauth_request = oauth2.Request(
method="GET", url=url, parameters=url_params)
oauth_request.update(
{
'oauth_nonce': oauth2.generate_nonce(),
'oauth_timestamp': oauth2.generate_timestamp(),
'oauth_token': TOKEN,
'oauth_consumer_key': CONSUMER_KEY
}
)
token = oauth2.Token(TOKEN, TOKEN_SECRET)
oauth_request.sign_request(
oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
print oauth_request, "48"
signed_url = oauth_request.to_url()
print u'Querying {0} ...'.format(url)
print signed_url
print "-----"
conn = urllib2.urlopen(signed_url, None)
try:
response = json.loads(conn.read())
finally:
conn.close()
return response
def search_yelp(offset):
url_params = {
'term':'gyms', 'location': 'New York, NY', 'offset':offset, 'limit':SEARCH_LIMIT
}
print urllib.urlencode(url_params)
return request(API_HOST, SEARCH_PATH, url_params=url_params)
def query_api():
n_businesses = 1
df=pandas.DataFrame(data=None)
offset = 0
while (n_businesses > 0): #run the cycle till the API gets results back.
response = search_yelp(offset)
print response
offset = offset + 20
businesses = response.get('businesses')
df2 = pandas.DataFrame(businesses)
df = df.append(df2, ignore_index=True)
n_businesses = len(businesses)
print n_businesses
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment