-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
executable file
·88 lines (75 loc) · 2.32 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
import sys
import argparse
import requests
from spider import *
douban = 'https://api.douban.com'
def usage():
print """
-l limits default 20
-q query keyword
-s minimum score default 7
-h help
eg. douban_book -s 8 -q docker -l 100
"""
def print_books(books):
if len(books) == 0:
print 'None results'
sys.exit(0)
for book in books:
print 'Name: '+book['title']
print 'id: '+book['id']
if book['author']:
print 'Author: '+reduce(lambda x,y:x+','+y,book['author'])
print 'Average Score: '+book['rating']['average']
print 'Price: '+book['price']
print '\n'
print book['author_intro']
print '\n'
def query_books_by_word(word,limit):
search_url = douban+'/v2/book/search'
query = {}
query['q'] = word
query['count'] = limit
query['start'] = 0
r = requests.get(search_url,params=query)
data = r.json()
books = data['books']
results = filter(lambda x:float(x['rating']['average']) > score,books)
results = sorted(results,key=lambda x:x['rating']['average'],reverse=True)
print_books(results)
def query_book_by_id(book_id):
search_url = douban + '/v2/book/' + book_id
r = requests.get(search_url)
data = r.json()
def print_comments(comments):
for comment in comments:
print comment['author']+' '+comment['vote']+' Useful'
print comment['content']
print "\n"
if __name__ == '__main__':
if len(sys.argv) < 2:
usage()
sys.exit(0)
if '-h' in sys.argv:
usage()
sys.exit(0)
parser = argparse.ArgumentParser()
parser.add_argument("-l",type=int,default=20,help='query limits')
parser.add_argument("-q",type=str,help='query key word')
parser.add_argument("-s",type=float,default=7,help='minimum score')
parser.add_argument("-t",type=str,default='s',help='sript action')
parser.add_argument("-i",type=str,help='book id')
args = vars(parser.parse_args())
word = args['q']
limit = args['l']
score = args['s']
action = args['t']
book_id = args['i']
if action == 's':
query_books_by_word(word,limit)
elif action == 'c':
comments = query_book_comments(book_id)
print_comments(comments)
else:
usage()