Skip to content

Instantly share code, notes, and snippets.

@hidsh
Forked from narusemotoki/gistsearch.py
Last active December 17, 2015 01:48
Show Gist options
  • Save hidsh/5530663 to your computer and use it in GitHub Desktop.
Save hidsh/5530663 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# gistsearch.py
#
from urllib import urlopen
import json
import sys
import os
github_id = 'gnrr' # github's user id
def exit_usage():
print('USAGE: %s keyword\n' % os.path.basename(sys.argv[0]))
sys.exit()
def fetch(id):
def fetch(page):
print('page: ' + str(page))
gists = json.loads(urlopen(url + str(page)).read())
message = gists['message'] if 'message' in gists else ''
if -1 == message.find('API Rate Limit'):
if 0 == len(gists):
return gists
return gists + fetch(page + 1)
print(message)
return json.loads('[]')
base_url = 'https://api.github.com/users/{username}/gists?page='
url = base_url.format(username=id)
return fetch(1)
if __name__ == '__main__':
if len(sys.argv) != 2: exit_usage()
keyword = sys.argv[1].strip().decode('utf-8')
for gist in fetch(github_id):
description = gist['description']
if not None == description and \
(None == keyword or not -1 == description.find(keyword)):
print(description)
print(gist['url'])
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment