List git gist
This script will print the output in HTML format on stdout. Console command
$ python list_your_gitgist.py > out.html
| #!/usr/bin/env python | |
| #Filename: list_your_gitgist.py | |
| # List git gist | get git gist | |
| # Lisence Under GPL2 | |
| # Author: Shouro Chowdhury and Mahbub Zaman | |
| import sys, traceback | |
| import urllib2 | |
| import json | |
| userName = 'your_user_name' | |
| passWord = '******' | |
| userData = '' | |
| # No Authentication needed just call this method | |
| def getPublicGist(): | |
| ghUrl = 'https://api.github.com/users/'+userName+'/gists' # GitHub API | |
| handler = urllib2.urlopen(ghUrl).read() # type str | |
| jData = json.loads(handler) # type list, contains dictionary | |
| noOfGist = len(jData) # number of gist | |
| printGists(noOfGist, jData) | |
| # Authentication needed, call login() method before calling this method | |
| def getPrivateGist(userData): | |
| try: | |
| request = urllib2.Request('https://api.github.com/gists') # create a new Urllib2 Request object | |
| request.add_header('Authorization', userData) | |
| handler = urllib2.urlopen(request).read() | |
| jData = json.loads(handler) # type list, contains dictionary | |
| noOfGist = len(jData) # number of gist | |
| printGists(noOfGist, jData) | |
| except Exception, err: | |
| print 'Something BAD happend: ', err | |
| #print traceback.format_exc() | |
| sys.exit(0) | |
| def login(): | |
| userData = "Basic " + (userName + ":" + passWord).encode("base64").rstrip() # encode username and password | |
| return userData | |
| def printGists(noOfGist, jData): | |
| print '<html> <body> <h1>', userName,' </h1>' | |
| for i in range (0, noOfGist): | |
| print '<a '+'href=',jData[i][u'html_url'],' target="_blank" >' | |
| temp = jData[i][u'files'].keys() | |
| print 'Title: ',jData[i][u'files'][temp[0]][u'filename'] | |
| print '<br/>' | |
| des = jData[i][u'description'] | |
| if (des == ('')): | |
| print 'Description: '+'No description is available' | |
| else: | |
| print 'Description: ',des | |
| print '</a>' | |
| print '<br/>' | |
| print '<br/>' | |
| print '</body> </html>' | |
| def main(): | |
| #userData = login() | |
| #getPrivateGist(userData) | |
| getPublicGist() | |
| if __name__ == '__main__': | |
| main() | |