-
Notifications
You must be signed in to change notification settings - Fork 0
/
douban.py
35 lines (31 loc) · 1015 Bytes
/
douban.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
# -*- coding:utf-8 -*-
import urllib2
import json
import traceback
def parse(url):
bookinfo = {}
if not url:
return [u"请输入URL"], bookinfo
while url[-1] == "/":
url = url[:-1]
posi = url.find("book.douban.com/subject/")
if posi == -1:
return
bookid = url[posi+24:]
apiurl = "https://api.douban.com/v2/book/"
if not bookid.isdigit():
return [u"URL错误"], bookinfo
try:
data = json.loads(urllib2.urlopen(apiurl+bookid).read())
print data
except:
return [u"API出错,请稍后再试"], bookinfo
keys = ["id", "title", "author","publisher", "pubdate", "isbn13", "price", "alt"]
for key in keys:
bookinfo[key] = data[key]
if isinstance(bookinfo[key], list):
bookinfo[key] = ' '.join(bookinfo[key])
bookinfo["mpic"] = data["images"]["medium"]
bookinfo["lpic"] = data["images"]["large"]
bookinfo["rating"] = data["rating"]["average"]
return "ok", bookinfo