TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Fetch the List of Popular GitHub Repos
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
HowToPython BasicPython Tutorials

How to Fetch the List of Popular GitHub Repos

Last updated: Feb 03, 2024 2:54 pm
By Soumya Agarwal
Share
5 Min Read
Python Script to Fetch the List of Popular GitHub Repositories
SHARE

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.

Contents
Why Need Popular GitHub Repositories?PrerequisitesHow to Find the Top GitHub RepositoriesUnderstanding the ScriptOutput: The List of 5 Most Popular GitHub ReposScript WalkthroughRunning the Script

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 the resp 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!

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

Selenium Python Extent Report Guide

10 Python Tricky Coding Exercises

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
Loading
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Soumya Agarwal Avatar
By Soumya Agarwal
Follow:
I'm a BTech graduate from IIITM Gwalior. I have been actively working with large MNCs like ZS and Amazon. My development skills include Android and Python programming, while I keep learning new technologies like data science, AI, and LLMs. I have authored many articles and published them online. I frequently write on Python programming, Android, and popular tech topics. I wish my tutorials are new and useful for you.
Previous Article Reducing List, String, Tuple with Reduce() in Python Python Reduce() for Reducing List, String, Tuple With Examples
Next Article Python Pandas Tutorial for Series and DataFrames Python Pandas Tutorial to Learn Series and DataFrames

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x