Assignment 2: Mashing Up Things and Services
- Due Feb 12, 2015 by 11:59pm
- Points 5
- Submitting a file upload
Introduction:
In this assignment you will build on Assignment 1 by connecting two Web services using Python. You will use output from the BART API as input for the Yelp API in order to find the top-rated restaurants near a few different BART stations.
Rationale:
Many of your projects (and ultimately all of them, when all of you will be asked to “mix and mingle” projects) will incorporate mashups of various Web services. This assignment will teach you how to authenticate developer credentials and create a simple mashup of two Web services.
Used Languages:
You will be using Python, the BART API, the Yelp API, and the Requests, oauth2, and ElementTree libraries.
Deliverables:
You should create a zip file containing the Python code. You must make sure that the Python code runs with version 2.7 of the interpreter - remember that your submitted code must work on our machines as well, so use relative paths if you add external libraries to your project, and include these libraries with your zip file. Please use UTF-8 encoding for your documents and avoid special characters. Upload your zip file to bCourses by Thursday, February 12, 2015, at 8pm PST.
Instructions:
Section 1 - Working with oauth2 & the Yelp API (3 points)
For Section 1 you will be learning how to authenticate developer credentials, create structured API search queries, and parse through response data using the Yelp API.
1.1 Setting up Yelp API access
In order to work with the Yelp API, as is common with many APIs, you’ll need to get a developer key. You can use a throw-away email account if you don’t feel like using your actual email to register. Once registered, visit the Manage API access page to retrieve your credentials for the v2.0 API.
1.2 Authenticating using the oauth2 library
For this section you’ll be creating a function to call the Yelp API.
Create a new Python script that you’ll be using throughout the rest of Section 1.
Next, authenticate with Yelp’s Web service using the oauth2 library (which you’ll need to download/install) and your developer credentials. In addition to the oauth2 documentation, review Yelp’s authentication documentation to get a better idea of how to use your credentials to authenticate with Yelp’s API. Yelp also provides a good example of how to set up authentication in Python.
Once your authentication is set up, your function should use the Requests library to perform a GET request to the Yelp API call with the following query parameters:
term=”restaurants”
location=”Berkeley”
You should receive a JSON response, and your code should print this response.
HINT: Create a variable that is a structured query URL, then use this URL with the oauth2.Request function.
1.3 Getting & parsing data from the Yelp API using structured queries
Now that you’ve made a basic API call, you’re going to make a new call with additional query parameters in order to narrow the results. You’ll then extract certain information from the response data. Refer to the Yelp Search API documentation to learn how to manipulate query parameters.
First, create a new structured query URL, identical to the one from Section 1.2, except without the ‘location’ parameter. Next, add the appropriate query parameters to accomplish the following:
- Set the search center point to the latitude & longitude for Berkeley:
- Latitude = 37.8759471 Longitude = -122.280622
- Limit your search results to the top-10 restaurants
- Sort your search by restaurant rating
Now that you have more specific search results, you’re going to extract the following information for each restaurant from the response JSON:
- Name
- Top-level category (e.g., ‘Mexican’)
- Rating
- Number of ratings
- Distance
Your code should print the above information for each restaurant.
HINT: You can traverse JSON objects similar to how you would traverse a dictionary.
Section 2 – Working with the BART API & connecting Web services (2 points)
2.1 Getting & parsing data from the BART API
Now that you have a working script for accessing the Yelp API, you’re going to create a separate script that calls the BART API.
In a new Python file, create a function that calls the BART API using the Requests library to find all the stations. Parse through the response XML using the built-in xml.etree.ElementTree module, and then return the city name (Berkeley), station name, and lat/long information for each of the Berkeley BART stations.
Your code should print the station information for each Berkeley station before returning.
HINT: Consider the type of data structure return in this section – you will need to extract the station information in the next section.
2.2 Using BART API data as inputs for the Yelp API
In this section you’ll be using the outputs of Section 2.1 as query parameters for the Yelp API script you made in Section 1.
First, go back to the Yelp API script from Section 1 and import the BART API script from Section 2 just as if you were importing a normal Python library. You can now use the function from your BART API script inside of your Yelp API script.
Next you’ll create a new function that uses your structured query from Section 1.3 to find the best Yelp options around each of the Berkeley BART stations. This time, however, set the results limit to 20 instead of 10.
Your code should print the Yelp results for each of the Berkeley BART station locations.
HINT: Call the function in your BART API script to get the station information needed to update the Yelp API query.
2.3 (Optional) Display results using a web framework
Create a simple web framework using Flask that displays the output from Section 2.2 on a locally served web page. If you’ve never used Flask before, here is a great tutorial.
Find Rubric