class Alexandria::BookProviders::ProxisProvider

Constants

BASE_SEARCH_URL
ISBN_REDIRECT_BASE_URL
SITE

Proxis essentially has three book databases, NL, FR and EN. Currently, this provider only searches the NL database, since it adds most to Alexandria (Amazon already has French and English titles).

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/alexandria/book_providers/proxis.rb, line 31
def initialize
  super("Proxis", "Proxis (Belgium)")
  # prefs.add("lang", _("Language"), "fr",
  #          LANGUAGES.keys)
  prefs.read
end

Public Instance Methods

create_search_uri(search_type, search_term) click to toggle source
# File lib/alexandria/book_providers/proxis.rb, line 53
def create_search_uri(search_type, search_term)
  if search_type == SEARCH_BY_ISBN
    BASE_SEARCH_URL % Library.canonicalise_ean(search_term)
  else
    BASE_SEARCH_URL % CGI.escape(search_term)
  end
end
data_for_header(header) click to toggle source
# File lib/alexandria/book_providers/proxis.rb, line 111
def data_for_header(header)
  tr = header.parent
  td = tr.at("td")
  text_of(td) if td
end
get_book_from_search_result(result) click to toggle source
# File lib/alexandria/book_providers/proxis.rb, line 61
def get_book_from_search_result(result)
  log.debug { "Fetching book from #{result[:lookup_url]}" }
  html_data = transport.get_response(URI.parse(result[:lookup_url]))
  parse_result_data(html_data.body)
end
parse_result_data(html) click to toggle source
# File lib/alexandria/book_providers/proxis.rb, line 117
def parse_result_data(html)
  doc = html_to_doc(html)
  book_data = {}
  book_data[:authors] = []
  # TITLE
  if (title_header = doc.search("div.detailBlock h3"))
    header_spans = title_header.first.search("span")
    title = text_of(header_spans.first)
    title = Regexp.last_match[1].strip if title =~ /(.+)-$/
    book_data[:title] = title
  end

  info_headers = doc.search("table.productInfoTable th")

  isbns = []
  unless info_headers.empty?
    info_headers.each do |th|
      isbns << data_for_header(th) if /(ISBN|EAN)/.match?(th.inner_text)
    end
    book_data[:isbn] = Library.canonicalise_ean(isbns.first)
  end

  # book = Book.new(title, ISBN.get(isbns.first))

  unless info_headers.empty?
    info_headers.each do |th|
      header_text = th.inner_text
      case header_text
      when /Type/
        book_data[:binding] = data_for_header(th)
      when /Verschijningsdatum/
        date = data_for_header(th)
        date =~ %r{/(\d{4})}
        book_data[:publish_year] = Regexp.last_match[1].to_i
      when /Auteur/
        book_data[:authors] << data_for_header(th)
      when /Uitgever/
        book_data[:publisher] = data_for_header(th)
      end
    end
  end

  image_url = nil
  if (cover_img = doc.at("img[@id$='imgProduct']"))
    image_url = if cover_img["src"].start_with?("http")
                  cover_img["src"]
                else
                  "#{SITE}/#{cover_img['src']}" # TODO: use html <base>
                end
    image_url = nil if /ProductNoCover/.match?(image_url)
  end

  book = Book.new(book_data[:title], book_data[:authors],
                  book_data[:isbn], book_data[:publisher],
                  book_data[:publish_year], book_data[:binding])
  [book, image_url]
end
parse_search_result_data(html) click to toggle source
# File lib/alexandria/book_providers/proxis.rb, line 89
def parse_search_result_data(html)
  doc = html_to_doc(html)
  book_search_results = []
  items = doc.search("table.searchResult tr")
  items.each do |item|
    result = {}
    title_link = item % "h5 a"
    if title_link
      result[:title] = text_of(title_link)
      result[:lookup_url] = title_link["href"]
      unless result[:lookup_url].start_with?("http")
        result[:lookup_url] = "#{SITE}#{result[:lookup_url]}"
      end
    end
    book_search_results << result
  end
  # require 'pp'
  # pp book_search_results
  # raise :Ruckus
  book_search_results
end
text_of(node) click to toggle source

from Palatina

# File lib/alexandria/book_providers/proxis.rb, line 74
def text_of(node)
  if node.nil?
    nil
  elsif node.text?
    node.to_html
  elsif node.elem?
    if node.children.nil?
      nil
    else
      node_text = node.children.map { |n| text_of(n) }.join
      node_text.strip.squeeze(" ")
    end
  end
end
url(book) click to toggle source
# File lib/alexandria/book_providers/proxis.rb, line 67
def url(book)
  if book.isbn.nil? || book.isbn.empty?
    ISBN_REDIRECT_BASE_URL % Library.canonicalise_ean(book.isbn)
  end
end