Fetch all Repositories of your Github Account

Fetch all Repositories of your Github Account

Many of you must already be aware of what an API is but here is my blog to tell how powerful it is and how it can help you to fetch relevant information from any website. All you need is API of that website.

So fasten your seat belts! Here we are gonna go and grab all repositories or projects you have build so far and published on your Github account, using Python.

In this project, I will make request to API of Github application (Github API) and fetch repositories from my github account.

FYI this is not only for github, you can take any remote application you wish to extract information from like gitlab, twitter, etc. You can also later extend this project by creating some repositories with Python code.

Now, let's get started!!

Prerequisites

• Basic knowledge of Python

• What is an API?

Code

Import the required package

import requests

Requests library is a simple HTTP library which allows to send HTTP request to a specified URL. When one makes a request to a URI, it returns a response. Python requests library provides inbuilt functionalities for managing both the request and response.

Now, search for the github API on google or directly go to this link: Github API. To fetch information of a particular user we require '/users' with the above link and use following query:

response = requests.get("https://api.github.com/users/HarshitaGupta16/repos")

The requests.get() method is used to get or retrieve data from a specified resource. This source will help to fetch all my accounts details. As a response for the requests made, response object will be returned.

Since response.text is of type string, you can check this by performing the following query:

print(type(response.text))

Output

<class 'str'>

So, json() method will be used which will convert the json response to one of the python data type. This is executed as:

my_repositories = response.json()
Since response was a list so json() converted the response to list by executing the above line of code. Lastly, traverse my_repositories to fetch repository name and url. To fetch this information execute the following code:
for repository in my_repositories:
    print(f"Repository Name: {repository['name']}\nProject Url: {repository['web_url']}\n") 

This will fetch and print all the repositories of a particular gihub account with their URL.

Output

image.png

This completes my small project to fetch information of any github account using Python and github API.

Simple enough, right?

Now try exploring more APIs of different applications and play around with it's different operations.

Never Stop Learning!! Happy Coding :D

Thank you for reading. Hope it helps :)

Open for your feedback and suggestions.

Connect me on: LinkedIn Twitter