Introduction
In this guide, we’ll walk you through a straightforward Python script that taps into the GitHub API to fetch the list of popular repositories. We’ll be using the requests
module, a handy tool for making HTTP requests in Python.
Why Need Popular GitHub Repositories?
Popular GitHub repositories help developers stay updated on community trends. They are a source for discovering influential projects and finding valuable learning resources. Additionally, they offer insights into contributor interest, fostering community engagement and showcasing emerging technologies and best practices.
Prerequisites
Before we start, make sure you have the requests
library installed. You can use Pip to install it with the following command:
pip install requests
How to Find the Top GitHub Repositories
The script finds the top repositories by sorting them based on the number of stars in descending order. It then displays details for the top 5, providing a quick overview of popular projects on GitHub.
Understanding the Script
Let’s break down the provided Python script to understand how it works:
import requests as req
def get_top_repos():
base_url = "https://api.github.com/search/repositories"
# Params for the API request
params = {
'q': 'stars:>1000', # Search for repositories with more than 1000 stars
'sort': 'stars',
'order': 'desc',
}
# Making the API request to search for top repos
resp = req.get(base_url, params=params)
if resp.status_code == 200:
# API call successful
results = resp.json()['items']
print("Top Repos:")
for repo in results[:5]: # Display details of the top 5 repos
print(f"\nRepo Name: {repo['name']}")
print(f"Owner: {repo['owner']['login']}")
print(f"Stars: {repo['stargazers_count']}")
print(f"Desc: {repo.get('description', 'No desc')}")
print(f"URL: {repo['html_url']}")
else:
print(f"Failed to get top repos. Status code: {resp.status_code}")
# Fetch and display info about top repos
get_top_repos()
Output: The List of 5 Most Popular GitHub Repos
When you run the above code, it will get us the top 5 GitHub Repos as shown below:
Top Repos:
Repo Name: freeCodeCamp
Owner: freeCodeCamp
Stars: 382756
Desc: freeCodeCamp.org's open-source codebase and curriculum. Learn to code for free.
URL: https://github.com/freeCodeCamp/freeCodeCamp
Repo Name: free-programming-books
Owner: EbookFoundation
Stars: 310530
Desc: :books: Freely available programming books
URL: https://github.com/EbookFoundation/free-programming-books
Repo Name: awesome
Owner: sindresorhus
Stars: 288194
Desc: ???? Awesome lists about all kinds of interesting topics
URL: https://github.com/sindresorhus/awesome
Repo Name: public-apis
Owner: public-apis
Stars: 278125
Desc: A collective list of free APIs
URL: https://github.com/public-apis/public-apis
Repo Name: coding-interview-university
Owner: jwasham
Stars: 277032
Desc: A complete computer science study plan to become a software engineer.
URL: https://github.com/jwasham/coding-interview-university
Script Walkthrough
Importing the requests
Module:
- The script begins by importing the
requests
module. This module simplifies the process of making web requests in Python.
Setting up the Base URL and Parameters:
base_url
is the address of the GitHub API endpoint for searching repositories.- The
params
dictionary contains key-value pairs representing the parameters for the API request. In this case, we’re looking for repositories with more than 1000 stars, sorted in descending order.
Making the API Request:
- The
req.get()
function is used to send a GET request to the GitHub API. It includes the base URL and parameters. The response is stored in theresp
variable.
Handling the API Response:
- The script checks if the API request was successful by inspecting the HTTP status code (200 indicates success).
- If successful, it extracts the relevant information from the JSON response, focusing on the list of repositories (
results
).
Displaying Repository Information:
- The script then goes on to loop through the top 5 repositories and prints details such as name, owner, stars, description, and URL.
Handling Errors:
- In case the API request fails, the script shows an error message along with the HTTP status code.
Invoking the Function:
- Finally, the
get_top_repos()
function is called to execute the script.
Running the Script
To run the script, use a Python interpreter:
python script_name.py
Replace script_name.py
with the actual name of your Python script.
Summary – Python Code to List Popular GitHub Repositories
In this How To guide, we’ve explored a simple Python script that leverages the requests
module to interact with the GitHub API. We covered the key steps involved in making API requests, handling responses, and presenting relevant data. Feel free to enhance this script according to your needs, perhaps by adding error-handling mechanisms or exploring additional features offered by the GitHub API.
Happy Coding!