API CAN CODE �Computational Foundations of �Data Science
Lesson 2.4: Accessing Data with APIs
This work was made possible through generous support from the National Science Foundation (Award # 2141655).
2.4 Recap
2
Warmup
3
What is API?
Application�
API �
Programming�
Interface�
4
Why Do We Need APIs?
5
Company/Service
End-user/Developers
Examples of APIs
6
Retrieve all tweets from users / specific hashtag
Retrieve information about tracks and playlists
Intro to RapidAPI
RapidAPI is the world's largest API marketplace, used to discover and connect to thousands of APIs.
7
Register for RapidAPI
Go to rapidapi.com and sign up
8
If your school email address does not work, try a personal email address instead.
Connecting RapidAPI to EduBlocks
What are the IMDB Top 100 Movies?
9
Connecting RapidAPI to EduBlocks
What are the IMDB Top 100 Movies?
10
There’s a quick guide in your handouts for future use!
Connecting RapidAPI to EduBlocks
response is the variable that holds the response from the data source / API
get() method sends a request to the specified url
11
get() method
response
url
Connecting RapidAPI to EduBlocks
response.text retrieves the text content of the response object
json.loads() takes a JSON string as input and returns a corresponding Python object
12
json.loads
response.text
Connecting RapidAPI to EduBlocks
13
Double check these boxes!
Each person has their unique API key
Top 100 Movies with EduBlocks
What are the 100 best movies ever made?
A basic EduBlocks project to request information from the IMDB API will print information on the Top 100 Movies:
Clone this project into your own library and rename it!
14
What format does this present the data in?
Offline API Backup!
15
Top 100 Movies with EduBlocks
What are the 100 best movies ever made?
Look below the final line in the project, and replace the final line with this chunk of code. What’s the difference when you run the project?
16
Returns a list of the top 100 movie titles
Printing Other Characteristics
What are the genres of each movie in the top 100?
Add a print statement so, in addition to seeing the title of each movie, we also see the genre. �
17
Sorting by year with EduBlocks
Of the top 100 movies, what are from after 2000?
Using a green “if” block, the project shown to the right filters down to only movies made after the year 2000.�
Note that this project still prints the title and genre(s) for each movie, as we saw in the previous version!�
Modify your project to include this filter.
18
Sorting by multiple conditions
Of the top 100 movies, what belong to both comedy and romance genre?
Add both conditions in the green “if” block to filter movies that are both comedy and romance.�
Modify your project to include this filter.
19
Data Transformation with EduBlocks
The rating system is currently out of 10, but we want it to be out of 100 instead!
How can we use EduBlocks math functions to transform the rating value into the new system?
Modify your project using the “math” blocks to transform the ratings.
20
Exploring Movies with EduBlocks
Exit Ticket: Come up with a question and answer.
“What is the highest rated movie of the 2000s?”
“What is the highest rated horror movie?”
“How many of the top 100 movies are from the 1970s?
Modify your project to answer your question. Save your project (and leave it open) so you can submit the link in the exit ticket!
21
Thanks!
apicancode@umd.edu
22
This work was made possible through generous support from the National Science Foundation (Award # 2141655).
API Can Code is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike
4.0 International (CC BY-NC-SA 4.0) License