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
@@ -1,95 +1,81 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
4
|
-
# Copyright (C) 2008 Cathal Mc Ginley
|
5
|
-
# Copyright (C) 2014, 2016 Matijs van Zuijlen
|
3
|
+
# This file is part of Alexandria.
|
6
4
|
#
|
7
|
-
#
|
8
|
-
# modify it under the terms of the GNU General Public License as
|
9
|
-
# published by the Free Software Foundation; either version 2 of the
|
10
|
-
# License, or (at your option) any later version.
|
11
|
-
#
|
12
|
-
# Alexandria is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15
|
-
# General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU General Public
|
18
|
-
# License along with Alexandria; see the file COPYING. If not,
|
19
|
-
# write to the Free Software Foundation, Inc., 51 Franklin Street,
|
20
|
-
# Fifth Floor, Boston, MA 02110-1301 USA.
|
5
|
+
# See the file README.md for authorship and licensing information.
|
21
6
|
|
22
7
|
# http://en.wikipedia.org/wiki/Amazon
|
23
8
|
|
24
|
-
require
|
25
|
-
require
|
9
|
+
require "hpricot"
|
10
|
+
require "alexandria/book_providers/amazon_ecs_util"
|
26
11
|
|
27
12
|
module Alexandria
|
28
13
|
class BookProviders
|
29
14
|
class AmazonProvider < GenericProvider
|
30
15
|
include Logging
|
31
16
|
include GetText
|
32
|
-
GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset:
|
17
|
+
GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8")
|
33
18
|
|
34
19
|
# CACHE_DIR = File.join(Alexandria::Library::DIR, '.amazon_cache')
|
35
20
|
|
36
|
-
LOCALES = [
|
21
|
+
LOCALES = ["ca", "de", "fr", "jp", "uk", "us"].freeze
|
37
22
|
|
38
23
|
def initialize
|
39
|
-
super(
|
24
|
+
super("Amazon", "Amazon")
|
40
25
|
# prefs.add("enabled", _("Enabled"), true, [true,false])
|
41
|
-
prefs.add(
|
42
|
-
prefs.add(
|
43
|
-
prefs.add(
|
44
|
-
prefs.add(
|
26
|
+
prefs.add("locale", _("Locale"), "us", AmazonProvider::LOCALES)
|
27
|
+
prefs.add("dev_token", _("Access key ID"), "")
|
28
|
+
prefs.add("secret_key", _("Secret access key"), "")
|
29
|
+
prefs.add("associate_tag", _("Associate Tag"), "")
|
45
30
|
|
46
31
|
prefs.read
|
47
|
-
token = prefs.variable_named(
|
32
|
+
token = prefs.variable_named("dev_token")
|
48
33
|
# kill old (shorter) tokens, or previously distributed Access Key Id (see #26250)
|
49
34
|
|
50
35
|
if token
|
51
36
|
token.new_value = token.value.strip if token.value != token.value.strip
|
37
|
+
if (token.value.size != 20) || (token.value == "0J356Z09CN88KB743582")
|
38
|
+
token.new_value = ""
|
39
|
+
end
|
52
40
|
end
|
53
|
-
token.new_value = '' if token && ((token.value.size != 20) || (token.value == '0J356Z09CN88KB743582'))
|
54
41
|
|
55
|
-
secret = prefs.variable_named(
|
56
|
-
if secret
|
57
|
-
secret.new_value = secret.value.strip
|
42
|
+
secret = prefs.variable_named("secret_key")
|
43
|
+
if secret && (secret.value != secret.value.strip)
|
44
|
+
secret.new_value = secret.value.strip
|
58
45
|
end
|
59
46
|
|
60
|
-
associate = prefs.variable_named(
|
61
|
-
|
62
|
-
|
63
|
-
|
47
|
+
associate = prefs.variable_named("associate_tag") or return
|
48
|
+
|
49
|
+
associate.new_value = "rubyalexa-20" if associate.value.strip.empty?
|
50
|
+
if associate.value != associate.value.strip
|
51
|
+
associate.new_value = associate.value.strip
|
64
52
|
end
|
65
53
|
end
|
66
54
|
|
67
55
|
def search(criterion, type)
|
68
56
|
prefs.read
|
69
57
|
|
70
|
-
if prefs[
|
71
|
-
raise(Amazon::RequestError,
|
72
|
-
'Secret Access Key required for Authentication:' \
|
73
|
-
' you must sign up for your own Amazon AWS account')
|
58
|
+
if prefs["secret_key"].empty?
|
59
|
+
raise(Amazon::RequestError, _("Provide secret key for your Amazon AWS account."))
|
74
60
|
end
|
75
61
|
|
76
62
|
if (config = Alexandria::Preferences.instance.http_proxy_config)
|
77
63
|
host, port, user, pass = config
|
78
|
-
url =
|
79
|
-
url += user +
|
80
|
-
url += host +
|
81
|
-
ENV[
|
64
|
+
url = "http://"
|
65
|
+
url += user + ":" + pass + "@" if user && pass
|
66
|
+
url += host + ":" + port.to_s
|
67
|
+
ENV["http_proxy"] = url
|
82
68
|
end
|
83
69
|
|
84
|
-
access_key_id = prefs[
|
70
|
+
access_key_id = prefs["dev_token"]
|
85
71
|
|
86
72
|
Amazon::Ecs.options = { aWS_access_key_id: access_key_id,
|
87
|
-
associateTag: prefs[
|
88
|
-
Amazon::Ecs.secret_access_key = prefs[
|
73
|
+
associateTag: prefs["associate_tag"] }
|
74
|
+
Amazon::Ecs.secret_access_key = prefs["secret_key"]
|
89
75
|
# #req.cache = Amazon::Search::Cache.new(CACHE_DIR)
|
90
76
|
locales = AmazonProvider::LOCALES.dup
|
91
|
-
locales.delete prefs[
|
92
|
-
locales.unshift prefs[
|
77
|
+
locales.delete prefs["locale"]
|
78
|
+
locales.unshift prefs["locale"]
|
93
79
|
locales.reverse!
|
94
80
|
|
95
81
|
begin
|
@@ -99,8 +85,10 @@ module Alexandria
|
|
99
85
|
when SEARCH_BY_ISBN
|
100
86
|
criterion = Library.canonicalise_isbn(criterion)
|
101
87
|
# This isn't ideal : I'd like to do an ISBN/EAN-specific search
|
102
|
-
res = Amazon::Ecs.item_search(criterion,
|
103
|
-
|
88
|
+
res = Amazon::Ecs.item_search(criterion,
|
89
|
+
response_group: "ItemAttributes,Images",
|
90
|
+
country: request_locale)
|
91
|
+
|
104
92
|
res.items.each do |item|
|
105
93
|
products << item
|
106
94
|
end
|
@@ -120,27 +108,27 @@ module Alexandria
|
|
120
108
|
# result with different ISBNs
|
121
109
|
|
122
110
|
if products.length > 1
|
123
|
-
log.warn
|
111
|
+
log.warn do
|
124
112
|
"ISBN search at Amazon[#{request_locale}] got #{products.length} results;" \
|
125
|
-
|
126
|
-
|
113
|
+
" returning the first result only"
|
114
|
+
end
|
127
115
|
end
|
128
116
|
|
129
117
|
when SEARCH_BY_TITLE
|
130
118
|
res = Amazon::Ecs.item_search(criterion,
|
131
|
-
response_group:
|
119
|
+
response_group: "ItemAttributes,Images",
|
132
120
|
country: request_locale)
|
133
121
|
|
134
122
|
res.items.each do |item|
|
135
|
-
products << item if item.get(
|
123
|
+
products << item if /#{criterion}/i.match?(item.get("itemattributes/title"))
|
136
124
|
end
|
137
125
|
# #req.keyword_search(criterion) do |product|
|
138
126
|
|
139
127
|
when SEARCH_BY_AUTHORS
|
140
128
|
criterion = "author:#{criterion}"
|
141
129
|
res = Amazon::Ecs.item_search(criterion,
|
142
|
-
response_group:
|
143
|
-
country: request_locale, type:
|
130
|
+
response_group: "ItemAttributes,Images",
|
131
|
+
country: request_locale, type: "Power")
|
144
132
|
res.items.each do |item|
|
145
133
|
products << item
|
146
134
|
end
|
@@ -148,7 +136,7 @@ module Alexandria
|
|
148
136
|
|
149
137
|
when SEARCH_BY_KEYWORD
|
150
138
|
res = Amazon::Ecs.item_search(criterion,
|
151
|
-
response_group:
|
139
|
+
response_group: "ItemAttributes,Images",
|
152
140
|
country: request_locale)
|
153
141
|
|
154
142
|
res.items.each do |item|
|
@@ -158,86 +146,94 @@ module Alexandria
|
|
158
146
|
else
|
159
147
|
raise InvalidSearchTypeError
|
160
148
|
end
|
161
|
-
raise Amazon::RequestError,
|
149
|
+
raise Amazon::RequestError, _("No products") if products.empty?
|
162
150
|
# raise NoResultsError if products.empty?
|
163
|
-
rescue Amazon::RequestError =>
|
164
|
-
log.debug { "Got Amazon::RequestError at #{request_locale}: #{
|
151
|
+
rescue Amazon::RequestError => ex
|
152
|
+
log.debug { "Got Amazon::RequestError at #{request_locale}: #{ex}" }
|
165
153
|
retry unless locales.empty?
|
166
154
|
raise NoResultsError
|
167
155
|
end
|
168
156
|
|
169
157
|
results = []
|
170
158
|
products.each do |item|
|
171
|
-
next unless item.get(
|
172
|
-
atts = item.search_and_convert('itemattributes')
|
173
|
-
title = normalize(atts.get('title'))
|
159
|
+
next unless item.get("itemattributes/productgroup") == "Book"
|
174
160
|
|
175
|
-
|
176
|
-
|
161
|
+
atts = item.search_and_convert("itemattributes")
|
162
|
+
title = normalize(atts.get("title"))
|
177
163
|
|
178
|
-
|
164
|
+
media = normalize(atts.get("binding"))
|
165
|
+
media = nil if media == "Unknown Binding"
|
166
|
+
|
167
|
+
isbn = normalize(atts.get("isbn"))
|
179
168
|
isbn = (Library.canonicalise_ean(isbn) if isbn && Library.valid_isbn?(isbn))
|
180
169
|
# hack, extract year by regexp (not Y10K compatible :-)
|
181
|
-
/([1-9][0-9]{3})/ =~ atts.get(
|
170
|
+
/([1-9][0-9]{3})/ =~ atts.get("publicationdate")
|
182
171
|
publishing_year = Regexp.last_match[1] ? Regexp.last_match[1].to_i : nil
|
183
172
|
book = Book.new(title,
|
184
|
-
atts.get_array(
|
173
|
+
atts.get_array("author").map { |x| normalize(x) },
|
185
174
|
isbn,
|
186
|
-
normalize(atts.get(
|
175
|
+
normalize(atts.get("manufacturer")),
|
187
176
|
publishing_year,
|
188
177
|
media)
|
189
178
|
|
190
|
-
image_url = item.get(
|
179
|
+
image_url = item.get("mediumimage/url")
|
191
180
|
log.info { "Found at Amazon[#{request_locale}]: #{book.title}" }
|
192
181
|
results << [book, image_url]
|
193
182
|
end
|
194
183
|
if type == SEARCH_BY_ISBN
|
195
184
|
if results.size == 1
|
196
|
-
|
185
|
+
results.first
|
197
186
|
else
|
198
|
-
|
199
|
-
query_isbn_canon = Library.canonicalise_ean(criterion)
|
200
|
-
results.each do |rslt|
|
201
|
-
book = rslt[0]
|
202
|
-
book_isbn_canon = Library.canonicalise_ean(book.isbn)
|
203
|
-
return rslt if query_isbn_canon == book_isbn_canon
|
204
|
-
log.debug { "rejected possible result #{book}" }
|
205
|
-
end
|
206
|
-
# gone through all and no ISBN match, so just return first result
|
207
|
-
log.info { 'no more results to check. Returning first result, just an approximation' }
|
208
|
-
return results.first
|
187
|
+
exact_match_or_first(criterion, results)
|
209
188
|
end
|
210
189
|
else
|
211
|
-
|
190
|
+
results
|
212
191
|
end
|
213
192
|
end
|
214
193
|
|
194
|
+
LOCALE_URLS = {
|
195
|
+
"fr" => "http://www.amazon.fr/exec/obidos/ASIN/%s",
|
196
|
+
"uk" => "http://www.amazon.co.uk/exec/obidos/ASIN/%s",
|
197
|
+
"de" => "http://www.amazon.de/exec/obidos/ASIN/%s",
|
198
|
+
"ca" => "http://www.amazon.ca/exec/obidos/ASIN/%s",
|
199
|
+
"jp" => "http://www.amazon.jp/exec/obidos/ASIN/%s",
|
200
|
+
"us" => "http://www.amazon.com/exec/obidos/ASIN/%s"
|
201
|
+
}.freeze
|
202
|
+
|
215
203
|
def url(book)
|
216
204
|
isbn = Library.canonicalise_isbn(book.isbn)
|
217
|
-
url =
|
218
|
-
when 'fr'
|
219
|
-
'http://www.amazon.fr/exec/obidos/ASIN/%s'
|
220
|
-
when 'uk'
|
221
|
-
'http://www.amazon.co.uk/exec/obidos/ASIN/%s'
|
222
|
-
when 'de'
|
223
|
-
'http://www.amazon.de/exec/obidos/ASIN/%s'
|
224
|
-
when 'ca'
|
225
|
-
'http://www.amazon.ca/exec/obidos/ASIN/%s'
|
226
|
-
when 'jp'
|
227
|
-
'http://www.amazon.jp/exec/obidos/ASIN/%s'
|
228
|
-
when 'us'
|
229
|
-
'http://www.amazon.com/exec/obidos/ASIN/%s'
|
230
|
-
end
|
205
|
+
url = LOCALE_URLS.fetch(prefs["locale"])
|
231
206
|
url % isbn
|
232
|
-
rescue => ex
|
207
|
+
rescue StandardError => ex
|
233
208
|
log.warn { "Cannot create url for book #{book}; #{ex.message}" }
|
234
209
|
nil
|
235
210
|
end
|
236
211
|
|
237
212
|
def normalize(str)
|
238
|
-
str = str.squeeze(
|
213
|
+
str = str.squeeze(" ").strip unless str.nil?
|
239
214
|
str
|
240
215
|
end
|
216
|
+
|
217
|
+
private
|
218
|
+
|
219
|
+
def exact_match_or_first(criterion, results)
|
220
|
+
log.info { "Found multiple results for lookup: checking for exact isbn match" }
|
221
|
+
query_isbn_canon = Library.canonicalise_ean(criterion)
|
222
|
+
exact_match = results.find do |book, _|
|
223
|
+
book_isbn_canon = Library.canonicalise_ean(book.isbn)
|
224
|
+
query_isbn_canon == book_isbn_canon
|
225
|
+
end
|
226
|
+
|
227
|
+
if exact_match
|
228
|
+
# gone through all and no ISBN match, so just return first result
|
229
|
+
log.info do
|
230
|
+
"no more results to check. Returning first result, just an approximation"
|
231
|
+
end
|
232
|
+
exact_match
|
233
|
+
else
|
234
|
+
results.first
|
235
|
+
end
|
236
|
+
end
|
241
237
|
end
|
242
238
|
end
|
243
239
|
end
|
@@ -1,389 +1,362 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
# Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
|
3
|
+
# This file is part of Alexandria.
|
5
4
|
#
|
6
|
-
#
|
7
|
-
# a copy of this software and associated documentation files (the
|
8
|
-
# "Software"), to deal in the Software without restriction, including
|
9
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
-
# the following conditions:
|
13
|
-
#
|
14
|
-
# The above copyright notice and this permission notice shall be
|
15
|
-
# included in all copies or substantial portions of the Software.
|
16
|
-
#
|
17
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
20
|
-
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
21
|
-
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
22
|
-
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
23
|
-
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
-
#++
|
25
|
-
|
26
|
-
# Modified by Cathal Mc Ginley 2008-02-18
|
27
|
-
# added Amazon::Ecs.transport - to enable Alexandria's proxy support
|
28
|
-
# Modified by Cathal Mc Ginley 2008-08-26
|
29
|
-
# Amazon::Element.get now uses inner_text, not inner_html, fixing #21659
|
30
|
-
# Modified by Cathal Mc Ginley 2009-08-13
|
31
|
-
# Added sign_request and hmac_sha256 methods for Authentication support
|
32
|
-
|
33
|
-
require 'net/http'
|
34
|
-
require 'hpricot'
|
35
|
-
require 'cgi'
|
36
|
-
|
37
|
-
require 'digest/sha2'
|
38
|
-
|
39
|
-
module Amazon
|
40
|
-
class RequestError < StandardError; end
|
41
|
-
|
42
|
-
class Ecs
|
43
|
-
include Alexandria::Logging
|
44
|
-
|
45
|
-
SERVICE_URLS = { us: 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
|
46
|
-
uk: 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
|
47
|
-
ca: 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
|
48
|
-
de: 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
|
49
|
-
jp: 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
|
50
|
-
fr: 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService' }.freeze
|
51
|
-
|
52
|
-
@@options = {}
|
53
|
-
@@debug = false
|
54
|
-
|
55
|
-
@@secret_access_key = ''
|
56
|
-
|
57
|
-
# Default search options
|
58
|
-
def self.options
|
59
|
-
@@options
|
60
|
-
end
|
5
|
+
# See the file README.md for authorship and licensing information.
|
61
6
|
|
62
|
-
|
63
|
-
|
64
|
-
|
7
|
+
require "net/http"
|
8
|
+
require "hpricot"
|
9
|
+
require "cgi"
|
65
10
|
|
66
|
-
|
67
|
-
def self.options=(opts)
|
68
|
-
@@options = opts
|
69
|
-
end
|
11
|
+
require "digest/sha2"
|
70
12
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
end
|
13
|
+
module Alexandria
|
14
|
+
module Amazon
|
15
|
+
class RequestError < StandardError; end
|
75
16
|
|
76
|
-
|
77
|
-
|
78
|
-
@@debug = dbg
|
79
|
-
end
|
17
|
+
class Ecs
|
18
|
+
include Logging
|
80
19
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
20
|
+
SERVICE_URLS = {
|
21
|
+
us: "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService",
|
22
|
+
uk: "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService",
|
23
|
+
ca: "http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService",
|
24
|
+
de: "http://webservices.amazon.de/onca/xml?Service=AWSECommerceService",
|
25
|
+
jp: "http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService",
|
26
|
+
fr: "http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService"
|
27
|
+
}.freeze
|
85
28
|
|
86
|
-
|
87
|
-
|
88
|
-
def self.item_search(terms, opts = {})
|
89
|
-
opts[:operation] = 'ItemSearch'
|
90
|
-
opts[:search_index] = opts[:search_index] || 'Books'
|
91
|
-
|
92
|
-
type = opts.delete(:type)
|
93
|
-
if type
|
94
|
-
opts[type.to_sym] = terms
|
95
|
-
else
|
96
|
-
opts[:keywords] = terms
|
97
|
-
end
|
29
|
+
@@options = {}
|
30
|
+
@@debug = false
|
98
31
|
|
99
|
-
|
100
|
-
end
|
32
|
+
@@secret_access_key = ""
|
101
33
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
34
|
+
# Default search options
|
35
|
+
def self.options
|
36
|
+
@@options
|
37
|
+
end
|
106
38
|
|
107
|
-
|
108
|
-
|
39
|
+
def self.secret_access_key=(key)
|
40
|
+
@@secret_access_key = key
|
41
|
+
end
|
109
42
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
43
|
+
# Set default search options
|
44
|
+
def self.options=(opts)
|
45
|
+
@@options = opts
|
46
|
+
end
|
115
47
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
log.debug { "Request URL: #{request_url}" }
|
48
|
+
# Get debug flag.
|
49
|
+
def self.debug
|
50
|
+
@@debug
|
51
|
+
end
|
121
52
|
|
122
|
-
|
123
|
-
|
124
|
-
|
53
|
+
# Set debug flag to true or false.
|
54
|
+
def self.debug=(dbg)
|
55
|
+
@@debug = dbg
|
125
56
|
end
|
126
|
-
Response.new(res.body)
|
127
|
-
end
|
128
57
|
|
129
|
-
|
130
|
-
|
131
|
-
# XML input is in string format
|
132
|
-
def initialize(xml)
|
133
|
-
@doc = Hpricot(xml)
|
58
|
+
def self.configure(&_proc)
|
59
|
+
yield @@options
|
134
60
|
end
|
135
61
|
|
136
|
-
#
|
137
|
-
|
62
|
+
# Search amazon items with search terms. Default search index option is 'Books'.
|
63
|
+
# For other search type other than keywords, please specify
|
64
|
+
# :type => [search type param name].
|
65
|
+
def self.item_search(terms, opts = {})
|
66
|
+
opts[:operation] = "ItemSearch"
|
67
|
+
opts[:search_index] = opts[:search_index] || "Books"
|
68
|
+
|
69
|
+
type = opts.delete(:type)
|
70
|
+
if type
|
71
|
+
opts[type.to_sym] = terms
|
72
|
+
else
|
73
|
+
opts[:keywords] = terms
|
74
|
+
end
|
138
75
|
|
139
|
-
|
140
|
-
def is_valid_request?
|
141
|
-
(@doc / 'isvalid').inner_html == 'True'
|
76
|
+
send_request(opts)
|
142
77
|
end
|
143
78
|
|
144
|
-
#
|
145
|
-
def
|
146
|
-
|
147
|
-
|
79
|
+
# Search an item by ASIN no.
|
80
|
+
def self.item_lookup(item_id, opts = {})
|
81
|
+
opts[:operation] = "ItemLookup"
|
82
|
+
opts[:item_id] = item_id
|
148
83
|
|
149
|
-
|
150
|
-
def error
|
151
|
-
Element.get(@doc, 'error/message')
|
84
|
+
send_request(opts)
|
152
85
|
end
|
153
86
|
|
154
|
-
#
|
155
|
-
def
|
156
|
-
|
157
|
-
|
87
|
+
# HACK : copied from book_providers.rb
|
88
|
+
def self.transport
|
89
|
+
config = Alexandria::Preferences.instance.http_proxy_config
|
90
|
+
config ? Net::HTTP.Proxy(*config) : Net::HTTP
|
158
91
|
end
|
159
92
|
|
160
|
-
#
|
161
|
-
|
162
|
-
|
163
|
-
|
93
|
+
# Generic send request to ECS REST service. You have to specify the
|
94
|
+
# :operation parameter.
|
95
|
+
def self.send_request(opts)
|
96
|
+
opts = options.merge(opts) if options
|
97
|
+
request_url = prepare_url(opts)
|
98
|
+
log.debug { "Request URL: #{request_url}" }
|
99
|
+
|
100
|
+
res = transport.get_response(URI.parse(request_url))
|
101
|
+
unless res.is_a? Net::HTTPSuccess
|
102
|
+
raise Amazon::RequestError, format(_("HTTP Response: %<code>s %<message>s"),
|
103
|
+
code: res.code, message: res.message)
|
104
|
+
end
|
164
105
|
|
165
|
-
|
166
|
-
def item_page
|
167
|
-
@item_page ||= (@doc / 'itemsearchrequest/itempage').inner_html.to_i
|
168
|
-
@item_page
|
106
|
+
Response.new(res.body)
|
169
107
|
end
|
170
108
|
|
171
|
-
#
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
109
|
+
# Response object returned after a REST call to Amazon service.
|
110
|
+
class Response
|
111
|
+
# XML input is in string format
|
112
|
+
def initialize(xml)
|
113
|
+
@doc = Hpricot(xml)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Return Hpricot object.
|
117
|
+
attr_reader :doc
|
118
|
+
|
119
|
+
# Return true if request is valid.
|
120
|
+
def is_valid_request?
|
121
|
+
(@doc / "isvalid").inner_html == "True"
|
122
|
+
end
|
123
|
+
|
124
|
+
# Return true if response has an error.
|
125
|
+
def has_error?
|
126
|
+
!(error.nil? || error.empty?)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Return error message.
|
130
|
+
def error
|
131
|
+
Element.get(@doc, "error/message")
|
132
|
+
end
|
176
133
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
134
|
+
# Return an array of Amazon::Element item objects.
|
135
|
+
def items
|
136
|
+
@items ||= (@doc / "item").map { |item| Element.new(item) }
|
137
|
+
@items
|
138
|
+
end
|
139
|
+
|
140
|
+
# Return the first item (Amazon::Element)
|
141
|
+
def first_item
|
142
|
+
items.first
|
143
|
+
end
|
144
|
+
|
145
|
+
# Return current page no if :item_page option is when initiating the request.
|
146
|
+
def item_page
|
147
|
+
@item_page ||= (@doc / "itemsearchrequest/itempage").inner_html.to_i
|
148
|
+
@item_page
|
149
|
+
end
|
150
|
+
|
151
|
+
# Return total results.
|
152
|
+
def total_results
|
153
|
+
@total_results ||= (@doc / "totalresults").inner_html.to_i
|
154
|
+
@total_results
|
155
|
+
end
|
156
|
+
|
157
|
+
# Return total pages.
|
158
|
+
def total_pages
|
159
|
+
@total_pages ||= (@doc / "totalpages").inner_html.to_i
|
160
|
+
@total_pages
|
161
|
+
end
|
181
162
|
end
|
182
|
-
end
|
183
163
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
# puts s
|
193
|
-
# end
|
194
|
-
# end
|
195
|
-
|
196
|
-
def self.prepare_url(opts)
|
197
|
-
country = opts.delete(:country)
|
198
|
-
country = country.nil? ? 'us' : country
|
199
|
-
request_url = SERVICE_URLS[country.to_sym]
|
200
|
-
raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
|
201
|
-
|
202
|
-
qs = ''
|
203
|
-
opts.each { |k, v|
|
204
|
-
next unless v
|
205
|
-
v = v.join(',') if v.is_a? Array
|
206
|
-
qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
|
207
|
-
}
|
208
|
-
url = "#{request_url}#{qs}"
|
209
|
-
# puts ">>> base url >> #{url}"
|
210
|
-
signed_url = sign_request(url)
|
211
|
-
# puts ">>> SIGNED >> #{signed_url}"
|
212
|
-
signed_url
|
213
|
-
end
|
164
|
+
def self.prepare_url(opts)
|
165
|
+
country = opts.delete(:country)
|
166
|
+
country = country.nil? ? "us" : country
|
167
|
+
request_url = SERVICE_URLS[country.to_sym]
|
168
|
+
unless request_url
|
169
|
+
raise Amazon::RequestError,
|
170
|
+
format(_("Invalid country '%<country>s'"), country: country)
|
171
|
+
end
|
214
172
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
|
219
|
-
end
|
173
|
+
qs = ""
|
174
|
+
opts.each do |k, v|
|
175
|
+
next unless v
|
220
176
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
d = Digest::SHA256.new
|
227
|
-
key = d.digest(key)
|
177
|
+
v = v.join(",") if v.is_a? Array
|
178
|
+
qs << "&#{camelize(k.to_s)}=#{CGI.escape(v.to_s)}"
|
179
|
+
end
|
180
|
+
url = "#{request_url}#{qs}"
|
181
|
+
sign_request(url)
|
228
182
|
end
|
229
183
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
opad_xor = ''
|
235
|
-
for i in 0..key.size - 1
|
236
|
-
ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
|
237
|
-
opad_xor << (opad_bytes[i] ^ key_bytes[i])
|
184
|
+
def self.camelize(string)
|
185
|
+
string.to_s
|
186
|
+
.gsub(%r{/(.?)}) { "::" + Regexp.last_match[1].upcase }
|
187
|
+
.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
|
238
188
|
end
|
239
189
|
|
240
|
-
|
241
|
-
|
190
|
+
def self.hmac_sha256(message, key)
|
191
|
+
block_size = 64
|
192
|
+
ipad = "\x36" * block_size
|
193
|
+
opad = "\x5c" * block_size
|
194
|
+
if key.size > block_size
|
195
|
+
d = Digest::SHA256.new
|
196
|
+
key = d.digest(key)
|
197
|
+
end
|
242
198
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
199
|
+
ipad_bytes = ipad.bytes.map { |b| b }
|
200
|
+
opad_bytes = opad.bytes.map { |b| b }
|
201
|
+
key_bytes = key.bytes.map { |b| b }
|
202
|
+
ipad_xor = ""
|
203
|
+
opad_xor = ""
|
204
|
+
(0..key.size - 1).each do |i|
|
205
|
+
ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
|
206
|
+
opad_xor << (opad_bytes[i] ^ key_bytes[i])
|
207
|
+
end
|
248
208
|
|
249
|
-
|
250
|
-
|
251
|
-
d2.update(opad)
|
252
|
-
d2.update(msg_hash)
|
253
|
-
d2.digest
|
254
|
-
end
|
209
|
+
ipad = ipad_xor + ipad[key.size..-1]
|
210
|
+
opad = opad_xor + opad[key.size..-1]
|
255
211
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
host = Regexp.last_match[1]
|
262
|
-
path = Regexp.last_match[2]
|
263
|
-
param_string = Regexp.last_match[3]
|
212
|
+
# inner hash
|
213
|
+
d1 = Digest::SHA256.new
|
214
|
+
d1.update(ipad)
|
215
|
+
d1.update(message)
|
216
|
+
msg_hash = d1.digest
|
264
217
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
218
|
+
# outer hash
|
219
|
+
d2 = Digest::SHA256.new
|
220
|
+
d2.update(opad)
|
221
|
+
d2.update(msg_hash)
|
222
|
+
d2.digest
|
223
|
+
end
|
269
224
|
|
270
|
-
|
271
|
-
|
272
|
-
# NOTE : take care not to double-encode
|
225
|
+
def self.sign_request(request)
|
226
|
+
raise AmazonNotConfiguredError unless @@secret_access_key
|
273
227
|
|
274
|
-
|
275
|
-
|
228
|
+
# Step 0 : Split apart request string
|
229
|
+
url_pattern = %r{http://([^/]+)(/[^?]+)\?(.*$)}
|
230
|
+
url_pattern =~ request
|
231
|
+
host = Regexp.last_match[1]
|
232
|
+
path = Regexp.last_match[2]
|
233
|
+
param_string = Regexp.last_match[3]
|
276
234
|
|
277
|
-
|
278
|
-
|
235
|
+
# Step 1: enter the timestamp
|
236
|
+
t = Time.now.getutc # MUST be in UTC
|
237
|
+
stamp = t.strftime("%Y-%m-%dT%H:%M:%SZ")
|
238
|
+
param_string += "&Timestamp=#{stamp}"
|
279
239
|
|
280
|
-
|
281
|
-
|
240
|
+
# Step 2 : URL-encode
|
241
|
+
param_string = param_string.gsub(",", "%2C").gsub(":", "%3A")
|
242
|
+
# NOTE : take care not to double-encode
|
282
243
|
|
283
|
-
|
284
|
-
|
244
|
+
# Step 3 : Split the parameter/value pairs
|
245
|
+
params = param_string.split("&")
|
285
246
|
|
286
|
-
|
247
|
+
# Step 4 : Sort params
|
248
|
+
params.sort!
|
287
249
|
|
288
|
-
|
289
|
-
|
290
|
-
base64_sig = [sig].pack('m').strip
|
250
|
+
# Step 5 : Rejoin the param string
|
251
|
+
canonical_param_string = params.join("&")
|
291
252
|
|
292
|
-
|
293
|
-
|
253
|
+
# Steps 6 & 7: Prepend HTTP request info
|
254
|
+
string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
|
294
255
|
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
end
|
256
|
+
# Step 8 : Calculate RFC 2104-compliant HMAC with SHA256 hash algorithm
|
257
|
+
sig = hmac_sha256(string_to_sign, @@secret_access_key)
|
258
|
+
base64_sig = [sig].pack("m").strip
|
299
259
|
|
300
|
-
|
301
|
-
|
302
|
-
# Pass Hpricot::Elements object
|
303
|
-
def initialize(element)
|
304
|
-
@element = element
|
305
|
-
end
|
260
|
+
# Step 9 : URL-encode + and = in sig
|
261
|
+
base64_sig = CGI.escape(base64_sig)
|
306
262
|
|
307
|
-
|
308
|
-
|
309
|
-
|
263
|
+
# Step 10 : Add the URL encoded signature to your request
|
264
|
+
"http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
|
265
|
+
end
|
310
266
|
end
|
311
267
|
|
312
|
-
#
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
268
|
+
# Internal wrapper class to provide convenient method to access Hpricot element value.
|
269
|
+
class Element
|
270
|
+
# Pass Hpricot::Elements object
|
271
|
+
def initialize(element)
|
272
|
+
@element = element
|
273
|
+
end
|
318
274
|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
return unless elements
|
324
|
-
elements = elements.map { |element| Element.new(element) }
|
325
|
-
return elements.first if elements.size == 1
|
326
|
-
elements
|
327
|
-
end
|
275
|
+
# Returns Hpricot::Elments object
|
276
|
+
def elem
|
277
|
+
@element
|
278
|
+
end
|
328
279
|
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
280
|
+
# Find Hpricot::Elements matching the given path. Example: element/"author".
|
281
|
+
def /(path)
|
282
|
+
elements = @element / path
|
283
|
+
return nil if elements.empty?
|
333
284
|
|
334
|
-
|
335
|
-
|
336
|
-
Element.get_unescaped(@element, path)
|
337
|
-
end
|
285
|
+
elements
|
286
|
+
end
|
338
287
|
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
288
|
+
# Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
|
289
|
+
# Returns an array Amazon::Elements if more than Hpricot::Elements size is
|
290
|
+
# greater than 1.
|
291
|
+
def search_and_convert(path)
|
292
|
+
elements = self./(path)
|
293
|
+
return unless elements
|
343
294
|
|
344
|
-
|
345
|
-
|
346
|
-
Element.get_hash(@element, path)
|
347
|
-
end
|
295
|
+
elements = elements.map { |element| Element.new(element) }
|
296
|
+
return elements.first if elements.size == 1
|
348
297
|
|
349
|
-
|
350
|
-
|
351
|
-
return unless element
|
352
|
-
result = element.at(path)
|
353
|
-
## inner_html doesn't decode entities, hence bug #21659
|
354
|
-
# result = result.inner_html if result
|
355
|
-
result = result.inner_text if result
|
356
|
-
result
|
357
|
-
end
|
298
|
+
elements
|
299
|
+
end
|
358
300
|
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
end
|
301
|
+
# Get the text value of the given path, leave empty to retrieve current element value.
|
302
|
+
def get(path = "")
|
303
|
+
Element.get(@element, path)
|
304
|
+
end
|
364
305
|
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
306
|
+
# Get the unescaped HTML text of the given path.
|
307
|
+
def get_unescaped(path = "")
|
308
|
+
Element.get_unescaped(@element, path)
|
309
|
+
end
|
310
|
+
|
311
|
+
# Get the array values of the given path.
|
312
|
+
def get_array(path = "")
|
313
|
+
Element.get_array(@element, path)
|
314
|
+
end
|
315
|
+
|
316
|
+
# Get the children element text values in hash format with the element
|
317
|
+
# names as the hash keys.
|
318
|
+
def get_hash(path = "")
|
319
|
+
Element.get_hash(@element, path)
|
378
320
|
end
|
379
|
-
end
|
380
321
|
|
381
|
-
|
382
|
-
|
383
|
-
|
322
|
+
# Similar to #get, except an element object must be passed-in.
|
323
|
+
def self.get(element, path = "")
|
324
|
+
return unless element
|
325
|
+
|
326
|
+
result = element.at(path)
|
327
|
+
## inner_html doesn't decode entities, hence bug #21659
|
328
|
+
# result = result.inner_html if result
|
329
|
+
result = result.inner_text if result
|
330
|
+
result
|
331
|
+
end
|
332
|
+
|
333
|
+
# Similar to #get_unescaped, except an element object must be passed-in.
|
334
|
+
def self.get_unescaped(element, path = "")
|
335
|
+
result = get(element, path)
|
336
|
+
CGI.unescapeHTML(result) if result
|
337
|
+
end
|
338
|
+
|
339
|
+
# Similar to #get_array, except an element object must be passed-in.
|
340
|
+
def self.get_array(element, path = "")
|
341
|
+
return unless element
|
342
|
+
|
343
|
+
result = element / path
|
344
|
+
if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
|
345
|
+
parsed_result = []
|
346
|
+
result.each do |item|
|
347
|
+
parsed_result << Element.get(item)
|
348
|
+
end
|
349
|
+
parsed_result
|
350
|
+
else
|
351
|
+
[Element.get(result)]
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
# Similar to #get_hash, except an element object must be passed-in.
|
356
|
+
def self.get_hash(element, path = "")
|
357
|
+
result = element&.at(path)
|
358
|
+
return unless result
|
384
359
|
|
385
|
-
result = element.at(path)
|
386
|
-
if result
|
387
360
|
hash = {}
|
388
361
|
result = result.children
|
389
362
|
result.each do |item|
|
@@ -391,10 +364,10 @@ module Amazon
|
|
391
364
|
end
|
392
365
|
hash
|
393
366
|
end
|
394
|
-
end
|
395
367
|
|
396
|
-
|
397
|
-
|
368
|
+
def to_s
|
369
|
+
elem&.to_s
|
370
|
+
end
|
398
371
|
end
|
399
372
|
end
|
400
373
|
end
|