Harnessing the Google, Yelp, and Foursquare APIs with Python

JW

Jonathan Wong / December 15, 2015

4 min read––– views

Data is a beautiful thing. Especially when we can sort, organize, and manipulate it to our needs. In this post, I’m going to walk you through harnessing the power of the Google Places, Yelp, and Foursquare APIs to create a list of businesses in your area.

Development#

Let’s create a Business model to represent the data we’ll retrieve from our sources. Most of the parameters to create a Business are pretty straight forward. If you’re comparing businesses between data sources, you’ll find that the naming conventions aren’t equal. To alleviate this issue, you can use the address as a constant between the businesses. You could also add other attributes to suit your needs, like a phone number or website link.

class Business:
  def __init__(self, name, address, rating,
               rating_count, checkin_count):
    self.name = name
    self.address = address
    self.rating = rating
    self.rating_count = rating_count
    self.checkin_count = checkin_count

We’ll be using the urllib2 library to make HTTP requests. Let’s create a helper function to reduce repeated code.

def make_request(url):
    with closing(urlopen(url)) as response:
        return json.loads(response.read())
return data

I’ll be focusing on explaining code snippets in the following section. If you’d like to jump straight to the completed code, here’s the link.

def make_request(url):
    with closing(urlopen(url)) as response:
        return json.loads(response.read())
return data

APIs#

Foursquare#

Before diving in, we must first obtain our client keys to access the API. You can find those here after creating an application. Next, let’s look at the different endpoints we can access. The explore endpoint allows us to find venues around a given location. This endpoint contains venue rating information as well as check-in counts.

https://api.foursquare.com/v2/venues/explore

Let’s give it some parameters to narrow our search:

  • ll: Latitude and longitude of the user’s location.
  • radius: Search radius in meters.
  • categoryId: Limit results to a specific category (e.g. “bars”).

Combining these parameters with our client keys, we can make a request to our newly created URL and retrieve a list of venues:

venue_list = []
try:
    data = make_request(url)
for item in data['response']['groups'][0]['items']:
    venue = item['venue']
    venue_list.append(Business(
        venue['name'],
        venue['location']['address'],
        venue['rating'],
        venue['ratingSignals'],
        venue['stats']['checkinsCount']))
except Exception, e:
    print e

After receiving our JSON response, we can iterate through the dictionary to create our array of Business objects containing our desired information.

Yelp#

Following suite with Foursquare, we must first obtain our consumer keys before we can access the API. You can find those here after logging in. I tried to refrain from using external libraries to reduce installation requirements, but ultimately ended up needing rauth. To install simply run:

pip install rauth

We’ll be using the Search API to obtain a list of businesses in our area.

https://api.yelp.com/v2/search

Once again, let’s give the URL some parameters to narrow our search:

  • ll: Latitude and longitude of the user’s location.
  • radius_filter: Search radius in meters. Max distance 40000 meters.
  • term: Limit results to a specific category (e.g. “bars”).

To obtain our desired information, we first need to create an authenticated session. Next, we make a request to our newly created URL and once again parse the JSON response.

session = rauth.OAuth1Session(
    consumer_key = CONSUMER_KEY,
    consumer_secret = CONSUMER_SECRET,
    access_token = TOKEN,
    access_token_secret = TOKEN_SECRET)

request = session.get(url, params=params)
data = request.json()
session.close()
business_list = []
for business in data['businesses']:
    address = business['location']['display_address'][0]
    business_list.append(Business(
        business['name'],
        address,
        business['rating'],
        business['review_count'],
        'N/A'))

Google Places#

You’ve got the hang of this now, right? Obtain the keys, read the documentation, and find our endpoint.

pip install rauth

We’ll be using the Search API to obtain a list of businesses in our area.

https://maps.googleapis.com/maps/api/place/nearbysearch/json?params

We need to supply these parameters:

  • location: Latitude and longitude of the user’s location.
  • radius: Search radius in meters. Max distance 50000 meters.
  • type: Limit results to a specific category (e.g. “bars”).
place_list = []
try:
    data = make_request(url)
    for result in data['results']:
        place = search_google_place(result['place_id'])
        place_list.append(place)
except Exception, e:
  print e

We’ll need to make a request to an individual place using a new URL.

https://maps.googleapis.com/maps/api/place/details/json?placeid=id

Finally, we’ll once again parse the JSON result and retrieve the places.

try:
    data = make_request(url)
    place = data['result']
    return Business(
        place['name'],
        place['formatted_address'].split(',')[0],
        place['rating'],
        place['user_ratings_total'],
        'N/A')
except Exception, e:
  print e

Congratulations! You’ve successfully retrieved all your local businesses from Foursquare, Yelp and Google.

Now that you’ve obtained a list of Business objects, you’re free to manipulate the data as you please. I chose to leverage the ratings to create a better ranking system for bars in my area, but the possibilities are endless. To view the entire source code for this article and to see my example, click this link. Thanks for reading!

Discuss on TwitterEdit on GitHub
Spotify album cover

/tools/photos