Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Problems
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • Data Science
  • Machine Learning
  • Courses
  • Linux
  • DevOps
  • SQL
  • Web Development
  • System Design
  • Aptitude
  • GfG Premium
Open In App
Next Article:
Downloading gists from Github made simple
Next article icon

Downloading gists from Github made simple

Last Updated : 29 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
GithubGist is a place where you can create a private or a public gist i.e store your files either privately or publicly. Let us assume a scenario where you have written a good number of gists for your project and you want to download a set of them. The only way you can do it using GithubGist is to open each and every individual gist to download ZIP or clone via HTTP or SSH. This article is all about making the above task simpler. Using the commands below, you can even download gists from other github users excluding the private ones until you know their password. We will be using requests package for this propose. It is an awesome package to send HTTP requests with minimal code. Installation 1. Download the package from PyPI via terminal using pip3 Syntax:
pip3 install requests
Note: To become a root user, run the following command:
sudo pip3 install requests
Python3 script The script can not run on an online IDE and therefore you can click here to see how it works. Python
import requests import os  def create_directory(dirname):     #Creates a new directory if a directory with dirname does not exist      try:         os.stat(dirname)     except:         os.mkdir(dirname)  def show(obj):     #Displays the items in the obj      for i in range(len(obj)):         print(str(i)+': '+str(obj[i]))  def auth():     #Asks for the user details      ask_auth = input("Do you want to download gists from your account                                ? Type 'yes' or 'no': ")     if(ask_auth=="yes"):         user = input("Enter your username: ")         password = input("Enter your password: ")         request = requests.get('https://api.github.com/users/'+user+'/gists'                                              , auth=(user, password))     elif(ask_auth=="no"):         user = input("Enter username: ")         request = requests.get('https://api.github.com/users/'                                                        +user+'/gists')     return [ask_auth, user, request]  def load(request):    #Loads the files and the gist urls     output = request.text.split(",")    gist_urls = []    files = []    for item in output:        if "raw_url" in item:            gist_urls.append(str(item[11:-1]))        if "filename" in item:            files.append(str(item.split(":")[1][2:-1]))    return [gist_urls, files]  def write_gist(filename, text):     #Writes text(gist) to filename      fp = open(filename, 'w')     fp.write(text)     fp.close()  def download(permission, user, request, fileno):     #Loads and writes all the gists to <em>dirname</em>      if(permission == "yes" or permission == "no"):         gist_urls, files = load(request)         dirname = user+"'s_gists/"         create_directory(dirname)         if(fileno[1] == "all"):             for i in range(len(gist_urls)):                 gist = requests.get(gist_urls[i])                 write_gist(dirname+files[i], gist.text)         else:             for i in range(1,len(fileno)):                 gist = requests.get(gist_urls[int(fileno[i])])                 write_gist(dirname+files[int(fileno[i])], gist.text)  def detailed(urls, pos):     #Prints out the contents of a file      gist = requests.get(urls[int(pos)])     print(gist.text)  def main():     #Authenticates and downloads gists according to user's choice     #Commands:     #show: To show all the available gists with their assigned gistno     #download all: To download all the available gists     #download gistno(s): To download gist(s) assigned to gistno(s)     #detailed gistno: To print content of gist assigned to gistno     #exit: To exit the script      ask_auth, user, request = auth()     urls, files = load(request)     try:         while(1):             command = input("Enter your command: ")             if("download" in command):                 download(ask_auth, user, request, command.split(" "))             elif("detailed" in command):                 detailed(urls, command.split(" ")[1])             elif(command == "show"):                 show(files)             elif(command == "exit"):                 return     except:         pass  if(__name__ == '__main__'):     main() 
Explanation GithubGist API stores information about each user at http://api.github.com/users/username/gists.
  • Send a HTTP request to the above url to retrieve information about the user.
  • Search for the raw_url of every available gist and sent HTTP requests to retrieve the information about them.
  • Manipulated the information to your needs.

Next Article
Downloading gists from Github made simple

S

Sri Sanketh Uppalapati
Improve
Article Tags :
  • python
Practice Tags :
  • python

Similar Reads

    How To Download Single Folder or Directory From GitHub Repository?
    GitHub is a popular platform for hosting and collaborating on code repositories. Often, you might come across a situation where you need to download just a single folder or directory from a GitHub repository rather than cloning the entire repository. This can save time and bandwidth, especially when
    3 min read
    What Is GitHub Gist?
    GitHub offers a feature called GitHub Gist that makes it simple for users to share text-based content, notes, and code snippets. It provides a simple, lightweight method for managing and sharing small content pieces, like scripts, configuration files, and even documentation.In this article, we will
    4 min read
    Github Desktop Download
    GitHub Desktop is a user-friendly application that simplifies the process of using Git and GitHub on your computer. It provides a graphical interface to manage your repositories, track changes, and collaborate with others without needing to use the command line.In this article, we will walk you thro
    5 min read
    How to Install, Configure and Use GIT on Ubuntu?
    Git is a powerful version control system widely used for tracking changes in source code during software development. This article will walk you through the steps to install, configure, and use Git on an Ubuntu system. Table of Content Installing Git with Default PackagesInstalling/Downloading Git f
    5 min read
    How to Push Anything to GitHub using Git Bash?
    GitHub has become the go-to platform for collaborative software development, offering powerful tools for version control, collaboration, and project management. Git Bash, a command-line interface for Git on Windows, provides a quick way to interact with GitHub repositories. In this guide, we'll lear
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences