Skip to content

Instantly share code, notes, and snippets.

@selimslab
Forked from leoloobeek/get_gists.py
Last active December 23, 2023 15:52
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save selimslab/958e2255a105f9a3f4b421896bce715d to your computer and use it in GitHub Desktop.
Save selimslab/958e2255a105f9a3f4b421896bce715d to your computer and use it in GitHub Desktop.
Download all gists of a user
import sys
from subprocess import call
import json
import os
import requests
def download_gists(gists: list):
for gist in gists:
call(["git", "clone", gist["git_pull_url"]])
#  name of the first file in the gist
new_folder_name = sorted(list(gist.get("files", {}).keys()))[0].split(".")[0]
os.rename(gist["id"], new_folder_name)
description_file = os.path.join(new_folder_name, "description.txt")
with open(description_file, "w") as f:
f.write(f"{gist['description']}\n")
def visit_pages(user: str):
next_page = True
page = 1
while next_page:
url = f"https://api.github.com/users/{user}/gists?page={page}"
r = requests.get(url)
if not len(r.json()):
next_page = False
else:
page += 1
download_gists(r.json())
user = sys.argv[1]
visit_pages(user)
@tjluoma
Copy link

tjluoma commented Dec 30, 2020

Sorry to be dense but I don't really know python. I tried python3 get_gists.py but got this message:

Traceback (most recent call last):
  File "/Users/tjluoma/Downloads/get_gists.py", line 5, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

Can you tell me what I'm doing wrong?

@selimslab
Copy link
Author

you need to install requests module. please run pip install requests

@antonydevanchi
Copy link

Just added some codestyle, shebang, one useless comment and parallelism.

🙃

#!/usr/bin/env python3

import os
import sys
import json
import hashlib
import requests

from subprocess import call
from concurrent.futures import ThreadPoolExecutor as PoolExecutor

def download_all_from_user(user: str):
    
    next_page = True
    page = 1
    
    while next_page:
        
        url = f"https://api.github.com/users/{user}/gists?page={page}"
        
        response = requests.get(url)

        if not len(response.json()):
            next_page = False
        else:
            page += 1

        download_all(response.json())

def download_all(gists: list):
    with PoolExecutor(max_workers=10) as executor:
        for _ in executor.map(download, gists):
            pass

def download(gist):
    
    target = gist["id"] + hashlib.md5(gist["updated_at"].encode('utf-8')).hexdigest()
    
    call(["git", "clone", gist["git_pull_url"], target])

    description_file = os.path.join(target, "description.txt")
    
    with open(description_file, "w") as f:
        f.write(f"{gist['description']}\n")

# Run

user = sys.argv[1]

download_all_from_user(user)

@selimslab
Copy link
Author

@antonydevanchi thank you :)

@varenc
Copy link

varenc commented Jun 14, 2023

I forked this as well! https://gist.github.com/varenc/c65e6af52e4dc6f2d5f1aac9ea9a349e

Fixed a bug where a user might have some gists with the same name, resulting in errors. Also added handling for the when the github API rate limits you (which happens often for unauthenticated requests!)

@vionwinnie
Copy link

Just added some codestyle, shebang, one useless comment and parallelism.

🙃

#!/usr/bin/env python3

import os
import sys
import json
import hashlib
import requests

from subprocess import call
from concurrent.futures import ThreadPoolExecutor as PoolExecutor

def download_all_from_user(user: str):
    
    next_page = True
    page = 1
    
    while next_page:
        
        url = f"https://api.github.com/users/{user}/gists?page={page}"
        
        response = requests.get(url)

        if not len(response.json()):
            next_page = False
        else:
            page += 1

        download_all(response.json())

def download_all(gists: list):
    with PoolExecutor(max_workers=10) as executor:
        for _ in executor.map(download, gists):
            pass

def download(gist):
    
    target = gist["id"] + hashlib.md5(gist["updated_at"].encode('utf-8')).hexdigest()
    
    call(["git", "clone", gist["git_pull_url"], target])

    description_file = os.path.join(target, "description.txt")
    
    with open(description_file, "w") as f:
        f.write(f"{gist['description']}\n")

# Run

user = sys.argv[1]

download_all_from_user(user)

This is amazing :) thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment