yelp.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # pylint: disable=invalid-name
  2. '''
  3. Yelp.py contains methods for
  4. authenticating the user and
  5. retrieving data from Yelp's API.
  6. '''
  7. import simplejson as json
  8. import oauth2
  9. import requests
  10. # OAuth credential placeholders that must be filled in by users.
  11. CONSUMER_KEY = 'EXMisJNWez_PuR5pr06hyQ'
  12. CONSUMER_SECRET = 'VCK-4cDjtQ9Ra4HC5ltClNiJFXs'
  13. TOKEN = 'AWYVs7Vim7mwYyT1BLJA2xhNTs_vXLYS'
  14. TOKEN_SECRET = 'Rv4GrlYxYGhxUs14s0VBfk7JLJY'
  15. def requestData(location):
  16. '''
  17. Authenticates a request and returns
  18. data from Yelp API.
  19. '''
  20. data = []
  21. url = 'http://api.yelp.com/v2/business/' + location + '?'
  22. consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
  23. oauth_request = oauth2.Request(method="GET", url=url)
  24. oauth_request.update(
  25. {
  26. 'oauth_nonce': oauth2.generate_nonce(),
  27. 'oauth_timestamp': oauth2.generate_timestamp(),
  28. 'oauth_token': TOKEN,
  29. 'oauth_consumer_key': CONSUMER_KEY
  30. }
  31. )
  32. token = oauth2.Token(TOKEN, TOKEN_SECRET)
  33. oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
  34. signed_url = oauth_request.to_url()
  35. req = requests.get(signed_url)
  36. content = json.loads(req.content)
  37. data.append(content)
  38. return data