Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
自分のGistを検索できないらしいので、ユーザ名とdescriptionに含まれていて欲しい文字列を引数で渡すと、引っかかったGistのdescriptionとURLを出力するスクリプト書いた。 指定したユーザのGistを全て持ってきてからdescriptionの中を見ていくので遅い。しかもすぐにAPI上限に届いてしまう。 $ python gistsearch narusemotoki python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib import urlopen
import json
import sys
base_url = 'https://api.github.com/users/{username}/gists?page='
def fetch(username):
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('[]')
url = base_url.format(username=username)
return fetch(1)
if __name__ == '__main__':
argc = len(sys.argv)
query = None
if 2 > argc:
print(u'第一引数はユーザ名[必須]、第二引数は検索文字列[任意]')
sys.exit()
elif 2 < argc:
query = sys.argv[2].strip().decode('utf-8')
for gist in fetch(sys.argv[1]):
description = gist['description']
if not None == description and \
(None == query or not -1 == description.find(query)):
print(description)
print(gist['url'])
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.