Created
June 18, 2014 14:01
-
-
Save kanazux/57b8fb3287a8739ace2a to your computer and use it in GitHub Desktop.
list your gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# psychographed by kanazuchi <[email protected]> | |
import re | |
import argparse | |
from urllib2 import urlopen | |
def set_parser(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'-u', dest='gist_user', | |
action='store', | |
help='Entry with your gist username') | |
parser.add_argument( | |
'-f', dest='file_w', | |
action='store', | |
default = 'terminal', | |
help='Entry with the file to save your gists. Default is the prompt terminal') | |
return parser.parse_args() | |
def get_gists(user): | |
list_gists = [] | |
page = 1 | |
gist_page = urlopen('https://gist.github.com/{}?page={}'.format(user,page)).read().split('\n') | |
gists_in_page = [[re.sub('"','',gist.split()[1].split('=')[1]),'\t\thttps://gist.github.com/{}?page={}'.format(user,page)] for gist in gist_page if re.match(r'.*div.*class=\"file\ .*', gist)] | |
while len(gists_in_page) > 0: | |
for gist in gists_in_page: | |
list_gists.append(gist) | |
page = page + 1 | |
gist_page = urlopen('https://gist.github.com/{}?page={}'.format(user,page)).read().split('\n') | |
gists_in_page = [[re.sub('"','',gist.split()[1].split('=')[1]),'\t\thttps://gist.github.com/{}?page={}'.format(user,page)] for gist in gist_page if re.match(r'.*div.*class=\"file\ .*', gist)] | |
return list_gists | |
def print_gists(file_w,gists): | |
if file_w == 'terminal': | |
for gist in gists: | |
print '{}\t\t{}'.format(gist[0].replace('file-',''),gist[1]) | |
else: | |
for gist in gists: | |
print >> open(file_w,'a'), '{}\t\t{}'.format(gist[0].replace('file-',''),gist[1]) | |
if __name__ == '__main__': | |
opts = set_parser() | |
gists = get_gists(opts.gist_user) | |
if gists != '': | |
print_gists(opts.file_w,gists) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment