Last active
December 14, 2015 16:19
-
-
Save narusemotoki/5114722 to your computer and use it in GitHub Desktop.
自分のGistを検索できないらしいので、ユーザ名とdescriptionに含まれていて欲しい文字列を引数で渡すと、引っかかったGistのdescriptionとURLを出力するスクリプト書いた。
指定したユーザのGistを全て持ってきてからdescriptionの中を見ていくので遅い。しかもすぐにAPI上限に届いてしまう。
$ python gistsearch narusemotoki python
This file contains hidden or 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/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