Module: Douban::Client::Book

Included in:
Douban::Client
Defined in:
lib/douban_api/client/book.rb

Overview

图书Api V2

Instance Method Summary collapse

Instance Method Details

#annotation(id, options = {}) ⇒ Hashie::Mash

获取某篇笔记的信息

Examples:

获取id为5963722的笔记的信息

client.annotation('5963722')

Parameters:

  • id (String)

    笔记的id

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :format (String)

    返回content字段格式,选填(编辑伪标签格式:text,HTML格式:html)默认为text

Returns:

  • (Hashie::Mash)

    笔记的信息

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_r



170
171
172
# File 'lib/douban_api/client/book.rb', line 170

def annotation(id, options={})
  response = get "v2/book/annotation/#{id}", options
end

#book(id) ⇒ Hashie::Mash

获取图书信息

Examples:

获取 id为3821057 的图书信息

Douban.book('3821057')

Parameters:

  • id (String)

    图书的id

Returns:

  • (Hashie::Mash)

    图书信息

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



17
18
19
# File 'lib/douban_api/client/book.rb', line 17

def book(id)
  response = get "v2/book/#{id}"
end

#book_annotations(id, options = {}) ⇒ Array<Hashie::Mash>

获取某本图书的所有笔记

Examples:

获取 id为3821057的图书 的笔记

client.book_annotations('3821057')

Parameters:

  • id (String)

    图书的id

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :format (String)

    返回content字段格式: 选填(编辑伪标签格式:text,HTML格式:html)默认为text

  • :order (String)

    排序: 选填(最新笔记:collect, 按有用程度:rank, 按页码先后:page),默认为rank

  • :page (Integer)

    按页码过滤: 选填

Returns:

  • (Array<Hashie::Mash>)

    笔记的列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



190
191
192
193
# File 'lib/douban_api/client/book.rb', line 190

def book_annotations(id, options={})
  response = get "v2/book/#{id}/annotations"
  response["annotations"]
end

#book_tags(id, optins = {}) ⇒ Array<Hashie::Mash>

某个图书中标记最多的标签, 最多返回前50个tag

Examples:

获取 id为3821057 的图书的标记最多的标签

Douban.book_tags("3821057")

Parameters:

  • id (String)

    图书的id

Returns:

  • (Array<Hashie::Mash>)

    标签的列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



71
72
73
74
# File 'lib/douban_api/client/book.rb', line 71

def book_tags(id, optins={})
  response = get "v2/book/#{id}/tags", options
  responses["tags"]
end

#books(name = nil, options = {}) ⇒ Array<Hashie::Mash>

获取某个用户的所有图书收藏信息

TODO 可以允许options和options传入ruby日期对象

Examples:

获取ahbei的所有图书收藏

Douban.books('ahbei')      

获取已认证用户的所有图书收藏

client.books()

Parameters:

  • name (String) (defaults to: nil)

    用户uid或者数字id

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :status (String)

    收藏状态: 选填(想读:wish 在读:reading 读过:read)默认为所有状态

  • :tag (String)

    收藏标签: 选填

  • :from (String)

    收藏更新时间过滤的起始时间

    选填,格式为符合rfc3339的字符串,例如“2012-10-19T17:14:11”,其他信息默认为不传此项

  • :to (String)

    收藏更新时间过滤的结束时间:同上

  • :rating (Integer)

    星评: 选填,数字1~5为合法值,其他信息默认为不区分星评

Returns:

  • (Array<Hashie::Mash>)

    该用户的图书收藏列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



320
321
322
323
324
325
326
327
328
# File 'lib/douban_api/client/book.rb', line 320

def books(name=nil, options={})
  if name.nil?
    response = get("v2/book/user/#{get_user_id}/collections", options)
  else
    response = get("v2/book/user/#{name}/collections", options)
  end

  response["collections"]
end

#change_book_collection(id, status, options = {}) ⇒ Hashie::Mash

获取某个用户的所有图书收藏信息

Examples:

度过id为3821057的图书,并标记为5星

client.change_book_collection('3821057', 'read', {
  :rating => 5
  :tag => "小说 美国 文学"
})

Parameters:

  • id (String)

    图书的id

  • status (String)

    收藏状态(想读:wish 在读:reading 读过:read)

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :tag (String)

    收藏标签字符串: 选填,用空格分隔

  • :comment (String)

    短评文本: 选填,最多350字

  • :privacy (String)

    隐私设置: 选填,值为‘private’为设置成仅自己可见,其他默认为公开

  • :rating (Integer)

    星评: 选填,数字1~5为合法值,其他信息默认为不区分星评

Returns:

  • (Hashie::Mash)

    返回创建的图书收藏信息

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_w



375
376
377
# File 'lib/douban_api/client/book.rb', line 375

def change_book_collection(id, status, options={})
  post "v2/book/#{id}/collection", options.merge(:status => status)
end

#create_book_annotation(id, options = {}) ⇒ Hashie::Mash

用户给某本图书写笔记

TODO 支持图片

Examples:

给id3821057的图书的第10页添加笔记

client.create_book_annotations('3821057',{
  :content =>"据说是现在新浪共享上是有下载的",
  :page => 10
})

Parameters:

  • id (String)

    图书的id

  • options (String) (defaults to: {})

    :content 笔记内容: 必填,需多于15字

Options Hash (options):

  • :page (Integer)

    页码: 页码或章节名选填其一,最多6位正整数

  • :chapter (String)

    章节名: 页码或章节名选填其一,最多100字

  • :privacy (String)

    隐私设 选填: 值为‘private’为设置成仅自己可见,其他默认为公开

Returns:

  • (Hashie::Mash)

    该笔记内容

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_w



216
217
218
# File 'lib/douban_api/client/book.rb', line 216

def create_book_annotation(id, options={})
  post "v2/book/reviews", options
end

#create_book_collection(id, status, options = {}) ⇒ Hashie::Mash

获取某个用户的所有图书收藏信息

Examples:

想读id为3821057的图书,并标记标签

client.create_book_collection('3821057', 'wish', {
  :tag => "小说 美国 文学"
})

Parameters:

  • id (String)

    图书的id

  • status (String)

    收藏状态(想读:wish 在读:reading 读过:read)

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :tag (String)

    收藏标签字符串: 选填,用空格分隔

  • :comment (String)

    短评文本: 选填,最多350字

  • :privacy (String)

    隐私设置: 选填,值为‘private’为设置成仅自己可见,其他默认为公开

  • :rating (Integer)

    星评: 选填,数字1~5为合法值,其他信息默认为不区分星评

Returns:

  • (Hashie::Mash)

    返回创建的图书收藏信息

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_w



350
351
352
# File 'lib/douban_api/client/book.rb', line 350

def create_book_collection(id, status, options={})
  post "v2/book/#{id}/collection", options.merge(:status => status)
end

#create_book_review(id, options = {}) ⇒ Hashie::Mash

发表新评论

Examples:

给 id为3821057 的图书添加评论

client.create_book_review("3821057", {
  :title  => "我们的萧条就是我们的生活",
  :content => "嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
   嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
   嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻...(省略一些嘻)",
  :rating => 5
})

Parameters:

  • id (String)

    图书的id

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :title (String)

    必传

  • :content (String)

    必传,且多于150字

  • :rating (Integer)

    非必传,数字1~5为合法值,其他信息默认为不打分

Returns:

  • (Hashie::Mash)

    图书评论Review信息

See Also:

是否需要认证:

  • true

需要的scope:

  • douban_basic_common



115
116
117
# File 'lib/douban_api/client/book.rb', line 115

def create_book_review(id, options={})
  post "v2/book/reviews", options.merge(:book => id)
end

#edit_book_annotation(id, options = {}) ⇒ Hashie::Mash

用户修改某篇笔记

TODO 支持图片

Examples:

给id3821057的图书的第10页添加笔记

client.create_book_annotations('3821057',{
  :content =>"据说是现在新浪共享上是有下载的",
  :page => 10
})

Parameters:

  • id (String)

    图书的id

  • options (String) (defaults to: {})

    :content 笔记内容: 必填,需多于15字

Options Hash (options):

  • :page (Integer)

    页码: 页码或章节名选填其一,最多6位正整数

  • :chapter (String)

    章节名: 页码或章节名选填其一,最多100字

  • :privacy (String)

    隐私设 选填: 值为‘private’为设置成仅自己可见,其他默认为公开

Returns:

  • (Hashie::Mash)

    该笔记内容

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_w



241
242
243
# File 'lib/douban_api/client/book.rb', line 241

def edit_book_annotation(id, options={})
  put "v2/book/annotation/#{id}", options
end

#edit_book_review(id, options = {}) ⇒ Hashie::Mash

修改评论

Examples:

修改id为5669920的评论

client.edit_book_review("5669920", {
  :title  => "我们读故事,看电影,不过是因为我们懦弱",
  :content => "嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
   嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
   嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻...(省略一些嘻)",
  :rating => 5
})

Parameters:

  • id (String)

    评论的id

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :title (String)

    必传

  • :content (String)

    必传,且多于150字

  • :rating (Integer)

    非必传,数字1~5为合法值,其他信息默认为不打分

Returns:

  • (Hashie::Mash)

    图书评论Review信息

See Also:

是否需要认证:

  • true

需要的scope:

  • douban_basic_common



137
138
139
# File 'lib/douban_api/client/book.rb', line 137

def edit_book_review(id, options={})
  put "v2/book/reviews/#{id}", options
end

#isbn(id) ⇒ Hashie::Mash

根据isbn获取图书信息

Examples:

获取 ISBN为9787208083950 的图书信息

Douban.isbn('9787208083950')

Parameters:

  • id (String)

    图书的id

Returns:

  • (Hashie::Mash)

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



30
31
32
# File 'lib/douban_api/client/book.rb', line 30

def isbn(id)
  response = get "v2/book/isbn/#{id}"
end

#remove_book_annotation(id) ⇒ Boolean

用户删除某篇笔记

Examples:

删除id为15143201的笔记 (只能删除用户自己的笔记)

client.remove_book_annotation('15143201')

Parameters:

  • id (String)

    笔记的id

Returns:

  • (Boolean)

    删除成功则返回true, 否则false

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_w



254
255
256
257
258
259
260
261
# File 'lib/douban_api/client/book.rb', line 254

def remove_book_annotation(id)
  begin
    delete "v2/book/annotation/#{id}"
    return true
  rescue Douban::NotFound
    return false
  end
end

#remove_book_collection(id) ⇒ Boolean

用户删除某篇笔记

Examples:

删除用户对id为3821057的图书收藏

client.remove_book_collection('3821057')

Parameters:

  • id (String)

    图书的id

Returns:

  • (Boolean)

    删除成功则返回true, 否则false

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_w



388
389
390
391
392
393
394
395
# File 'lib/douban_api/client/book.rb', line 388

def remove_book_collection(id)
  begin
    delete "v2/book/#{id}/collection"
    return true
  rescue Douban::NotFound
    return false
  end
end

#remove_book_review(id) ⇒ Boolean

删除评论

Examples:

删除 id为5669920 图书评论

client.remove_book_review('5669920')

Parameters:

  • id (String)

    图书的id

Returns:

  • (Boolean)

    删除成功则返回true, 否则false

See Also:

是否需要认证:

  • true

需要的scope:

  • douban_basic_common



150
151
152
153
154
155
156
157
# File 'lib/douban_api/client/book.rb', line 150

def remove_book_review(id)
  begin
    delete "v2/book/review/#{id}"
    return true
  rescue Douban::NotFound
    return false
  end
end

#search_books(q, options = {}) ⇒ Hashie::Mash

通过关键字搜索图书

Examples:

搜索 的图书信息

Douban.search_books("搏击俱乐部")

Parameters:

  • q (String)

    查询关键字

Returns:

  • (Hashie::Mash)

    符合查询条件的图书列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



43
44
45
46
# File 'lib/douban_api/client/book.rb', line 43

def search_books(q, options={})
  response = get "v2/book/search", options.merge(:q => q)
  response["books"]
end

#search_books_by_tag(tag, options = {}) ⇒ Array<Hashie::Mash>

通过tag搜索图书

Examples:

搜索tag包含“美国文学”的图书信息

Douban.search_books_by_tag("美国文学")

Parameters:

  • tag (String)

    查询的tag

Returns:

  • (Array<Hashie::Mash>)

    符合查询条件的图书列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



57
58
59
60
# File 'lib/douban_api/client/book.rb', line 57

def search_books_by_tag(tag, options={})
  response = get "v2/book/search", options.merge(:tag => tag)
  response["books"]
end

#user_book(id) ⇒ Hashie::Mash

获取用户对某本图书的收藏信息

Examples:

获取已认证用户的对id为3821057的书的收藏信息

client.user_book('3821057')

Parameters:

  • id (String)

    图书的id

Returns:

  • (Hashie::Mash)

    用户对这本书的收藏信息

See Also:

是否需要认证:

  • true

需要的scope:

  • book_basic_r



293
294
295
# File 'lib/douban_api/client/book.rb', line 293

def user_book(id)
  response = get "v2/book/#{id}/collection"
end

#user_book_annotations(name = nil, options = {}) ⇒ Array<Hashie::Mash>

获取某个用户的所有笔记

Examples:

获取ahbei的所有笔记

Douban.user_book_annotations('ahbei')      

获取已认证用户的所有笔记

client.user_book_annotations()

Parameters:

  • name (String) (defaults to: nil)

    用户uid或者数字id

Returns:

  • (Array<Hashie::Mash>)

    该用户的笔记列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



274
275
276
277
278
279
280
281
282
# File 'lib/douban_api/client/book.rb', line 274

def user_book_annotations(name=nil, options={})
  if name.nil?
    response = get("v2/book/user/#{get_user_id}/annotations", options)
  else
    response = get("v2/book/user/#{name}/annotations", options)
  end
  
  response["annotations"]
end

#user_book_tags(name = nil, options = {}) ⇒ Array<Hashie::Mash>

获取用户对图书的所有标签

Examples:

获取ahbei图书收藏的所有标签

Douban.user_book_tags('ahbei')      

获取已认证用户的图书收藏的所有标签

client.user_book_tags

Parameters:

  • name (String) (defaults to: nil)

    用户uid或者数字id

Returns:

  • (Array<Hashie::Mash>)

    标签列表

See Also:

是否需要认证:

  • false

需要的scope:

  • book_basic_r



87
88
89
90
91
92
93
94
95
# File 'lib/douban_api/client/book.rb', line 87

def user_book_tags(name=nil, options={})
  if name.nil?
    response = get("v2/book/user/#{get_user_id}/tags", options)
  else
    response = get("v2/book/user/#{name}/tags", options)
  end
  
  response["tags"]
end