Class: Alexandria::BookProviders::ThaliaProvider

Inherits:
WebsiteBasedProvider show all
Includes:
Logging
Defined in:
lib/alexandria/book_providers/thalia_provider.rb

Constant Summary collapse

SITE =
"https://www.thalia.de"
BASE_SEARCH_URL =

type,term

"#{SITE}/shop/bde_bu_hg_startseite/suche/?%s=%s"

Instance Attribute Summary

Attributes inherited from AbstractProvider

#fullname, #name, #prefs

Instance Method Summary collapse

Methods included from Logging

included, #log

Methods inherited from WebsiteBasedProvider

#html_to_doc, #text_of

Methods inherited from AbstractProvider

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

Constructor Details

#initializeThaliaProvider

Returns a new instance of ThaliaProvider.



25
26
27
28
29
# File 'lib/alexandria/book_providers/thalia_provider.rb', line 25

def initialize
  super("Thalia", "Thalia (Germany)")
  # no preferences for the moment
  prefs.read
end

Instance Method Details

#create_search_uri(search_type, search_term) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



81
82
83
84
85
86
87
# 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) ⇒ Object



89
90
91
92
93
# 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) ⇒ Object



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#search(criterion, type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alexandria/book_providers/thalia_provider.rb', line 35

def search(criterion, type)
  req = create_search_uri(type, criterion)
  log.debug { req }
  html_data = transport.get_response(URI.parse(req))
  if type == SEARCH_BY_ISBN
    parse_result_data(html_data.body, criterion)
  else
    results = parse_search_result_data(html_data.body)
    raise NoResultsError if results.empty?

    results.map { |result| get_book_from_search_result(result) }
  end
end

#url(book) ⇒ Object



31
32
33
# File 'lib/alexandria/book_providers/thalia_provider.rb', line 31

def url(book)
  create_search_uri(SEARCH_BY_ISBN, book.isbn)
end