douban_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,93 @@
1
+ module Douban
2
+ class Client
3
+ # 豆瓣日记 API V2 http://developers.douban.com/wiki/?title=note_v2
4
+ module Note
5
+ def create_note(options={})
6
+ post "v2/notes", options
7
+ end
8
+
9
+ def notes(user_id=nil, format="text", options={})
10
+ options.merge!(:format => format)
11
+
12
+ if user_id.nil?
13
+ response = get("v2/note/user_created/#{get_user_id}", options)
14
+ else
15
+ response = get("v2/note/user_created/#{user_id}", options)
16
+ end
17
+
18
+ response["notes"]
19
+ end
20
+
21
+ def liked_notes(user_id=nil, format="text", options={})
22
+ options.merge!(:format => format)
23
+
24
+ if user_id.nil?
25
+ response = get("v2/note/user_liked/#{get_user_id}", options)
26
+ else
27
+ response = get("v2/note/user_liked/#{user_id}", options)
28
+ end
29
+
30
+ response["notes"]
31
+ end
32
+
33
+ def notes_guesses(user_id=nil, format="text", options={})
34
+ options.merge!(:format => format)
35
+
36
+ if user_id.nil?
37
+ response = get("/v2/note/people_notes/#{get_user_id}/guesses", options)
38
+ else
39
+ response = get("v2/note/user_liked/#{user_id}", options)
40
+ end
41
+
42
+ response["notes"]
43
+ end
44
+
45
+ def note(id, format="text")
46
+ options.merge!(:format => format)
47
+ get "v2/note/#{id}", options
48
+ end
49
+
50
+ def create_note(options={})
51
+ post "v2/notes", options
52
+ end
53
+
54
+ def like_note(id)
55
+ post "v2/note/#{id}/like"
56
+ end
57
+
58
+ def unlike_note(id)
59
+ delete "v2/note/#{id}/like"
60
+ end
61
+
62
+ def update_note(id, options={})
63
+ put "v2/note/#{id}", options
64
+ end
65
+
66
+ def upload_photon_to_note(id, options)
67
+ post "v2/note/#{id}", options
68
+ end
69
+
70
+ def remove_note(id)
71
+ delete("v2/note/#{id}")["status"] == 200
72
+ end
73
+
74
+ def note_comments(id, options={})
75
+ response = get("v2/note/#{id}/comments", options)
76
+ response["comments"]
77
+ end
78
+
79
+ def create_note_comment(id, content)
80
+ post "v2/note/#{id}/comments", {:content => content}
81
+ end
82
+
83
+
84
+ def note_comment(note_id, comment_id)
85
+ get "v2/note/#{note_id}/comment/#{comment_id}"
86
+ end
87
+
88
+ def remove_note_comment(note_id, comment_id)
89
+ delete "v2/note/#{note_id}/comment/#{comment_id}"
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,105 @@
1
+ module Douban
2
+ class Client
3
+ # Douban api online onlines
4
+ # @see http://developers.douban.com/wiki/?title=online_v2
5
+ module Online
6
+ def online(id)
7
+ response = get "v2/online/#{id}"
8
+ end
9
+
10
+ def online_participants(id)
11
+ response = get "v2/online/#{id}/participants"
12
+ response["users"]
13
+ end
14
+
15
+ def online_discussions(id)
16
+ response = get "v2/online/#{id}/discussions"
17
+ response["discussions"]
18
+ end
19
+
20
+ def onlines(options={})
21
+ response = get "v2/onlines", options
22
+ response["onlines"]
23
+ end
24
+
25
+ def create_online(title, options={})
26
+ post "v2/onlines", options.merge(:title => title)
27
+ end
28
+
29
+ def update_online(id, options={})
30
+ put "v2/online/#{id}", options
31
+ end
32
+
33
+ def remove_online(id)
34
+ delete "v2/online/#{id}"
35
+ end
36
+
37
+ def participants_online(id)
38
+ post "v2/online/#{id}/participants"
39
+ end
40
+
41
+ def unparticipants_online(id)
42
+ post "v2/online/#{id}/participants"
43
+ end
44
+
45
+ def like_online(id)
46
+ post "v2/online/#{id}/like"
47
+ end
48
+
49
+ def unline_online(id)
50
+ delete "v2/online/#{id}/like"
51
+ end
52
+
53
+ def online_photos(id)
54
+ response = get "v2/online/#{id}/photos"
55
+ response["photos"]
56
+ end
57
+
58
+ def online_discussions(id)
59
+ response = get "v2/online/#{id}/discussions"
60
+ response["discussions"]
61
+ end
62
+
63
+ def upload_online_photo(id, options={})
64
+ post "v2/online/#{id}/photos", options
65
+ end
66
+
67
+
68
+ def created_onlines(id)
69
+ if id.nil?
70
+ response = get "v2/online/user_created/#{get_user_id}"
71
+ else
72
+ response = get "v2/onlines/user_created/#{id}"
73
+ end
74
+
75
+ response["onlines"]
76
+ end
77
+
78
+ def participated_onlines(id=nil)
79
+ if id.nil?
80
+ response = get "v2/online/user_participated/#{get_user_id}"
81
+ else
82
+ response = get "v2/online/user_participated/#{id}"
83
+ end
84
+
85
+ response["onlines"]
86
+ end
87
+
88
+ def wished_onlines(id=nil)
89
+ if id.nil?
90
+ response = get "v2/online/user_wished/#{get_user_id}"
91
+ else
92
+ response = get "v2/online/user_wished/#{id}"
93
+ end
94
+
95
+ response["onlines"]
96
+ end
97
+
98
+ def onlines(loc_id, options={})
99
+ response = get "v2/online/list", optins.merge(:loc => loc_id)
100
+ response["onlines"]
101
+ end
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,363 @@
1
+ module Douban
2
+ class Client
3
+ # 豆瓣广播 Api V2 http://developers.douban.com/wiki/?title=shuo_v2
4
+ module Shuo
5
+ # 发送一条广播
6
+ #
7
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
8
+ # @scope shuo_basic_w
9
+ # @authenticated true
10
+ # @param text [String] 广播文本内容
11
+ # @option options [String] :rec_title 推荐网址的标题
12
+ # @option options [String] :rec_url 推荐网址的href
13
+ # @option options [String] :rec_desc 推荐网址的描述
14
+ # @return [Hashie::Mash] 发送成功的广播数据
15
+ # @example 发送一条广播
16
+ # client.create_status("嘻嘻")
17
+ # TODO 支持附带图片的广播
18
+ def create_status(text, options={})
19
+ options.merge!(:text => text, :source => client_id)
20
+ post "shuo/v2/statuses/", options
21
+ end
22
+ alias :shuo :create_status
23
+
24
+ # 友邻广播
25
+ #
26
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
27
+ # @scope shuo_basic_r
28
+ # @authenticated true
29
+ # @option options [Integer] :since_id
30
+ # 若指定此参数,则只返回ID比since_id大的广播消息(即比since_id发表时间晚的广播消息)。
31
+ # @option options [Integer] :until_id
32
+ # 若指定此参数,则返回ID小于或等于until_id的广播消息
33
+ # @return [Array<Hashie::Mash>] 广播列表
34
+ # @example 获取当前认证用户的友邻广播
35
+ # client.timeline
36
+ def timeline(options={})
37
+ get "shuo/v2/statuses/home_timeline", options
38
+ end
39
+
40
+ # 获取用户发布的广播列表
41
+ #
42
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
43
+ # @scope shuo_basic_r
44
+ # @authenticated false
45
+ # @param name [String] 用户uid或者数字id
46
+ # @option options [Integer] :since_id
47
+ # 若指定此参数,则只返回ID比since_id大的广播消息(即比since_id发表时间晚的广播消息)。
48
+ # @option options [Integer] :until_id
49
+ # 若指定此参数,则返回ID小于或等于until_id的广播消息
50
+ # @return [Array<Hashie::Mash>] 广播列表
51
+ # @example 获取ahbei的广播列表
52
+ # Douban.user_timeline('ahbei')
53
+ def user_timeline(name=nil, options={})
54
+ get "shuo/v2/statuses/user_timeline/#{name}", options
55
+ end
56
+
57
+
58
+ # 读取一条广播
59
+ #
60
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
61
+ # @scope shuo_basic_r
62
+ # @authenticated false
63
+ # @param id [String] 广播的id
64
+ # @option pack [Boolean] 是否打包 resharers 和 comments 数据
65
+ # @return [Hashie::Mash] 广播信息
66
+ # @example 获取id为1057462112的广播,并且包含resharers 和 comments 数据
67
+ # Douban.status('1057462112', true)
68
+ def status(id, pack=false)
69
+ get "shuo/v2/statuses/#{id}", :pack => pack
70
+ end
71
+
72
+ # 删除一条广播(只有删除自己的广播)
73
+ #
74
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
75
+ # @scope shuo_basic_w
76
+ # @authenticated true
77
+ # @param id [String] 广播的id
78
+ # @return [Boolean] 删除成功则返回true, 否则false
79
+ # @example 删除id为1057462112的广播
80
+ # client.remove_status('1057462112')
81
+ def remove_status(id)
82
+ begin
83
+ delete "shuo/v2/statuses/#{id}"
84
+ return true
85
+ rescue Douban::NotFound
86
+ return false
87
+ end
88
+ end
89
+
90
+ # 获取一条广播的回复列表
91
+ #
92
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
93
+ # @scope shuo_basic_r
94
+ # @authenticated false
95
+ # @param id [String] 广播的id
96
+ # @return [Array<Hashie::Mash>] 评论列表
97
+ # @example 获取id为1056160363的广播的评论
98
+ # Douban.status_comments('1056160363')
99
+ def status_comments(id, options={})
100
+ get "shuo/v2/statuses/#{id}/comments", options
101
+ end
102
+
103
+ # 添加一条评论
104
+ #
105
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
106
+ # @scope shuo_basic_w
107
+ # @authenticated true
108
+ # @param id [String] 广播的id
109
+ # @param text [String] 评论的文本
110
+ # @return [Hashie::Mash] 广播信息
111
+ # @example 评论一个条广播
112
+ # client.create_status_comment('1057158586', "谢谢啊!")
113
+ # TODO report to douban api team
114
+ def create_status_comment(id, text)
115
+ post "shuo/v2/statuses/#{id}/comments", options.merge(:text => text)
116
+ end
117
+
118
+ # 获取单条回复的内容
119
+ #
120
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
121
+ # @scope shuo_basic_r
122
+ # @authenticated false
123
+ # @param id [String] 评论的id
124
+ # @return [Hashie::Mash] 评论信息
125
+ # @example 获取 id 为2998638 的评论
126
+ # Douban.comment('2998638')
127
+ def comment(id)
128
+ get "shuo/v2/statuses/comment/#{id}"
129
+ end
130
+
131
+ # 删除回复
132
+ #
133
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
134
+ # @scope shuo_basic_w
135
+ # @authenticated true
136
+ # @param id [String] 评论的id
137
+ # @return [Boolean] 删除成功则返回true, 否则false
138
+ # @example 删除 id 为2998638 的评论
139
+ # client.remove_comment('2998638')
140
+ def remove_comment(id)
141
+ begin
142
+ delete "shuo/v2/statuses/comment/#{id}"
143
+ return true
144
+ rescue Douban::NotFound
145
+ return false
146
+ end
147
+ end
148
+
149
+ # 转播
150
+ #
151
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
152
+ # @scope shuo_basic_w
153
+ # @authenticated true
154
+ # @param id [String] 广播id
155
+ # @return [Hashie::Mash] 广播列表
156
+ # @example 转播id为1057472949的广播
157
+ # client.reshare('1057472949')
158
+ def create_reshare(id)
159
+ post "shuo/v2/statuses/#{id}/reshare"
160
+ end
161
+ alias :reshare :create_reshare
162
+
163
+ # 获取最近转播的用户列表
164
+ #
165
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
166
+ # @scope shuo_basic_r
167
+ # @authenticated false
168
+ # @param id [String] 广播id
169
+ # @return [Array<Hashie::Mash>] 用户列表
170
+ # @example 查看id为1057472949的广播的转发者
171
+ # Douban.resharers('1057472949')
172
+ def resharers(id, options={})
173
+ get "shuo/v2/statuses/#{id}/reshare", options
174
+ end
175
+
176
+ # 删除转播
177
+ #
178
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
179
+ # @scope shuo_basic_w
180
+ # @authenticated true
181
+ # @param id [String] 广播的id
182
+ # @return [Boolean] 删除成功则返回true, 否则false
183
+ # @example 删除用户对id为1057472949的广播的转播
184
+ # client.remove_reshare('1057472949')
185
+ def remove_reshare(id)
186
+ begin
187
+ delete "shuo/v2/statuses/#{id}/reshare"
188
+ return true
189
+ rescue Douban::NotFound
190
+ return false
191
+ end
192
+ end
193
+ alias :unreshare :remove_reshare
194
+
195
+ # 赞一条广播
196
+ #
197
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
198
+ # @scope shuo_basic_w
199
+ # @authenticated true
200
+ # @param id [String] 广播的id
201
+ # @return [Hashie::Mash] 对应的广播数据
202
+ # @example 赞id为1057472949的广播
203
+ # client.like('1057472949')
204
+ def like(id)
205
+ post "shuo/v2/statuses/#{id}/like"
206
+ end
207
+
208
+ # 获取最近赞的用户列表
209
+ #
210
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
211
+ # @scope shuo_basic_r
212
+ # @authenticated false
213
+ # @param id [String] 广播的id
214
+ # @return [Array<Hashie::Mash>] 用户列表
215
+ # @example 获取id为1057472949的用户
216
+ # Douban.liked_users('1057472949')
217
+ def liked_users(id, options={})
218
+ get "shuo/v2/statuses/#{id}/like", options
219
+ end
220
+
221
+ # 取消赞一条广播
222
+ #
223
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
224
+ # @scope shuo_basic_w
225
+ # @authenticated true
226
+ # @param id [String] 广播的id
227
+ # @return [Hashie::Mash] 对应的广播数据
228
+ # @example 取消赞id为1057472949的广播
229
+ # client.unlike('1057472949')
230
+ def unlike(id)
231
+ delete "shuo/v2/statuses/#{id}/like"
232
+ end
233
+
234
+ # 获取用户关注列表
235
+ #
236
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
237
+ # @scope shuo_basic_r
238
+ # @authenticated false
239
+ # @option options [Integer] :tag 该tag的id
240
+ # @param user_id [String] 用户的数字id
241
+ # @return [Array<Hashie::Mash>] 用户列表
242
+ # @example 获取2217855关注的用户
243
+ # Douban.following('2217855')
244
+ def following(user_id=nil, options={})
245
+ if user_id.nil?
246
+ get "shuo/v2/users/#{get_user_id}/following", options
247
+ else
248
+ get "shuo/v2/users/#{user_id}/following", options
249
+ end
250
+ end
251
+
252
+ # 获取用户关注者列表
253
+ #
254
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
255
+ # @scope shuo_basic_r
256
+ # @authenticated false
257
+ # @param user_id [String] 用户的数字id
258
+ # @return [Array<Hashie::Mash>] 用户列表
259
+ # @example 获取2217855的关注者
260
+ # Douban.followers('2217855')
261
+ def followers(user_id=nil, options={})
262
+ if user_id.nil?
263
+ get "shuo/v2/users/#{get_user_id}/followers", options
264
+ else
265
+ get "shuo/v2/users/#{user_id}/followers", options
266
+ end
267
+ end
268
+
269
+ # 获取共同关注的用户列表
270
+ #
271
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
272
+ # @scope shuo_basic_r
273
+ # @authenticated true
274
+ # @param user_id [String] 用户的数字id
275
+ # @return [Array<Hashie::Mash>] 用户列表
276
+ # @example 获取已认证用户和2012964的共同关注者
277
+ # client.follow_in_common('2012964')
278
+ def follow_in_common(user_id, options={})
279
+ get "shuo/v2/users/#{user_id}/follow_in_common", options
280
+ end
281
+
282
+ # 获取关注的人关注了该用户的列表
283
+ #
284
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
285
+ # @scope shuo_basic_r
286
+ # @authenticated true
287
+ # @param user_id [String] 用户的数字id
288
+ # @return [Array<Hashie::Mash>] 用户列表
289
+ # @example 关注的人也关注2012964的用户
290
+ # client.following_followers_of('2012964')
291
+ def following_followers_of(user_id=ni, options={})
292
+ get "shuo/v2/users/#{user_id}/following_followers_of"
293
+ end
294
+
295
+ # 搜索用户
296
+ #
297
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
298
+ # @scope shuo_basic_r
299
+ # @authenticated false
300
+ # @param q [String] 搜索字符串
301
+ # @return [Array<Hashie::Mash>] 用户列表
302
+ # @example 关注的人也关注2012964的用户
303
+ # Douban.shuo_search_users("小明")
304
+ def shuo_search_users(q, options={})
305
+ get "shuo/v2/users/search", options.merge(:q => q)
306
+ end
307
+
308
+ # block用户
309
+ #
310
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
311
+ # @scope shuo_basic_w
312
+ # @authenticated true
313
+ # @param user_id [String] 用户的数字id
314
+ # @return [Boolean] block成功则返回true, 否则false
315
+ # @example block id为2012964的用户
316
+ # client.block('2012964')
317
+ def block(user_id)
318
+ post("shuo/v2/users/#{user_id}/block")["r"] == 1
319
+ end
320
+
321
+ # follow一个用户
322
+ #
323
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
324
+ # @scope shuo_basic_w
325
+ # @authenticated true
326
+ # @param user_id [String] 用户的数字id
327
+ # @return [Hashie::Mash] follow用户的信息
328
+ # @example follow id为2012964的用户
329
+ # client.follow('2012964')
330
+ def follow(user_id)
331
+ post "shuo/v2/friendships/create", :source => client_id, :user_id => user_id
332
+ end
333
+
334
+ # 取消关注一个用户
335
+ #
336
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
337
+ # @scope shuo_basic_w
338
+ # @authenticated true
339
+ # @param user_id [String] 用户的数字id
340
+ # @return [Hashie::Mash] unfollow用户的信息
341
+ # @example 取消关注 id为2012964的用户
342
+ # client.unfollow('2012964')
343
+ def unfollow(user_id)
344
+ post "shuo/v2/friendships/destroy", :source => client_id, :user_id => user_id
345
+ end
346
+
347
+ # 取消关注一个用户
348
+ #
349
+ # @see http://developers.douban.com/wiki/?title=shuo_v2
350
+ # @scope shuo_basic_r
351
+ # @authenticated false
352
+ # @param source_id [String] 用户的数字id
353
+ # @param target_id [String] 用户的数字id
354
+ # @return [Hashie::Mash] 关系的信息
355
+ # @example 获取 id 2217855 和 2012964 的用户关系
356
+ # client.friendship('2217855','2012964')
357
+ def friendship(source_id, target_id)
358
+ options = {:source_id => source_id, :target_id => target_id, :source => client_id}
359
+ get "shuo/v2/friendships/show", options
360
+ end
361
+ end
362
+ end
363
+ end
@@ -0,0 +1,47 @@
1
+ module Douban
2
+ class Client
3
+ # 豆瓣用户API V2 http://developers.douban.com/wiki/?title=user_v2
4
+ module User
5
+
6
+ # 获取用户信息
7
+ #
8
+ # @scope douban_basic_common
9
+ # @see http://developers.douban.com/wiki/?title=user_v2#get_user
10
+ # @authenticated false
11
+ # @param id [String] 用户uid或者数字id
12
+ # @return [Hashie::Mash] 用户信息
13
+ # @example 查看ahbei的信息
14
+ # Douban.user('ahbei')
15
+ def user(user_id="~me")
16
+ get "v2/user/#{user_id}"
17
+ end
18
+
19
+ # 获取当前授权用户信息
20
+ #
21
+ # @scope douban_basic_common
22
+ # @see http://developers.douban.com/wiki/?title=user_v2#get_me
23
+ # @authenticated true
24
+ # @return [Hashie::Mash] 用户信息
25
+ # @example 获取当前授权用户信息
26
+ # client.me
27
+ def me
28
+ user
29
+ end
30
+
31
+ # 搜索用户
32
+ #
33
+ # @scope douban_basic_common
34
+ # @see http://developers.douban.com/wiki/?title=user_v2#search
35
+ # @authenticated false
36
+ # @param q [String] 全文检索的关键词
37
+ # @return [Array<Hashie::Mash>] 用户信息列表
38
+ # @example 获取当前授权用户信息
39
+ # Douban.search_users("傻多速")
40
+ def search_users(q, options={})
41
+ response = get "v2/user", options.merge(:q => q)
42
+ response["users"]
43
+ end
44
+ alias :users :search_users
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ module Douban
2
+ class Client
3
+ # @private
4
+ module Utils
5
+ private
6
+
7
+ # Returns the configured user name or the user name of the authenticated user
8
+ #
9
+ # @return [String]
10
+ def get_user_id
11
+ @user_id ||= self.user.id
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Douban
2
+ # Wrapper for the Douban REST API
3
+ #
4
+ # @note All methods have been separated into modules and follow the same grouping used in {TODO:doc_URL the Douban API Documentation}.
5
+ # @see TODO:doc_url
6
+ class Client < API
7
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
8
+
9
+ include Douban::Client::Utils
10
+
11
+ include Douban::Client::Book
12
+ include Douban::Client::User
13
+ include Douban::Client::Movie
14
+ include Douban::Client::Music
15
+ include Douban::Client::Event
16
+ include Douban::Client::Shuo
17
+ include Douban::Client::Doumail
18
+ include Douban::Client::Album
19
+ include Douban::Client::Note
20
+ include Douban::Client::Discussion
21
+ include Douban::Client::Online
22
+ include Douban::Client::Comment
23
+
24
+ end
25
+ end