-
-
Save glowinthedark/d21dc765e6afe3106713e7307906df72 to your computer and use it in GitHub Desktop.
Download all gists for a specific 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 | |
# | |
# download all gists for a user, pass github user as first arg | |
# if the gists have already been downloaded before then update them | |
import json | |
import os | |
import re | |
import requests | |
import sys | |
from subprocess import call | |
def slugify(value): | |
value = re.sub(r'[^\w\s-]', '_', value).strip().lower() | |
value = re.sub(r'[-\s]+', '_', value) | |
return value | |
if __name__ == '__main__': | |
user = sys.argv[1] | |
r = requests.get('https://api.github.com/users/{0}/gists'.format(user)) | |
for gist in r.json(): | |
fname = next(iter(gist['files'])) | |
gist_dir = fname and slugify(fname) | |
gist_dir = os.path.join('gists', f'{gist_dir}-{gist["id"]}') | |
if not os.path.exists(gist_dir): | |
# if the repo does not exist then do "git clone" | |
call(['git', 'clone', gist['git_pull_url'], gist_dir]) | |
else: | |
# if repo dir already exists then do "git pull" | |
call(['git', '-C', gist_dir, 'pull']) | |
if gist['comments']: | |
comments_file = os.path.join(gist_dir, 'comments.json') | |
with open(comments_file, 'w') as fh_comm: | |
fh_comm.write(json.dumps(requests.get(gist['comments_url']).json(), indent=1, sort_keys=True)) | |
description_file = os.path.join(gist_dir, 'description.json') | |
with open(description_file, 'w') as fh_desc: | |
desc = { | |
"description": gist['description'], | |
"json_url": gist['url'], | |
"html_url": gist['html_url'], | |
"pull_url": gist['git_pull_url'], | |
"id": gist['id'], | |
"updated": gist['updated_at'], | |
"files": gist['files'] | |
} | |
json.dump(desc, fh_desc, ensure_ascii=False, indent=1, sort_keys=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment