Skip to content

Instantly share code, notes, and snippets.

@rene-d
Forked from leoloobeek/get_gists.py
Last active October 29, 2023 15:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rene-d/7af3410ea34c4104c1cc6dd3f46b346f to your computer and use it in GitHub Desktop.
Save rene-d/7af3410ea34c4104c1cc6dd3f46b346f to your computer and use it in GitHub Desktop.
Download all gists for a specific user
#! /usr/bin/env python3
import requests
import sys
from subprocess import call
import yaml
import os
import json
import argparse
COLOR_RED = "\033[1;31m"
COLOR_BROWN = "\033[0;33m"
COLOR_LIGHT_CYAN = "\033[1;36m"
COLOR_END = "\033[0m"
parser = argparse.ArgumentParser(description='Download all gists for a given user')
parser.add_argument('-v', '--verbose', help="be verbose", action='store_true')
parser.add_argument('-f', '--file', help="configuration file", default="gists.yaml")
parser.add_argument('user', nargs='?', help="username")
args = parser.parse_args()
if args.verbose:
print("args={}".format(args))
config = {}
if os.path.isfile(args.file):
with open(args.file) as f:
config = yaml.load(f, Loader=yaml.SafeLoader)
# if a username is given, keep only its gists in the configuration, if any
if args.user:
config = {args.user: config.get(args.user, {})}
if args.verbose:
print("config={}".format(config))
# get the gists
for user, names in config.items():
if user == "self":
if os.path.isfile(os.path.expanduser("~/.gist")):
token = open(os.path.expanduser("~/.gist")).read()
r = requests.get('https://api.github.com/gists?access_token=' + token.format(user))
else:
print("missing ~/.gist file")
continue
else:
r = requests.get('https://api.github.com/users/{0}/gists'.format(user))
if r.status_code != 200:
print("error with user {}: {}".format(user, r))
continue
print("Found {} gist(s)".format(len(r.json())))
# with open('data.json', 'wb') as f:
# f.write(r.content)
# for each gist of the user
for i in r.json():
if args.verbose:
print()
print(json.dumps(i, indent=4))
id = i['id']
if names and id in names:
if os.path.isdir(id):
from shutil import rmtree
rmtree(id)
id = names[id]
if i['public'] is False:
secret = COLOR_RED + " (secret)"
else:
secret = ""
sys.stdout.write(COLOR_BROWN + "Updating " + id + secret + COLOR_BROWN + " ... " + COLOR_END)
sys.stdout.flush()
if os.path.isdir(id):
call(['git', '-C', id, 'pull'])
else:
call(['git', 'clone', i['git_pull_url'], id])
description_file = os.path.join(id, 'description.txt')
with open(description_file, 'w') as f:
f.write('{}\n\n'.format(i['html_url']))
f.write('{0}\n'.format(i['description']))
# data_file = os.path.join(id, 'data.json')
# with open(data_file, 'w') as f:
# json.dump(i, f, indent=4)
# get_gists.py configuration file
# Syntax:
# <username>:
# <gist id>: <pretty name>
rene-d:
7af3410ea34c4104c1cc6dd3f46b346f: get-gists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment