Skip to content

Instantly share code, notes, and snippets.

@puppybits
Last active April 10, 2022 21:04
Show Gist options
  • Save puppybits/5096082 to your computer and use it in GitHub Desktop.
Save puppybits/5096082 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
#
# Download all your gists locally.
# Change, modify and create new gists offline, then upload to github
#
# How to install:
# curl -O https://gist.github.com/raw/5096082/ac83f7ed9ce57323785291b91c71fd4d6e2c3d42/gist && export GITHUB_USERNAME=xxxxxxx && export GITHUB_PASSWORD=xxxxxxxx
#
# ## Feature Set:
# * list my gists
# * list my starred gists
# * download all my gists to ~/.gists
# * create new gist then upload it
# * stores user/pass in environment variables
#
# ## RoadMap:
# * alias map to open, update and save gists
# * add command line arguments
# * update/edit gist
# * download starred gists to ~/.gists/<username>/
# * fork a gist
# * better multi-file support
# * search all public gists
#
# v0.001
url_root = 'https://api.github.com/'
url_starred = ''.join([url_root, 'gists/starred'])
url_public = ''.join([url_root, 'gists/public'])
url_gists = ''.join([url_root, 'gists'])
url_gist = ''.join([url_root, 'gists/%s'])
home = '~/.gists'
import requests, os, simplejson as json
class Gist:
@staticmethod
def setCredentials(user, pazz):
os.system( 'export GITHUB_USERNAME=%s' % (user) )
os.system( 'export GITHUB_PASSWORD=%s' % (pazz) )
def __init__(self):
self.auth=(os.environ.get('GITHUB_USERNAME'),
os.environ.get('GITHUB_PASSWORD'))
self.exp = os.path.expanduser
def list_starred(self):
r = requests.get(url_starred, auth=self.auth )
j = r.json
for i in range(len(j)):
print '%i: [%s] %s\n' % (i, j[i]['user']['login'], j[i]['description'])
def list_gists(self):
r = requests.get(url_gists, auth=self.auth )
j = r.json
for i in range(len(j)):
print '%i: %s - files:%i \n' % (i, j[i]['description'], len(j[i]['files']))
# download all gists. multi-file gists get their own folder
def download(self):
if not os.path.exists(self.exp(home)):
os.makedirs(self.exp(home))
r = requests.get(url_gists, auth=self.auth )
j = r.json
for i in j:
subfolder = '' if len(i['files'].keys()) is 1 else i['id']
gistDir = self.exp( '%s/%s' % (home, subfolder) )
if not os.path.exists( gistDir ):
os.makedirs( gistDir )
for key in i['files']:
f = i['files'][key]
file = self.exp('%s/%s'%(gistDir, f['filename']))
file = open( file, 'w')
print f['raw_url']
c = requests.get( f['raw_url'] )
file.write( c.text )
file.close()
# currently only single file
def new(self, filename):
file = self.exp( '%s/%s' % (home, filename) )
f = open( file, 'w')
f.close()
os.system( 'mate %s' % (file) )
# send a new single file
def upload(self, file, message):
f = self.exp( '%s/%s' % (home, file) )
f = open(f, 'r').readlines()
data = {'description':message,
'public':True,
'files':
{file:
{'content':f[0]}
}
}
r = requests.post( url_gists, data=json.dumps(data))
# update a new file
# def update(self, file):
# # figure out where to get the gistid
# url = url_gist % (gistid)
#
# f = self.exp( '%s/%s' % (home, file) )
# f = open(f, 'r').readlines()
# data = {'description':message,
# 'public':True,
# 'files':
# {file:
# {'content':f[0]}
# }
# }
#
# requests.request('PATCH', url_gists, data=json.dumps(data))
# consumer_key = "Your Email Address"
# consumer_secret = "Your Password"
# Gist.setCredentials(consumer_key,consumer_secret)
# testing
Gist().list_gists()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment