GitHub API With Python

GitHub API With Python

In this post, we will use the GitHub API to fetch user data from Github. We will be able to fetch the user's website address with number of repositories, followers, and followings. If you want to know more about GitHub, you can read it here

Let's begin.

We need requests module for this project with JSON. So first we import requests and JSON. If you don't have requests module installed, you would need to install it with pip.

pip install requests

First, let's understand the GitHub API. This is the API we would use here.

https://api.github.com/users/abc

In the above API link, abc is a username.

Now import requests and JSON.

import requests
import json

we will create a simple function to perform the operations and create a few empty lists and a dictionary.

def githubProfile():
    parsedData = []
    jsonList = []
    userData = {}

Now using the requests module we need to call the API and save it in a new variable.

req = requests.get('https://api.github.com/users/' + username)

Here we are adding a username in the API. This username will be provided by us. if we print, req.content, we would see the JSON data in our terminal.

We have to load this JSON string data using the load method and save it in a new list using the append function.

jsonList.append(json.loads(req.content))

This whole data is in jsonList in the JSON format and now we need to get this in our empty dictionary. Run a for loop on jsonList and add the data in the empty dictionary userData by given each value a key name.

for data in jsonList:
        userData['name'] = data['name']
        userData['blog'] = data['blog']
        userData['email'] = data['email']
        userData['public_gists'] = data['public_gists']
        userData['public_repos'] = data['public_repos']
        userData['followers'] = data['followers']
        userData['following'] = data['following']

Now we have all the data in userData. We will append it to a list and return the value. Although we can always return the userData directly.

parsedData.append(userData)
return parsedData

Let's see the complete code.

import requests
import json

def githubProfile():
    parsedData = []
    req = requests.get('https://api.github.com/users/' + username)
    jsonList = []
    jsonList.append(json.loads(req.content))
    userData = {}
    for data in jsonList:
        userData['name'] = data['name']
        userData['blog'] = data['blog']
        userData['email'] = data['email']
        userData['public_gists'] = data['public_gists']
        userData['public_repos'] = data['public_repos']
        userData['followers'] = data['followers']
        userData['following'] = data['following']
    parsedData.append(userData)
    return parsedData


username = input("enter your github username: ")
mydata = githubProfile()

print(mydata)

In the end, we are asking for input and saving it in username. This username will be added to the API link when we call the function.

Hope you find it helpful.

My Python GitHub repository.