Class: Alexandria::BookProviders::DoubanProvider

Inherits:
GenericProvider show all
Includes:
Logging
Defined in:
lib/alexandria/book_providers/douban.rb

Constant Summary collapse

SITE =
'http://www.douban.com'
BASE_URL =
'http://api.douban.com/book/subjects?q=%s&max-results=5&alt=json'

Instance Attribute Summary

Attributes inherited from AbstractProvider

#fullname, #name, #prefs

Instance Method Summary collapse

Methods included from Logging

included, #log

Methods inherited from AbstractProvider

#<=>, abstract?, #abstract?, #action_name, #enabled, #reinitialize, #remove, #toggle_enabled, #transport, unabstract, #variable_name

Constructor Details

#initializeDoubanProvider

Returns a new instance of DoubanProvider.



40
41
42
43
# File 'lib/alexandria/book_providers/douban.rb', line 40

def initialize
  super('Douban', 'Douban (China)')
  prefs.read
end

Instance Method Details

#parse_search_result(response) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/alexandria/book_providers/douban.rb', line 86

def parse_search_result(response)
  book_search_results = []
  begin
    # dbresult = JSON.parse(response)
    dbresult = YAML.safe_load(json2yaml(response))
    # File.open(",douban.yaml", "wb") {|f| f.write(json2yaml(response)) }
    if dbresult['opensearch:totalResults']['$t'].to_i > 0
      for item in dbresult['entry']
        name = item['title']['$t']
        isbn = nil
        publisher = nil
        pubdate = nil
        binding = nil
        for av in item['db:attribute']
          isbn = av['$t'] if av['@name'] == 'isbn13'
          publisher = av['$t'] if av['@name'] == 'publisher'
          pubdate = av['$t'] if av['@name'] == 'pubdate'
          binding = av['$t'] if av['@name'] == 'binding'
        end
        authors = if item['author']
                    item['author'].map { |a| a['name']['$t'] }
                  else
                    []
                  end
        image_url = nil
        for av in item['link']
          image_url = av['@href'] if av['@rel'] == 'image'
        end
        book = Book.new(name, authors, isbn, publisher, pubdate, binding)
        book_search_results << [book, image_url]
      end
    end
  rescue => ex
    log.warn(ex.backtrace.join('\n'))
  end
  book_search_results
end

#search(criterion, type) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/alexandria/book_providers/douban.rb', line 49

def search(criterion, type)
  keyword = criterion
  request_url = BASE_URL % CGI.escape(keyword)

  search_response = transport.get_response(URI.parse(request_url))

  results = parse_search_result(search_response.body)
  raise NoResultsError if results.empty?

  if type == SEARCH_BY_ISBN
    return results.first
  else
    return results
  end
end

#url(_book) ⇒ Object



45
46
47
# File 'lib/alexandria/book_providers/douban.rb', line 45

def url(_book)
  nil
end