Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download all gists for an authenticated user
#!/usr/bin/env python3
from os import makedirs, mkdir
import requests
# Generate a personal token for GitHub and replace it here.
GITHUB_USERNAME='username'
GITHUB_TOKEN='token'
def get_gists(page=1):
gist_list_url = 'https://api.github.com/gists'
gist_list_request = requests.get(gist_list_url,
params={'page': str(page)},
auth=(GITHUB_USERNAME,GITHUB_TOKEN))
return gist_list_request
def aggregate_gists():
page = 1
gists = get_gists()
gist_list = gists.json()
ok = True
if gists.status_code == 200:
while ok:
print(f'Fetching page {page}')
page = page + 1
# Attempt to get next page, if any.
next_gists = get_gists(page)
if next_gists.status_code != 200 or len(next_gists.json()) == 0:
print(f'Page {page} not found')
ok = False
else:
gist_list.extend(next_gists.json())
return gist_list
def save_gist(gist):
dir = f"gists/{gist['id']}"
for file in gist['files']:
file = gist['files'][file]
makedirs(dir, exist_ok=True)
response = requests.get(file['raw_url'],
auth=(GITHUB_USERNAME,GITHUB_TOKEN))
with open(f"{dir}/{file['filename']}", 'wb') as f:
f.write(response.content)
if __name__ == '__main__':
gist_list = aggregate_gists()
print("Saving gist list")
for gist in gist_list:
save_gist(gist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment