4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

QiitaAPI: cliでqiitaの検索結果を取得してみた

Last updated at Posted at 2015-07-20

参考

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()

使用例

1.png

2.png

3.png

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?