-
Notifications
You must be signed in to change notification settings - Fork 0
/
douban_wrapper.py
101 lines (86 loc) · 3.67 KB
/
douban_wrapper.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
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/env python
# -*- coding: utf-8 -*-
import urllib, urllib2, json
from config import DOUBAN_APIKEY
# 访问豆瓣API获取书籍数据
BOOK_URL_BASE = 'http://api.douban.com/v2/book/search'
# 访问豆瓣API获取电影数据
FILM_URL_BASE = 'http://api.douban.com/v2/movie/search'
# 访问豆瓣API获取音乐数据
MUSIC_URL_BASE = 'http://api.douban.com/v2/music/search'
# 通过豆瓣API获取书籍数据,然后把数据格式化成特定的格式:
# [{"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"},
# {"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"},
# {"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"}]
def search_book(q):
params = {'q': q, 'apikey': DOUBAN_APIKEY, 'count': 3}
url = BOOK_URL_BASE + '?' + urllib.urlencode(params)
resp = urllib2.urlopen(url)
r = json.loads(resp.read())
books = r['books']
bookList = []
for i, book in enumerate(books):
item = {}
title = u'%s\t%s分\n%s\n%s\t%s' % (
book['title'], book['rating']['average'], ','.join(book['author']), book['publisher'], book['price'])
description = ''
picUrl = book['images']['large'] if i == 1 else book['images']['small']
url = book['alt']
item['title'] = title
item['description'] = description
item['image'] = picUrl
item['url'] = url
bookList.append(item)
return bookList
# 通过豆瓣API获取电影数据,然后把数据格式化成特定的格式:
# [{"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"},
# {"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"},
# {"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"}]
def search_film(q):
params = {'q': q, 'apikey': DOUBAN_APIKEY, 'count': 3}
url = FILM_URL_BASE + '?' + urllib.urlencode(params)
resp = urllib2.urlopen(url)
r = json.loads(resp.read())
films = r['subjects']
filmList = []
for i, film in enumerate(films):
item = {}
title = u'%s\t%s分\n%s年\n%s' % (
film['title'], film['rating']['average'], film['year'], ','.join(film['genres']))
description = ''
picUrl = film['images']['large'] if i == 1 else film['images']['small']
url = film['alt']
item['title'] = title
item['description'] = description
item['image'] = picUrl
item['url'] = url
filmList.append(item)
return filmList
# 通过豆瓣API获取音乐数据,然后把数据格式化成特定的格式:
# [{"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"},
# {"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"},
# {"title":"失控", "description":"", "image":"http://xxx", "url":"http://xxx"}]
def search_music(q):
params = {'q': q, 'apikey': DOUBAN_APIKEY, 'count': 3}
url = MUSIC_URL_BASE + '?' + urllib.urlencode(params)
# raise Exception(url, url)
resp = urllib2.urlopen(url)
r = json.loads(resp.read())
musics = r['musics']
musicList = []
for i, music in enumerate(musics):
print music
item = {}
title = u'%s\t%s分\n%s\n%s\t%s' % (
music['title'], music['rating']['average'], ','.join(music['attrs']['singer']),
','.join(music['attrs']['publisher']),
','.join(music['attrs']['pubdate']))
description = ''
picUrl = music['image']
url = music['alt']
item['title'] = title
item['description'] = description
item['image'] = picUrl
item['url'] = url
musicList.append(item)
return musicList