-
-
Save varenc/c65e6af52e4dc6f2d5f1aac9ea9a349e to your computer and use it in GitHub Desktop.
Download all gists of a user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from subprocess import call | |
import json | |
import os | |
import requests | |
import shutil | |
import colorama | |
def download_gists(gists: list, user: str): | |
GISTS_DIR = f"{user}_gists" | |
print(f"{colorama.Fore.GREEN}Creating '{GISTS_DIR}'...full path: {os.path.abspath(GISTS_DIR)}{colorama.Style.RESET_ALL}") | |
if not os.path.exists(GISTS_DIR): | |
os.mkdir(GISTS_DIR) | |
else: | |
print(f"{colorama.Fore.RED}Warning: '{GISTS_DIR}' at '{os.path.abspath(GISTS_DIR)}' already exists!{colorama.Style.RESET_ALL}") | |
user_response = input("Would you like to delete the folder and all its contents? If we don't we may create duplicates (y/n): ") | |
if user_response.lower() == "y": | |
print(f"{colorama.Fore.RED}Deleting '{GISTS_DIR}'...{colorama.Style.RESET_ALL}") | |
shutil.rmtree(GISTS_DIR) | |
os.mkdir(GISTS_DIR) | |
for gist in gists: | |
gist_name = sorted(list(gist.get("files", {}).keys()))[0].split(".")[0] | |
new_folder_name = os.path.join(GISTS_DIR, gist_name) | |
call(["git", "clone", gist["git_pull_url"], "-q"]) | |
if os.path.exists(new_folder_name): | |
print(f"{colorama.Fore.RED}Warning: {new_folder_name} already exists. Appending a number to the folder name...{colorama.Style.RESET_ALL}") | |
i = 1 | |
while os.path.exists(f"{os.path.join(GISTS_DIR, gist_name)} ({i})"): | |
i += 1 | |
gist_name = f"{gist_name} ({i})" | |
new_folder_name = os.path.join(GISTS_DIR, gist_name) | |
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") | |
print(f"{colorama.Fore.GREEN}Downloaded gist: '{gist_name}'{colorama.Style.RESET_ALL}") | |
def visit_pages(user: str): | |
all_gists = [] | |
next_page = True | |
page = 1 | |
while next_page: | |
print(f"{colorama.Fore.GREEN}Downloading github gist page {page} for user '{user}'...{colorama.Style.RESET_ALL}") | |
url = f"https://api.github.com/users/{user}/gists?page={page}" | |
r = requests.get(url) | |
if r.status_code != 200: | |
print(f"{colorama.Fore.RED}Error: {r.status_code} when retrieving {url}{colorama.Style.RESET_ALL}") | |
sys.exit(1) | |
if not len(r.json()): | |
next_page = False | |
else: | |
all_gists.extend(r.json()) | |
page += 1 | |
download_gists(all_gists, user) | |
if len(sys.argv) < 2: | |
print("Please provide a GitHub username as an argument.") | |
sys.exit(1) | |
user = sys.argv[1] | |
visit_pages(user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment