Sunday, October 6, 2019

Working with the Response Dictionary

Using the information from the API call stored as a dictionary, we can work with the data stored there. The following program generate some output that summarizes the information:

import requests

# Make an API call and store the response.
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Store API response in a variable.
response_dict = r.json()
print(f"Total repositories: {response_dict['total_count']}")

# Explore information about the repositories.
repo_dicts = response_dict['items']
print(f"Repositories returned: {len(repo_dicts)}")

# Examine the first repository.
repo_dict = repo_dicts[0]
print(f"\nKeys: {len(repo_dict)}")
for key in sorted(repo_dict.keys()):
    print(key)


Here we print the value associated with 'total_count', which represents the total number of Python repositories on GitHub. The value associated with 'items' is a list containing a number of dictionaries,
each of which contains data about an individual Python repository. We store this list of dictionaries in repo_dicts. We then print the length of repo_dicts to see how many repositories we have information for. Next we pull out the first item from repo_dicts and store it in repo_dict w. We then print the number of keys in the dictionary to see how much information we have. Finally we print all the dictionary’s keys to see what kind of information is included. The output is shown below:

Status code: 200
Total repositories: 4294757
Repositories returned: 30

Keys: 74
archive_url
archived
assignees_url
:

:
:
url
watchers
watchers_count
------------------
(program exited with code: 0)

Press any key to continue . . .


GitHub’s API returns a lot of information about each repository: there are 74 keys in repo_dict. When we  look through these keys, we’ll get a sense of the kind of information we can extract about a project. (The only way to know what information is available through an API is to read the documentation or to examine the information through code, as we’re doing here.)

In the following program we pull out the values for some of the keys in repo_dict:

import requests

# Make an API call and store the response.
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Store API response in a variable.
response_dict = r.json()
print(f"Total repositories: {response_dict['total_count']}")

# Explore information about the repositories.
repo_dicts = response_dict['items']
print(f"Repositories returned: {len(repo_dicts)}")

# Examine the first repository.
repo_dict = repo_dicts[0]
print("\nSelected information about first repository:")
print(f"Name: {repo_dict['name']}")
print(f"Owner: {repo_dict['owner']['login']}")
print(f"Stars: {repo_dict['stargazers_count']}")
print(f"Repository: {repo_dict['html_url']}")
print(f"Created: {repo_dict['created_at']}")
print(f"Updated: {repo_dict['updated_at']}")
print(f"Description: {repo_dict['description']}")


In the above program,we print the values for a number of keys from the first repository’s dictionary. First we print the name of the project. An entire dictionary represents the project’s owner, we use the key owner to access the dictionary representing the owner, and then use the key login to get the
owner’s login name. We also print how many stars the project has earned and the URL for the project’s GitHub repository. We then show when it was created and when it was last updated. Finally, we print the repository’s description; the output should look as shown below:

Status code: 200
Total repositories: 4294805
Repositories returned: 30

Selected information about first repository:
Name: system-design-primer
Owner: donnemartin
Stars: 74208
Repository: https://github.com/donnemartin/system-design-primer
Created: 2017-02-26T16:15:28Z
Updated: 2019-10-04T05:58:26Z
Description: Learn how to design large-scale systems. Prep for the system design
 interview.  Includes Anki flashcards.
------------------
(program exited with code: 0)

Press any key to continue . . .


We can see from the output that the most-starred Python project on GitHub as of this writing is system-design-primer, its owner is user donnemartin, and it has been starred by more than 74,000 GitHub users. We can see the URL for the project’s repository, its creation date of June 2014, and that it was updated recently. Additionally, the description tells us that awesome-python contains a list of
popular Python resources.

Next we'll write a loop to print selected information about each repository the API call returns so we can include them all in the visualization:

import requests

# Make an API call and store the response.
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Store API response in a variable.
response_dict = r.json()
print(f"Total repositories: {response_dict['total_count']}")

# Explore information about the repositories.
repo_dicts = response_dict['items']
print(f"Repositories returned: {len(repo_dicts)}")

print("\nSelected information about each repository:")
for repo_dict in repo_dicts:
    print(f"Name: {repo_dict['name']}")
    print(f"Owner: {repo_dict['owner']['login']}")
    print(f"Stars: {repo_dict['stargazers_count']}")
    print(f"Repository: {repo_dict['html_url']}")
    print(f"Created: {repo_dict['created_at']}")
    print(f"Updated: {repo_dict['updated_at']}")
    print(f"Description: {repo_dict['description']}")


First we print an introductory message and then we loop through all the dictionaries in repo_dicts. Inside the loop, we print the name of each project, its owner, how many stars it has, its URL on GitHub, and the project’s description, as shown in the output below:

Status code: 200
Total repositories: 4281727
Repositories returned: 30

Selected information about each repository:
Name: awesome-python
Owner: vinta
Stars: 73698
Repository: https://github.com/vinta/awesome-python
Created: 2014-06-27T21:00:06Z
Updated: 2019-10-04T06:11:14Z
Description: A curated list of awesome Python frameworks, libraries, software an
d resources

:
:
:
Name: pandas
Owner: pandas-dev
Stars: 21625
Repository: https://github.com/pandas-dev/pandas
Created: 2010-08-24T01:37:33Z
Updated: 2019-10-04T05:30:39Z
Description: Flexible and powerful data analysis / manipulation library for Pyth
on, providing labeled data structures similar to R data.frame objects, statistic
al functions, and much more
------------------
(program exited with code: 0)

Press any key to continue . . .


Here I am ending this post, in the next post we'll discuss about Visualizing Repositories Using Plotly.


Share:

0 comments:

Post a Comment