alexandria-book-collection-manager 0.7.2 → 0.7.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +9 -0
- data/.github/workflows/ruby.yml +77 -0
- data/.gitignore +5 -1
- data/.hound.yml +2 -0
- data/.rubocop.yml +87 -37
- data/.rubocop_todo.yml +62 -191
- data/.simplecov +5 -2
- data/CHANGELOG.md +63 -0
- data/Gemfile +4 -3
- data/INSTALL.md +26 -14
- data/README.md +52 -42
- data/Rakefile +93 -109
- data/TODO.md +9 -1
- data/alexandria-book-collection-manager.gemspec +50 -43
- data/bin/alexandria +30 -53
- data/doc/FAQ +2 -6
- data/doc/dependency_decisions.yml +27 -8
- data/lib/alexandria.rb +27 -37
- data/lib/alexandria/about.rb +50 -50
- data/lib/alexandria/book_providers.rb +90 -97
- data/lib/alexandria/book_providers/adlibris.rb +41 -76
- data/lib/alexandria/book_providers/amazon_aws.rb +96 -100
- data/lib/alexandria/book_providers/amazon_ecs_util.rb +295 -322
- data/lib/alexandria/book_providers/barnes_and_noble.rb +48 -45
- data/lib/alexandria/book_providers/douban.rb +26 -42
- data/lib/alexandria/book_providers/proxis.rb +44 -55
- data/lib/alexandria/book_providers/pseudomarc.rb +77 -85
- data/lib/alexandria/book_providers/siciliano.rb +64 -65
- data/lib/alexandria/book_providers/thalia.rb +42 -41
- data/lib/alexandria/book_providers/web.rb +15 -33
- data/lib/alexandria/book_providers/worldcat.rb +70 -97
- data/lib/alexandria/book_providers/z3950.rb +160 -173
- data/lib/alexandria/config.rb +1 -1
- data/lib/alexandria/console.rb +8 -21
- data/lib/alexandria/default_preferences.rb +37 -0
- data/lib/alexandria/execution_queue.rb +15 -13
- data/lib/alexandria/export_format.rb +47 -0
- data/lib/alexandria/export_library.rb +193 -300
- data/lib/alexandria/import_library.rb +108 -141
- data/lib/alexandria/import_library_csv.rb +43 -46
- data/lib/alexandria/library_collection.rb +79 -0
- data/lib/alexandria/library_sort_order.rb +45 -0
- data/lib/alexandria/library_store.rb +233 -0
- data/lib/alexandria/logging.rb +11 -13
- data/lib/alexandria/models/book.rb +13 -20
- data/lib/alexandria/models/library.rb +81 -353
- data/lib/alexandria/net.rb +5 -6
- data/lib/alexandria/preferences.rb +73 -87
- data/lib/alexandria/scanners.rb +2 -2
- data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +20 -18
- data/lib/alexandria/scanners/keyboard.rb +8 -8
- data/lib/alexandria/smart_library.rb +133 -170
- data/lib/alexandria/ui.rb +15 -15
- data/lib/alexandria/ui/about_dialog.rb +49 -0
- data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +119 -136
- data/lib/alexandria/ui/alert_dialog.rb +64 -0
- data/lib/alexandria/ui/bad_isbns_dialog.rb +41 -0
- data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +16 -15
- data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +39 -52
- data/lib/alexandria/ui/book_properties_dialog_base.rb +318 -0
- data/lib/alexandria/ui/builder_base.rb +7 -27
- data/lib/alexandria/ui/calendar_popup.rb +58 -0
- data/lib/alexandria/ui/callbacks.rb +189 -183
- data/lib/alexandria/ui/completion_models.rb +10 -23
- data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
- data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
- data/lib/alexandria/ui/dndable.rb +7 -7
- data/lib/alexandria/ui/error_dialog.rb +25 -0
- data/lib/alexandria/ui/export_dialog.rb +142 -0
- data/lib/alexandria/ui/icons.rb +47 -63
- data/lib/alexandria/ui/iconview.rb +12 -10
- data/lib/alexandria/ui/iconview_tooltips.rb +41 -54
- data/lib/alexandria/ui/import_dialog.rb +157 -0
- data/lib/alexandria/ui/init.rb +21 -33
- data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +36 -0
- data/lib/alexandria/ui/libraries_combo.rb +16 -14
- data/lib/alexandria/ui/listview.rb +73 -87
- data/lib/alexandria/ui/main_app.rb +24 -26
- data/lib/alexandria/ui/misc_dialogs.rb +10 -0
- data/lib/alexandria/ui/multi_drag_treeview.rb +28 -41
- data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +156 -194
- data/lib/alexandria/ui/new_book_dialog_manual.rb +139 -0
- data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
- data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
- data/lib/alexandria/ui/preferences_dialog.rb +313 -0
- data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
- data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
- data/lib/alexandria/ui/really_delete_dialog.rb +53 -0
- data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +56 -68
- data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
- data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
- data/lib/alexandria/ui/smart_library_properties_dialog_base.rb +242 -0
- data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
- data/lib/alexandria/ui/sound.rb +11 -13
- data/lib/alexandria/ui/ui_manager.rb +236 -251
- data/lib/alexandria/undo_manager.rb +1 -0
- data/lib/alexandria/version.rb +4 -19
- data/lib/alexandria/web_themes.rb +22 -21
- data/po/Makefile +2 -2
- data/po/cs.po +993 -880
- data/po/cy.po +957 -874
- data/po/de.po +990 -869
- data/po/el.po +989 -869
- data/po/es.po +985 -865
- data/po/fr.po +986 -870
- data/po/ga.po +907 -823
- data/po/gl.po +981 -865
- data/po/it.po +986 -868
- data/po/ja.po +969 -853
- data/po/mk.po +983 -863
- data/po/nb.po +979 -863
- data/po/nl.po +983 -864
- data/po/pl.po +1017 -974
- data/po/pt.po +988 -861
- data/po/pt_BR.po +984 -868
- data/po/ru.po +992 -873
- data/po/sk.po +987 -869
- data/po/sv.po +977 -861
- data/po/uk.po +975 -865
- data/po/zh_TW.po +976 -860
- data/schemas/alexandria.schemas +25 -3
- data/share/alexandria/glade/acquire_dialog__builder.glade +15 -12
- data/share/alexandria/glade/book_properties_dialog__builder.glade +171 -299
- data/share/alexandria/glade/main_app__builder.glade +24 -33
- data/share/alexandria/glade/new_book_dialog__builder.glade +27 -59
- data/share/alexandria/glade/preferences_dialog__builder.glade +250 -290
- data/share/gnome/help/alexandria/C/introduction.xml +0 -8
- data/share/gnome/help/alexandria/C/searching.xml +1 -1
- data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
- data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
- data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
- data/share/gnome/help/alexandria/ja/introduction.xml +0 -8
- data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
- data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
- data/spec/alexandria/book_providers_spec.rb +75 -171
- data/spec/alexandria/book_spec.rb +12 -10
- data/spec/alexandria/console_spec.rb +27 -0
- data/spec/alexandria/export_library_spec.rb +130 -0
- data/spec/alexandria/library_spec.rb +128 -172
- data/spec/alexandria/library_store_spec.rb +37 -0
- data/spec/alexandria/preferences_spec.rb +44 -17
- data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
- data/spec/alexandria/smart_library_spec.rb +30 -25
- data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/book_properties_dialog_spec.rb +17 -0
- data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/export_dialog_spec.rb +36 -0
- data/spec/alexandria/ui/icons_spec.rb +26 -0
- data/spec/alexandria/ui/iconview_spec.rb +7 -21
- data/spec/alexandria/ui/import_dialog_spec.rb +46 -0
- data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
- data/spec/alexandria/ui/main_app_spec.rb +7 -34
- data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +15 -0
- data/spec/alexandria/ui/new_book_dialog_spec.rb +22 -0
- data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
- data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
- data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
- data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
- data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +49 -0
- data/spec/alexandria/ui/sound_spec.rb +2 -2
- data/spec/alexandria/ui/ui_manager_spec.rb +43 -20
- data/spec/end_to_end/basic_run_spec.rb +52 -0
- data/spec/spec_helper.rb +65 -33
- data/tasks/setup.rb +2 -2
- data/tasks/spec.rake +16 -3
- data/util/rake/fileinstall.rb +39 -35
- data/util/rake/gettextgenerate.rb +7 -7
- data/util/rake/omfgenerate.rb +7 -7
- metadata +178 -45
- data/dogtail/basic_run_test.py +0 -9
- data/lib/alexandria/book_providers/deastore.rb +0 -265
- data/lib/alexandria/book_providers/mcu.rb +0 -182
- data/lib/alexandria/book_providers/renaud.rb +0 -149
- data/lib/alexandria/ui/dialogs/about_dialog.rb +0 -61
- data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -72
- data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +0 -51
- data/lib/alexandria/ui/dialogs/book_properties_dialog_base.rb +0 -426
- data/lib/alexandria/ui/dialogs/export_dialog.rb +0 -171
- data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -196
- data/lib/alexandria/ui/dialogs/misc_dialogs.rb +0 -87
- data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +0 -154
- data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -74
- data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -568
- data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -59
- data/lib/alexandria/ui/dialogs/smart_library_properties_dialog_base.rb +0 -420
- data/spec/alexandria/scanners/cuecat_spec.rb +0 -67
- data/spec/alexandria/ui/dialogs_spec.rb +0 -96
- data/spec/alexandria/ui/sidepane_spec.rb +0 -29
- data/spec/alexandria/ui/ui_utilities_spec.rb +0 -62
- data/spec/alexandria/utilities_spec.rb +0 -52
- data/tasks/dogtail.rake +0 -6
@@ -24,20 +24,20 @@
|
|
24
24
|
# New Tlalia provider, taken from Palatina MetaDataSource and modified
|
25
25
|
# for Alexandria. (21 Dec 2009)
|
26
26
|
|
27
|
-
require
|
28
|
-
require
|
29
|
-
require
|
27
|
+
require "net/http"
|
28
|
+
require "cgi"
|
29
|
+
require "alexandria/book_providers/web"
|
30
30
|
|
31
31
|
module Alexandria
|
32
32
|
class BookProviders
|
33
33
|
class ThaliaProvider < WebsiteBasedProvider
|
34
|
-
include
|
34
|
+
include Logging
|
35
35
|
|
36
|
-
SITE =
|
36
|
+
SITE = "http://www.thalia.de"
|
37
37
|
BASE_SEARCH_URL = "#{SITE}/shop/bde_bu_hg_startseite/suche/?%s=%s" # type,term
|
38
38
|
|
39
39
|
def initialize
|
40
|
-
super(
|
40
|
+
super("Thalia", "Thalia (Germany)")
|
41
41
|
# no preferences for the moment
|
42
42
|
prefs.read
|
43
43
|
end
|
@@ -48,24 +48,25 @@ module Alexandria
|
|
48
48
|
|
49
49
|
def search(criterion, type)
|
50
50
|
req = create_search_uri(type, criterion)
|
51
|
-
|
51
|
+
log.debug { req }
|
52
52
|
html_data = transport.get_response(URI.parse(req))
|
53
53
|
if type == SEARCH_BY_ISBN
|
54
54
|
parse_result_data(html_data.body, criterion)
|
55
55
|
else
|
56
56
|
results = parse_search_result_data(html_data.body)
|
57
57
|
raise NoResultsError if results.empty?
|
58
|
+
|
58
59
|
results.map { |result| get_book_from_search_result(result) }
|
59
60
|
end
|
60
61
|
end
|
61
62
|
|
62
63
|
def create_search_uri(search_type, search_term)
|
63
64
|
(search_type_code = {
|
64
|
-
SEARCH_BY_ISBN
|
65
|
-
SEARCH_BY_AUTHORS =>
|
66
|
-
SEARCH_BY_TITLE
|
67
|
-
SEARCH_BY_KEYWORD =>
|
68
|
-
}[search_type]) ||
|
65
|
+
SEARCH_BY_ISBN => "sq",
|
66
|
+
SEARCH_BY_AUTHORS => "sa", # Autor
|
67
|
+
SEARCH_BY_TITLE => "st", # Titel
|
68
|
+
SEARCH_BY_KEYWORD => "ssw" # Schlagwort
|
69
|
+
}[search_type]) || ""
|
69
70
|
search_type_code = CGI.escape(search_type_code)
|
70
71
|
search_term_encoded = if search_type == SEARCH_BY_ISBN
|
71
72
|
# search_term_encoded = search_term.as_isbn_13
|
@@ -79,12 +80,12 @@ module Alexandria
|
|
79
80
|
def parse_search_result_data(html)
|
80
81
|
doc = html_to_doc(html)
|
81
82
|
book_search_results = []
|
82
|
-
results_divs = doc /
|
83
|
+
results_divs = doc / "div.articlePresentationSearchCH"
|
83
84
|
results_divs.each do |div|
|
84
85
|
result = {}
|
85
|
-
title_link = div %
|
86
|
+
title_link = div % "div.articleText/h2/a"
|
86
87
|
result[:title] = title_link.inner_html
|
87
|
-
result[:lookup_url] = title_link[
|
88
|
+
result[:lookup_url] = title_link["href"]
|
88
89
|
book_search_results << result
|
89
90
|
end
|
90
91
|
book_search_results
|
@@ -93,26 +94,26 @@ module Alexandria
|
|
93
94
|
def data_from_label(node, label_text)
|
94
95
|
label_node = node % "strong[text()*='#{label_text}']"
|
95
96
|
if (item_node = label_node.parent)
|
96
|
-
data =
|
97
|
+
data = ""
|
97
98
|
item_node.children.each do |n|
|
98
99
|
data += n.to_html if n.text?
|
99
100
|
end
|
100
101
|
data.strip
|
101
102
|
else
|
102
|
-
|
103
|
+
""
|
103
104
|
end
|
104
105
|
end
|
105
106
|
|
106
107
|
def get_book_from_search_result(result)
|
107
108
|
log.debug { "Fetching book from #{result[:lookup_url]}" }
|
108
109
|
html_data = transport.get_response(URI.parse(result[:lookup_url]))
|
109
|
-
parse_result_data(html_data.body,
|
110
|
+
parse_result_data(html_data.body, "noisbn", true)
|
110
111
|
end
|
111
112
|
|
112
113
|
def parse_result_data(html, isbn, recursing = false)
|
113
114
|
doc = html_to_doc(html)
|
114
115
|
|
115
|
-
results_divs = doc /
|
116
|
+
results_divs = doc / "div.articlePresentationSearchCH"
|
116
117
|
unless results_divs.empty?
|
117
118
|
if recursing
|
118
119
|
# already recursing, avoid doing so endlessly second time
|
@@ -120,6 +121,7 @@ module Alexandria
|
|
120
121
|
# list
|
121
122
|
return
|
122
123
|
end
|
124
|
+
|
123
125
|
# ISBN-lookup results in multiple results (trying to be
|
124
126
|
# useful, such as for new editions e.g. 9780974514055
|
125
127
|
# "Programming Ruby" )
|
@@ -128,11 +130,10 @@ module Alexandria
|
|
128
130
|
# e.g. .../dave_thomas/ISBN0-9745140-5-5/ID6017044.html
|
129
131
|
chosen = results.first # fallback!
|
130
132
|
results.each do |rslt|
|
131
|
-
if rslt[:lookup_url] =~
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
end
|
133
|
+
if rslt[:lookup_url] =~ %r{/ISBN(\d+[\d-]*)/} &&
|
134
|
+
(Regexp.last_match[1].delete("-") == isbn10)
|
135
|
+
chosen = rslt
|
136
|
+
break
|
136
137
|
end
|
137
138
|
end
|
138
139
|
html_data = transport.get_response(URI.parse(chosen[:lookup_url]))
|
@@ -140,9 +141,9 @@ module Alexandria
|
|
140
141
|
end
|
141
142
|
|
142
143
|
begin
|
143
|
-
if (div = doc %
|
144
|
+
if (div = doc % "div#contentFull")
|
144
145
|
title_img = ((div % :h2) / :img).first
|
145
|
-
title = title_img[
|
146
|
+
title = title_img["alt"]
|
146
147
|
|
147
148
|
# note, the following img also has alt="von Author, Author..."
|
148
149
|
|
@@ -150,7 +151,7 @@ module Alexandria
|
|
150
151
|
authors = []
|
151
152
|
author_links = author_h.parent / :a
|
152
153
|
author_links.each do |a|
|
153
|
-
if a[
|
154
|
+
if a["href"].include? "BUCH/sa"
|
154
155
|
# 'sa' means search author, there may also be 'ssw' (search keyword) links
|
155
156
|
authors << a.inner_text[0..-2].strip
|
156
157
|
# NOTE stripping the little >> character here...
|
@@ -158,36 +159,36 @@ module Alexandria
|
|
158
159
|
end
|
159
160
|
end
|
160
161
|
|
161
|
-
item_details = doc %
|
162
|
+
item_details = doc % "ul.itemDataList"
|
162
163
|
isbns = []
|
163
|
-
isbns << data_from_label(item_details,
|
164
|
-
isbns << data_from_label(item_details,
|
164
|
+
isbns << data_from_label(item_details, "EAN")
|
165
|
+
isbns << data_from_label(item_details, "ISBN")
|
165
166
|
|
166
167
|
year = nil
|
167
|
-
date = data_from_label(item_details,
|
168
|
-
year = Regexp.last_match[1].to_i if date =~ /(
|
168
|
+
date = data_from_label(item_details, "Erschienen:")
|
169
|
+
year = Regexp.last_match[1].to_i if date =~ /(\d{4})/
|
169
170
|
|
170
|
-
binding = data_from_label(item_details,
|
171
|
+
binding = data_from_label(item_details, "Einband")
|
171
172
|
|
172
|
-
publisher = data_from_label(item_details,
|
173
|
+
publisher = data_from_label(item_details, "Erschienen bei:")
|
173
174
|
|
174
175
|
book = Book.new(title, authors, isbns.first,
|
175
176
|
publisher, year, binding)
|
176
177
|
|
177
178
|
image_url = nil
|
178
|
-
if (image_link = doc %
|
179
|
-
image_url = image_link[
|
179
|
+
if (image_link = doc % "a[@id=itemPicStart]")
|
180
|
+
image_url = image_link["href"]
|
180
181
|
end
|
181
182
|
|
182
|
-
|
183
|
+
[book, image_url]
|
183
184
|
|
184
185
|
end
|
185
|
-
rescue => ex
|
186
|
+
rescue StandardError => ex
|
186
187
|
trace = ex.backtrace.join("\n> ")
|
187
|
-
log.warn
|
188
|
-
|
188
|
+
log.warn do
|
189
|
+
"Failed parsing search results for Thalia " \
|
189
190
|
"#{ex.message} #{trace}"
|
190
|
-
|
191
|
+
end
|
191
192
|
raise NoResultsError
|
192
193
|
end
|
193
194
|
end
|
@@ -1,27 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
3
|
+
# This file is part of Alexandria.
|
4
4
|
#
|
5
|
-
#
|
6
|
-
# Copyright (C) 2014 Matijs van Zuijlen
|
7
|
-
#
|
8
|
-
# Alexandria is free software; you can redistribute it and/or
|
9
|
-
# modify it under the terms of the GNU General Public License as
|
10
|
-
# published by the Free Software Foundation; either version 2 of the
|
11
|
-
# License, or (at your option) any later version.
|
12
|
-
#
|
13
|
-
# Alexandria is distributed in the hope that it will be useful,
|
14
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16
|
-
# General Public License for more details.
|
17
|
-
#
|
18
|
-
# You should have received a copy of the GNU General Public
|
19
|
-
# License along with Alexandria; see the file COPYING. If not,
|
20
|
-
# write to the Free Software Foundation, Inc., 51 Franklin Street,
|
21
|
-
# Fifth Floor, Boston, MA 02110-1301 USA.
|
5
|
+
# See the file README.md for authorship and licensing information.
|
22
6
|
|
23
|
-
require
|
24
|
-
require
|
7
|
+
require "hpricot"
|
8
|
+
require "htmlentities"
|
25
9
|
|
26
10
|
module Alexandria
|
27
11
|
class BookProviders
|
@@ -31,9 +15,9 @@ module Alexandria
|
|
31
15
|
@htmlentities = HTMLEntities.new
|
32
16
|
end
|
33
17
|
|
34
|
-
def html_to_doc(html, source_data_charset =
|
18
|
+
def html_to_doc(html, source_data_charset = "ISO-8859-1")
|
35
19
|
html.force_encoding source_data_charset
|
36
|
-
utf8_html = html.encode(
|
20
|
+
utf8_html = html.encode("utf-8")
|
37
21
|
normalized_html = @htmlentities.decode(utf8_html)
|
38
22
|
Hpricot(normalized_html)
|
39
23
|
end
|
@@ -42,19 +26,17 @@ module Alexandria
|
|
42
26
|
def text_of(node)
|
43
27
|
if node.nil?
|
44
28
|
nil
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
node_text.strip.squeeze(' ')
|
54
|
-
end
|
29
|
+
elsif node.text?
|
30
|
+
node.to_html
|
31
|
+
elsif node.elem?
|
32
|
+
if node.children.nil?
|
33
|
+
nil
|
34
|
+
else
|
35
|
+
node_text = node.children.map { |n| text_of(n) }.join
|
36
|
+
node_text.strip.squeeze(" ")
|
55
37
|
end
|
56
|
-
# node.inner_html.strip
|
57
38
|
end
|
39
|
+
# node.inner_html.strip
|
58
40
|
end
|
59
41
|
end
|
60
42
|
end
|
@@ -1,24 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
3
|
+
# This file is part of Alexandria.
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# Alexandria is free software; you can redistribute it and/or
|
9
|
-
# modify it under the terms of the GNU General Public License as
|
10
|
-
# published by the Free Software Foundation; either version 2 of the
|
11
|
-
# License, or (at your option) any later version.
|
12
|
-
#
|
13
|
-
# Alexandria is distributed in the hope that it will be useful,
|
14
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16
|
-
# General Public License for more details.
|
17
|
-
#
|
18
|
-
# You should have received a copy of the GNU General Public
|
19
|
-
# License along with Alexandria; see the file COPYING. If not,
|
20
|
-
# write to the Free Software Foundation, Inc., 51 Franklin Street,
|
21
|
-
# Fifth Floor, Boston, MA 02110-1301 USA.
|
5
|
+
# See the file README.md for authorship and licensing information.
|
6
|
+
# frozen_string_literal: true
|
22
7
|
|
23
8
|
# http://en.wikipedia.org/wiki/WorldCat
|
24
9
|
# See http://www.oclc.org/worldcat/policies/terms/
|
@@ -30,34 +15,27 @@
|
|
30
15
|
# Updated from Palatina, to reflect changes in the worldcat website.
|
31
16
|
# (1 Sep 2009)
|
32
17
|
|
33
|
-
require
|
34
|
-
require
|
35
|
-
require
|
18
|
+
require "cgi"
|
19
|
+
require "alexandria/net"
|
20
|
+
require "alexandria/book_providers/web"
|
36
21
|
|
37
22
|
module Alexandria
|
38
23
|
class BookProviders
|
39
24
|
class WorldCatProvider < WebsiteBasedProvider
|
40
|
-
include
|
25
|
+
include Logging
|
41
26
|
|
42
|
-
SITE =
|
27
|
+
SITE = "https://www.worldcat.org"
|
43
28
|
BASE_SEARCH_URL = "#{SITE}/search?q=%s%s&qt=advanced" # type, term
|
44
29
|
|
45
30
|
def initialize
|
46
|
-
super(
|
47
|
-
# prefs.add("enabled", _("Enabled"), true, [true,false])
|
31
|
+
super("WorldCat", "WorldCat")
|
48
32
|
prefs.read
|
49
33
|
end
|
50
34
|
|
51
35
|
def search(criterion, type)
|
52
|
-
# puts create_search_uri(type, criterion)
|
53
36
|
req = create_search_uri(type, criterion)
|
54
|
-
puts req if $DEBUG
|
55
37
|
html_data = transport.get_response(URI.parse(req))
|
56
|
-
# Note: I tried to use Alexandria::WWWAgent,
|
57
|
-
# but this caused failures here (empty pages...)
|
58
|
-
# find out how the requests differ
|
59
38
|
|
60
|
-
# puts html_data.class
|
61
39
|
if type == SEARCH_BY_ISBN
|
62
40
|
parse_result_data(html_data.body, criterion)
|
63
41
|
else
|
@@ -70,18 +48,15 @@ module Alexandria
|
|
70
48
|
|
71
49
|
def url(book)
|
72
50
|
create_search_uri(SEARCH_BY_ISBN, book.isbn)
|
73
|
-
rescue => ex
|
74
|
-
log.warn { "Cannot create url for book #{book}; #{ex.message}" }
|
75
|
-
nil
|
76
51
|
end
|
77
52
|
|
78
53
|
private
|
79
54
|
|
80
55
|
def create_search_uri(search_type, search_term)
|
81
|
-
(search_type_code = { SEARCH_BY_ISBN
|
82
|
-
SEARCH_BY_AUTHORS =>
|
83
|
-
SEARCH_BY_TITLE
|
84
|
-
SEARCH_BY_KEYWORD =>
|
56
|
+
(search_type_code = { SEARCH_BY_ISBN => "isbn:",
|
57
|
+
SEARCH_BY_AUTHORS => "au:",
|
58
|
+
SEARCH_BY_TITLE => "ti:",
|
59
|
+
SEARCH_BY_KEYWORD => "" }[search_type]) || ""
|
85
60
|
search_type_code = CGI.escape(search_type_code)
|
86
61
|
search_term_encoded = if search_type == SEARCH_BY_ISBN
|
87
62
|
Library.canonicalise_ean(search_term) # isbn-13
|
@@ -98,18 +73,18 @@ module Alexandria
|
|
98
73
|
end
|
99
74
|
|
100
75
|
def parse_search_result_data(html)
|
101
|
-
doc = html_to_doc(html,
|
76
|
+
doc = html_to_doc(html, "UTF-8")
|
102
77
|
book_search_results = []
|
103
78
|
begin
|
104
|
-
result_cells = doc /
|
105
|
-
# puts result_cells.length
|
79
|
+
result_cells = doc / "td.result/div.name/.."
|
106
80
|
result_cells.each do |td|
|
107
|
-
type_icon = (td %
|
108
|
-
next unless type_icon && type_icon[
|
109
|
-
|
81
|
+
type_icon = (td % "div.type/img.icn")
|
82
|
+
next unless type_icon && type_icon["src"].include?("icon-bks")
|
83
|
+
|
84
|
+
name_div = td % "div.name"
|
110
85
|
title = name_div.inner_text
|
111
86
|
anchor = name_div % :a
|
112
|
-
url = anchor[
|
87
|
+
url = anchor["href"] if anchor
|
113
88
|
lookup_url = "#{SITE}#{url}"
|
114
89
|
result = {}
|
115
90
|
result[:title] = title
|
@@ -117,31 +92,31 @@ module Alexandria
|
|
117
92
|
|
118
93
|
book_search_results << result
|
119
94
|
end
|
120
|
-
rescue => ex
|
95
|
+
rescue StandardError => ex
|
121
96
|
trace = ex.backtrace.join("\n> ")
|
122
|
-
log.warn
|
123
|
-
|
97
|
+
log.warn do
|
98
|
+
"Failed parsing search results for WorldCat " \
|
124
99
|
"#{ex.message} #{trace}"
|
125
|
-
|
100
|
+
end
|
126
101
|
end
|
127
102
|
book_search_results
|
128
103
|
end
|
129
104
|
|
130
105
|
def parse_result_data(html, search_isbn = nil, recursing = false)
|
131
|
-
doc = html_to_doc(html,
|
106
|
+
doc = html_to_doc(html, "UTF-8")
|
132
107
|
|
133
108
|
begin
|
134
|
-
if doc %
|
135
|
-
log.debug {
|
109
|
+
if doc % "div#div-results-none"
|
110
|
+
log.debug { "WorldCat reports no results" }
|
136
111
|
raise NoResultsError
|
137
112
|
end
|
138
113
|
|
139
|
-
if doc %
|
114
|
+
if doc % "table.table-results"
|
140
115
|
if recursing
|
141
|
-
log.warn {
|
116
|
+
log.warn { "Infinite loop prevented redirecting through WorldCat" }
|
142
117
|
raise NoResultsError
|
143
118
|
end
|
144
|
-
log.info {
|
119
|
+
log.info { "Found multiple results for lookup: checking each" }
|
145
120
|
search_results = parse_search_result_data(html)
|
146
121
|
book = nil
|
147
122
|
cover_url = nil
|
@@ -153,40 +128,39 @@ module Alexandria
|
|
153
128
|
html2 = rslt2.body
|
154
129
|
|
155
130
|
book, cover_url = parse_result_data(html2, search_isbn, true)
|
156
|
-
first_result = [book, cover_url] if first_result.nil?
|
157
131
|
|
158
132
|
log.debug { "got book #{book}" }
|
159
133
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
log.
|
168
|
-
else
|
169
|
-
# no constraint to match isbn, just return first result
|
134
|
+
return [book, cover_url] unless search_isbn
|
135
|
+
|
136
|
+
first_result = [book, cover_url] if first_result.nil?
|
137
|
+
|
138
|
+
search_isbn_canon = Library.canonicalise_ean(search_isbn)
|
139
|
+
rslt_isbn_canon = Library.canonicalise_ean(book.isbn)
|
140
|
+
if search_isbn_canon == rslt_isbn_canon
|
141
|
+
log.info { "book #{book} is a match" }
|
170
142
|
return [book, cover_url]
|
171
143
|
end
|
144
|
+
log.debug { "not a match, checking next" }
|
172
145
|
end
|
173
146
|
|
174
147
|
# gone through all and no ISBN match, so just return first result
|
175
|
-
log.info
|
148
|
+
log.info do
|
149
|
+
"no more results to check. Returning first result, just an approximation"
|
150
|
+
end
|
176
151
|
return first_result
|
177
|
-
|
178
152
|
end
|
179
153
|
|
180
|
-
title_header = doc %
|
154
|
+
title_header = doc % "h1.title"
|
181
155
|
title = title_header.inner_text if title_header
|
182
156
|
unless title
|
183
|
-
log.warn {
|
157
|
+
log.warn { "Unexpected lack of title from WorldCat lookup" }
|
184
158
|
raise NoResultsError
|
185
159
|
end
|
186
160
|
log.info { "Found book #{title} at WorldCat" }
|
187
161
|
|
188
162
|
authors = []
|
189
|
-
authors_tr = doc %
|
163
|
+
authors_tr = doc % "tr#details-allauthors"
|
190
164
|
if authors_tr
|
191
165
|
(authors_tr / :a).each do |a|
|
192
166
|
authors << a.inner_text
|
@@ -194,17 +168,17 @@ module Alexandria
|
|
194
168
|
end
|
195
169
|
|
196
170
|
# can we do better? get the City name?? or multiple publishers?
|
197
|
-
bibdata = doc %
|
171
|
+
bibdata = doc % "div#bibdata"
|
198
172
|
bibdata_table = bibdata % :table
|
199
|
-
publisher_row = bibdata_table %
|
173
|
+
publisher_row = bibdata_table % "th[text()*=Publisher]/.."
|
200
174
|
|
201
175
|
if publisher_row
|
202
|
-
publication_info = (publisher_row /
|
176
|
+
publication_info = (publisher_row / "td").last.inner_text
|
203
177
|
|
204
|
-
publication_info =~ if publication_info.index(
|
205
|
-
|
206
|
-
elsif publication_info.index(
|
207
|
-
|
178
|
+
publication_info =~ if publication_info.index(";")
|
179
|
+
/;\s*([^\d]+)\s*\d*/
|
180
|
+
elsif publication_info.index(":")
|
181
|
+
/:\s*([^;:,]+)/
|
208
182
|
else
|
209
183
|
/([^;,]+)/
|
210
184
|
end
|
@@ -217,34 +191,33 @@ module Alexandria
|
|
217
191
|
year = nil
|
218
192
|
end
|
219
193
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
log.warn { 'No ISBN found on page' }
|
228
|
-
end
|
194
|
+
isbn_row = doc % "tr#details-standardno"
|
195
|
+
if isbn_row
|
196
|
+
isbns = (isbn_row / "td").last.inner_text.split
|
197
|
+
isbn = Library.canonicalise_isbn(isbns.first)
|
198
|
+
else
|
199
|
+
log.warn { "No ISBN found on page" }
|
200
|
+
isbn = search_isbn
|
229
201
|
end
|
230
202
|
|
231
|
-
|
203
|
+
book_binding = "" # not given on WorldCat website (as far as I can tell)
|
232
204
|
|
233
|
-
book = Book.new(title, authors, isbn, publisher, year,
|
205
|
+
book = Book.new(title, authors, isbn, publisher, year, book_binding)
|
234
206
|
|
235
207
|
image_url = nil # hm, it's on the website, but uses JavaScript...
|
236
208
|
|
237
|
-
|
238
|
-
rescue => ex
|
209
|
+
[book, image_url]
|
210
|
+
rescue StandardError => ex
|
239
211
|
raise ex if ex.instance_of? NoResultsError
|
212
|
+
|
240
213
|
trace = ex.backtrace.join("\n> ")
|
241
|
-
log.warn
|
242
|
-
|
214
|
+
log.warn do
|
215
|
+
"Failed parsing search results for WorldCat " \
|
243
216
|
"#{ex.message} #{trace}"
|
244
|
-
|
217
|
+
end
|
245
218
|
raise NoResultsError
|
246
219
|
end
|
247
220
|
end
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|