Coding: GitHub Public API Application using Python (Part-3)

Welcome back! In this part, we are going to code the application using Python. If you have not read the other two parts, I would highly recommend you to go through both of them as they are prerequisite to this part.
Part-1 laid the foundation of application, and in Part-2, I have described the structure and fields of GitHub API.

Tools to need

I am going to use only one tool in this article, which is: “Python 3.6 IDLE.” If you do not know anything about it, and you have not found anywhere in your computer this software, then follow the Part-1. Alright! I would assume that you would have this IDLE in your start panel. The tool will look like this:

Application

Step 1:

Go to the file and choose new file. Save it as“GitHubApi.py”(as shown below).

Left window is for the outputs, whereas the right window will be used to write the code for GitHub API and logic.

Step 2:

Import the “requests” package, which you have installed in the Part-1. The code line would be:

import requests

Step 3:

Let us take the username/channel name/GitHub page name from the user at run time. The code line would be:

entry = input(“Enter the GitHub Channel name: ”)

Step 4:

As I have explained in the Part-2 that the following GitHub’s API link is used to access the public information of the users:

https://api.github.com/users/{channel-name}

Now the code line would be:

raw_data = requests.get(“https://api.github.com/users/”+entry)

The entry should be the valid channel name. In this part I am not going to do the exception handling.

Now the raw_data holds the whole details of the entry, but I am interested in the JSON of the GitHub Public API of a user.

Step 5:

In order to get the JSON out of the raw data above, you need to write the following line of code:

user_json = raw_data.json()

If you print the user_json, you would get the following result:

Here is the copy of above code:

import requests

entry = input("Enter the GitHub Channel name: ")

raw_data = requests.get("https://api.github.com/users/"+entry)

user_json = raw_data.json()

print(user_json)

Step 6:

What if you want to know just the bio field of the user? Well, it is really simple. Take the “user_json” and write the following line of code:

bio = user_json[“bio”]

And if you print the code, you would see the following output:

Likewise, if you want to get other fields, we can use the above method to get all the individual data.

The final code and output would look like this:

Final Code:

# Here we are going to write code

import requests
def options():
    option = input()
    if option=="1":
        print("Bio:" + user_json["bio"])
        print("\n\n")
    elif option=="2":
        print("ID:{0}".format(user_json["id"]))
        print("\n\n")
    elif option=="3":
        print("Followers:{0}".format(user_json["followers"]))
        print("\n\n")
    elif option=="4":
        print("Following:{0}".format(user_json["following"]))
        print("\n\n")
    elif option=="5":
        print("Public Repositories{0}".format(user_json["public_repos"]))
        print("\n\n")
    elif option=="q":
        quit()
    else:
        print("Wrong Option. Enter Again!\n")
        options()

 

while(1):

    entry = input("Enter the GitHub Channel name: ")

    raw_data = requests.get("https://api.github.com/users/"+entry)

    user_json = raw_data.json()

    print("Enter 1 for Bio\nEnter 2 for ID\nEnter 3 for Followers\nEnter 4 for Followings\nEnter 5 for Public Repositories\nEnter q to Quit\n")
    options()

Summary

In this part, we have used Python to use GitHub API in order to get the public data of any User. We have also learned how to use any API in general using Python. That’s it for this tutorial!

Related article

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?