class Alexandria::BookProviders::ThaliaProvider

Constants

BASE_SEARCH_URL
SITE

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/alexandria/book_providers/thalia_provider.rb, line 25
def initialize
  super("Thalia", "Thalia (Germany)")
  # no preferences for the moment
  prefs.read
end

Public Instance Methods

create_search_uri(search_type, search_term) click to toggle source
# File lib/alexandria/book_providers/thalia_provider.rb, line 49
def create_search_uri(search_type, search_term)
  (search_type_code = {
    SEARCH_BY_ISBN    => "sq",
    SEARCH_BY_AUTHORS => "sa", # Autor
    SEARCH_BY_TITLE   => "st", # Titel
    SEARCH_BY_KEYWORD => "ssw" # Schlagwort
  }[search_type]) || ""
  search_type_code = CGI.escape(search_type_code)
  search_term_encoded = if search_type == SEARCH_BY_ISBN
                          # search_term_encoded = search_term.as_isbn_13
                          Library.canonicalise_isbn(search_term) # check this!
                        else
                          CGI.escape(search_term)
                        end
  format(BASE_SEARCH_URL, search_type_code, search_term_encoded)
end
data_from_label(node, label_text) click to toggle source
# File lib/alexandria/book_providers/thalia_provider.rb, line 81
def data_from_label(node, label_text)
  label_node = node % "th[text()*='#{label_text}']"
  return "" unless label_node

  item_node = label_node.parent % "td"
  item_node.inner_text.strip
end
get_book_from_search_result(result) click to toggle source
# File lib/alexandria/book_providers/thalia_provider.rb, line 89
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, "noisbn", recursing: true)
end
parse_result_data(html, isbn, recursing: false) click to toggle source
# File lib/alexandria/book_providers/thalia_provider.rb, line 95
def parse_result_data(html, isbn, recursing: false)
  doc = html_to_doc(html)

  results_divs = doc / "ul.weitere-formate"
  unless results_divs.empty?
    if recursing
      # already recursing, avoid doing so endlessly second time
      # around *should* lead to a book description, not a result
      # list
      return
    end

    # ISBN-lookup results in multiple results
    results = parse_search_result_data(html)
    chosen = results.first # fallback!
    html_data = transport.get_response(URI.parse(chosen[:lookup_url]))
    return parse_result_data(html_data.body, isbn, recursing: true)
  end

  begin
    if (div = doc % "section#sbe-product-details")
      title = div["data-titel"]

      if (author_p = doc % "p.aim-author")
        authors = []
        author_links = author_p / :a
        author_links.each do |a|
          authors << a.inner_text.strip
        end
      end

      item_details = doc % "section.artikeldetails"
      isbns = []
      isbns << data_from_label(item_details, "EAN")
      isbns << data_from_label(item_details, "ISBN")
      isbns.reject!(&:empty?)

      year = nil
      date = data_from_label(item_details, "Erscheinungsdatum")
      year = Regexp.last_match[1].to_i if date =~ /(\d{4})/

      book_binding = data_from_label(item_details, "Einband")

      publisher = data_from_label(item_details, "Verlag")

      book = Book.new(title, authors, isbns.first,
                      publisher, year, book_binding)

      image_url = nil
      if (image = doc % "section.imagesPreview img")
        image_url = image["src"]
      end

      [book, image_url]
    end
  rescue StandardError => ex
    trace = ex.backtrace.join("\n> ")
    log.warn do
      "Failed parsing search results for Thalia " \
      "#{ex.message} #{trace}"
    end
    raise NoResultsError
  end
end
parse_search_result_data(html) click to toggle source
# File lib/alexandria/book_providers/thalia_provider.rb, line 66
def parse_search_result_data(html)
  doc = html_to_doc(html)
  book_search_results = []

  results_items = doc / "ul.weitere-formate li.format"

  results_items.each do |item|
    result = {}
    item_link = item % "a"
    result[:lookup_url] = "#{SITE}#{item_link['href']}"
    book_search_results << result
  end
  book_search_results
end
url(book) click to toggle source
# File lib/alexandria/book_providers/thalia_provider.rb, line 31
def url(book)
  create_search_uri(SEARCH_BY_ISBN, book.isbn)
end