Last active
April 3, 2021 23:45
-
-
Save louisswarren/690235db86090f9e6f544cd66b500a17 to your computer and use it in GitHub Desktop.
Clone all gists
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 itertools | |
import json | |
import subprocess | |
import sys | |
import urllib.request | |
user = 'louisswarren' | |
api_url = 'https://api.github.com/users/{user}/gists?page={page}' | |
https_prefix = 'https://gist.github.com/' | |
git_prefix = 'git@gist.github.com:' | |
def json_query(url): | |
with urllib.request.urlopen(url) as response: | |
return json.loads(response.read().decode('utf-8')) | |
def run(*cmdlist, stdin=None, stderr=subprocess.STDOUT, check_exit=False): | |
return subprocess.run( | |
cmdlist, | |
stdout=subprocess.PIPE, | |
stdin=stdin, | |
stderr=stderr, | |
check=check_exit, | |
).stdout.decode() | |
def clone(repodict): | |
url = repo['git_pull_url'].replace(https_prefix, git_prefix) | |
description = (repo['description'] + ' ').lstrip() | |
shortid = repo['id'][:7] | |
name = f'{description}({shortid})' | |
print("Cloning", name) | |
run('git', 'clone', url, name) | |
if __name__ == '__main__': | |
for page in itertools.count(): | |
repos = json_query(api_url.format(user=user, page=page)) | |
for repo in repos: | |
clone(repo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment