1 of 22

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 of 22

2.4 Recap

  • EduBlocks can help us create and use variables with variable and print statements�
  • We can filter data with green conditional statements (like if statements)�
  • Loops and iteration can allow us to programmatically go through all the observations in a list or dataset

2

3 of 22

Warmup

  • Students review this example of EduBlocks code to see how lists, loops, and conditional statements are displayed in block form. �
  • How does this code work? �What does it do?

3

4 of 22

What is API?

Application�

API

Programming�

Interface�

  • A way for software applications and services to exchange information and communicate with each other.

  • They provide accessible way to extract and share data. �
  • We’ll be using RapidAPI, a database of API sources

4

5 of 22

Why Do We Need APIs?

5

Company/Service

  • Increasing the distribution of the service and exposing it to a large number of users.
  • Preventing inappropriate access and modification of data.
  • Expanding system capabilities.
  • An organized and controlled way to access vast amount of data.
  • Short development time.
  • Infrastructure for developing new applications.
    • For example: an app with information about NBA players might be built off an API providing that data!

End-user/Developers

6 of 22

Examples of APIs

6

Retrieve all tweets from users / specific hashtag

Retrieve information about tracks and playlists

7 of 22

Intro to RapidAPI

RapidAPI is the world's largest API marketplace, used to discover and connect to thousands of APIs.

7

8 of 22

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.

9 of 22

Connecting RapidAPI to EduBlocks

What are the IMDB Top 100 Movies?

9

10 of 22

Connecting RapidAPI to EduBlocks

What are the IMDB Top 100 Movies?

10

There’s a quick guide in your handouts for future use!

11 of 22

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

12 of 22

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

13 of 22

Connecting RapidAPI to EduBlocks

13

  • Make sure the “Top 100 Movies List” endpoint is selected!
  • Under “Code Snippets,” switch to “(Python) Requests” to get code for EduBlocks

Double check these boxes!

Each person has their unique API key

14 of 22

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?

15 of 22

Offline API Backup!

  • If the IMDB API is down, you can use a pre-downloaded version of the same data. �
  • Use the backup EduBlocks file we created for this lesson, found here.

15

16 of 22

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

17 of 22

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. �

  • How is genre stored in this dataset?
  • Does each movie have one genre? How are multiple genres stored and displayed?
  • What else could you print out about different movies?

17

18 of 22

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

19 of 22

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

20 of 22

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

21 of 22

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

22 of 22

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