Skip to content
Related Articles

Related Articles

Downloading gists from Github made simple

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 04 Sep, 2017

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.


1. Download the package from PyPI via terminal using pip3

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.

import requests
import os
def create_directory(dirname):
    #Creates a new directory if a directory with dirname does not exist
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': ")
        user = input("Enter your username: ")
        password = input("Enter your password: ")
        request = requests.get(''+user+'/gists'
                                             , auth=(user, password))
        user = input("Enter username: ")
        request = requests.get(''
    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:
       if "filename" in item:
   return [gist_urls, files]
def write_gist(filename, text):
    #Writes text(gist) to filename
    fp = open(filename, 'w')
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/"
        if(fileno[1] == "all"):
            for i in range(len(gist_urls)):
                gist = requests.get(gist_urls[i])
                write_gist(dirname+files[i], gist.text)
            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)])
def main():
    #Authenticates and downloads gists according to user's choice
    #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)
            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"):
            elif(command == "exit"):
if(__name__ == '__main__'):

GithubGist API stores information about each user at

  • 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.

This article is contributed by Sri Sanketh Uppalapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!