Created
August 16, 2014 14:36
-
-
Save wellsnake/b8ddd8f11d4b712af3fd to your computer and use it in GitHub Desktop.
douban_workflow
This file contains 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/python | |
# encoding: utf-8 | |
__author__ = 'wellsnake' | |
import sys | |
from workflow import Workflow, ICON_WEB, web | |
def main(wf): | |
# 设置豆瓣图书查询API地址 | |
doubanURL = 'https://api.douban.com/v2/book/search' | |
# 根据豆瓣API接口文档设置API参数,q为查询的内容,count表示返回结果最多个数 | |
# wf.args[0]表示查询内容为alfred传递进来的查询内容 | |
params = dict(q=wf.args[0], count=20) | |
# 获取豆瓣图书API的调用结果 | |
r = web.get(doubanURL, params) | |
# 如果有错直接抛出异常 | |
r.raise_for_status() | |
# 开始循环获取返回结果内图书信息,豆瓣API接口返回的都是json格式 | |
for post in r.json()['books']: | |
# 因为接口返回的作者是一个list(可能存在多个作者),所以需要遍历出所有的作者 | |
authors = '' | |
for author in post['author']: | |
authors += author + ' ' | |
# 开始拼接图书详细信息的字符串,包括作者、出版社、单价、评分 | |
detail = u'作者:' + authors + u'|出版社:' + post['publisher'] + u'|单价:' + post['price'] + \ | |
u'|评分:' + post['rating']['average'] | |
# 将结果加入到WorkFlow对象内 | |
# add_item参数具体为:大标题、小标题(详细信息)、传出的参数、貌似用来排序?、是否可以被action控件继续调用、图标 | |
wf.add_item(post['title'], detail, arg=post['id'], uid=post['id'], valid=False, icon=ICON_WEB) | |
# 将最终结果以xml格式反馈给控制台或者Alfred | |
wf.send_feedback() | |
if __name__ == '__main__': | |
wf = Workflow() | |
sys.exit(wf.run(main)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment