Pull All Gists from Github?

Viewed 7603

Is there an API call or any scripts that I failed to overturn that would pull all my Gists from Github to an outside git repo or just return me a list of their names? I know each one is a separate git repo, so I have assumed the best I can do is get the latter, then script to get all of them onto my local box.

EDIT 1: I know about pulling and pushing git repos from one service to another, I am specifically looking for people who have the 411 on collecting an authoritative list of all Gists I have, private and public. I also thought this might be useful to others. It is not so much about migration, but a backup strategy . . . of sorts.

EDIT 2: So, it appears this might not be possible. I apparently did not Google hard enough to search the updated Github/Gist API. The other API calls work with simple curl commands, but not the v1 API for Gist. Still, the API says TBD for all private and public Gists, so I think that puts the cabash on the whole thing unless an enlightened soul hooks a brotha up.

$ curl http://github.com/api/v2/json/repos/show/alharaka
{"repositories":[{"url":"https://github.com/alharaka/babushka","has_wiki":true,"homepage":"http:
... # tons of more output
echo $?
0
$ 

This one does not work so hot.

$ curl https://gist.github.com/api/v1/:format/gists/:alharaka
$ echo $?
0
$

EDIT 3: Before I get asked, I noticed there is a difference in the API versioning; this "brilliant hack" did not help either. Still very cool though.

$ curl https://gist.github.com/api/v2/:format/gists/:alharaka # Notice v2 instead of v1
$ echo $?
0
$
11 Answers

March 2021 update (Python3)

If a user has a ton of gists with the same file name, this works great.

import requests, json, time, uuid
headers = {"content-type" : "application/json"}
url =  'https://api.github.com/users/ChangeToYourTargetUser/gists?per_page=100&page='

for page in range(1,100):  #do pages start at 1 or 0?
    print('page: ' + str(page))
    r = requests.get(url+str(page), headers = headers)
    metadata_file = './data/my_gist_list.json'
    # Getting metadata
    prettyJson = json.dumps(r.json(), indent=4, sort_keys=True)
    f = open(metadata_file, 'w')
    f.write(prettyJson)

    print('Metadata obtained as {}'.format(metadata_file))

    # Downloading files
    data = r.json()
    counter = 0
    for i in data:
        time.sleep(1.1)
        files_node = i['files']
        file_name = [k for k in files_node][0]
        r = requests.get(files_node[file_name]['raw_url'])
        f = open('./data/{}'.format(str(uuid.uuid4())), 'w')
        f.write(r.text)
        f.close()
        print('Download' + str(i))
        counter += 1

    print('{} files successfully downloaded.'.format(counter))

I use this and it works like a charm!


# first: mkdir user && cd user && cp /path/to/get_gists.py .
# python3 get_gists.py user
import requests
import sys
from subprocess import call

user = sys.argv[1]

r = requests.get('https://api.github.com/users/{0}/gists'.format(user))

for i in r.json():
    call(['git', 'clone', i['git_pull_url']])

    description_file = './{0}/description.txt'.format(i['id'])
    with open(description_file, 'w') as f:
        f.write('{0}\n'.format(i['description']))


And what about the GitHub CLI?

brew install gh

gh auth login

gh gist list [flags]

  Options:

    -L, --limit int   Maximum number of gists to fetch (default 10)
    --public          Show only public gists
    --secret          Show only secret gists

gh gist clone <gist> [<directory>] [-- <gitflags>...]
Related