Skip to content

Instantly share code, notes, and snippets.

@P-rgb-max
Last active February 4, 2024 17:08
Show Gist options
  • Save P-rgb-max/925e9718c5fc61687741366007cf0cad to your computer and use it in GitHub Desktop.
Save P-rgb-max/925e9718c5fc61687741366007cf0cad to your computer and use it in GitHub Desktop.
Shows last GitHub gists via API
#!/usr/bin/env python3
import requests as req
listUrl = 'https://api.github.com/gists/public'
rawGists = req.get(listUrl).json()[:30]
gists = {}
for gist in rawGists:
files = {}
for name, file in gist['files'].items():
files[name] = file['raw_url']
gists[gist['description']] = files
for i, desc in enumerate(gists.keys()):
print(f'{i + 1}. {desc}')
sel = 1
while True:
sel = input('gist>')
try:
sel = int(sel)
except:
print('Enter a number!')
else:
break
files = gists[list(gists)[sel - 1]]
for i, name in enumerate(files.keys()):
print(f'{i + 1}. {name}')
sel = 1
while True:
sel = input('file>')
try:
sel = int(sel)
except:
print('Enter a number!')
else:
break
name = list(files)[sel - 1]
file = req.get(files[name]).text
with open(name, 'w') as f:
f.write(file)
print(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment