参考
qiita.py
#coding:utf-8
import sys
import urllib
import json
class QiitaApi:
def __init__(self):
self.baseurl = "http://qiita.com/api/v2/items?page=1&per_page=20"
def access(self, url=""):
response = urllib.urlopen(self.baseurl + url)
return json.loads(response.read())
def getSearchResult(self, search_str):
url = "&query=%s" % (search_str)
return self.access(url)
def blue(string):
return "\x1b[34m" + string + "\x1b[39m"
def getItems():
qapi = QiitaApi()
for item in qapi.access():
print item[u'title'] + "\n\t" + blue(item[u'url'])
def getSearchResult():
qapi = QiitaApi()
for item in qapi.getSearchResult(sys.argv[1]):
print item[u'title'] + "\n\t" + blue(item[u'url'])
def check_arg(argv):
if len(argv) == 1:
print "usage: %s search_str" % argv[0]
else:
return True
if __name__ == "__main__":
if check_arg(sys.argv) == True:
getSearchResult()
else:
getItems()