alexandria-book-collection-manager 0.7.3 → 0.7.8
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 +4 -1
- data/.rubocop.yml +86 -36
- data/.rubocop_todo.yml +58 -161
- data/.simplecov +5 -2
- data/CHANGELOG.md +56 -2
- data/Gemfile +4 -3
- data/INSTALL.md +23 -11
- data/README.md +52 -41
- data/Rakefile +78 -75
- data/alexandria-book-collection-manager.gemspec +50 -44
- data/bin/alexandria +12 -22
- data/doc/FAQ +1 -2
- data/doc/dependency_decisions.yml +27 -8
- data/lib/alexandria.rb +25 -23
- data/lib/alexandria/about.rb +50 -50
- data/lib/alexandria/book_providers.rb +86 -91
- data/lib/alexandria/book_providers/adlibris.rb +37 -74
- data/lib/alexandria/book_providers/amazon_aws.rb +94 -100
- data/lib/alexandria/book_providers/amazon_ecs_util.rb +289 -324
- data/lib/alexandria/book_providers/barnes_and_noble.rb +42 -42
- data/lib/alexandria/book_providers/douban.rb +25 -41
- data/lib/alexandria/book_providers/proxis.rb +34 -29
- data/lib/alexandria/book_providers/pseudomarc.rb +77 -85
- data/lib/alexandria/book_providers/siciliano.rb +60 -64
- data/lib/alexandria/book_providers/thalia_provider.rb +161 -0
- data/lib/alexandria/book_providers/web.rb +5 -5
- data/lib/alexandria/book_providers/worldcat.rb +66 -95
- data/lib/alexandria/book_providers/z3950.rb +153 -169
- data/lib/alexandria/config.rb +1 -1
- data/lib/alexandria/console.rb +3 -3
- data/lib/alexandria/default_preferences.rb +37 -0
- data/lib/alexandria/execution_queue.rb +13 -12
- data/lib/alexandria/export_format.rb +8 -8
- data/lib/alexandria/export_library.rb +128 -127
- data/lib/alexandria/import_library.rb +102 -126
- data/lib/alexandria/import_library_csv.rb +41 -41
- data/lib/alexandria/library_collection.rb +6 -5
- data/lib/alexandria/library_sort_order.rb +4 -2
- data/lib/alexandria/library_store.rb +39 -28
- data/lib/alexandria/logging.rb +10 -14
- data/lib/alexandria/models/book.rb +5 -4
- data/lib/alexandria/models/library.rb +63 -53
- data/lib/alexandria/net.rb +5 -6
- data/lib/alexandria/preferences.rb +66 -63
- data/lib/alexandria/scanners.rb +2 -2
- data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +17 -17
- data/lib/alexandria/scanners/keyboard.rb +8 -8
- data/lib/alexandria/smart_library.rb +110 -112
- data/lib/alexandria/ui.rb +15 -15
- data/lib/alexandria/ui/{dialogs/about_dialog.rb → about_dialog.rb} +2 -2
- data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +108 -109
- data/lib/alexandria/ui/alert_dialog.rb +66 -0
- data/lib/alexandria/ui/{dialogs/bad_isbns_dialog.rb → bad_isbns_dialog.rb} +13 -9
- 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} +25 -38
- data/lib/alexandria/ui/{dialogs/book_properties_dialog_base.rb → book_properties_dialog_base.rb} +64 -157
- data/lib/alexandria/ui/builder_base.rb +1 -1
- data/lib/alexandria/ui/calendar_popup.rb +58 -0
- data/lib/alexandria/ui/callbacks.rb +187 -155
- data/lib/alexandria/ui/completion_models.rb +8 -22
- 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/{dialogs/export_dialog.rb → export_dialog.rb} +37 -58
- data/lib/alexandria/ui/icons.rb +38 -43
- 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 +30 -41
- data/lib/alexandria/ui/{dialogs/keep_bad_isbn_dialog.rb → keep_bad_isbn_dialog.rb} +9 -6
- data/lib/alexandria/ui/libraries_combo.rb +15 -14
- data/lib/alexandria/ui/listview.rb +69 -67
- 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 +8 -9
- data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +113 -114
- data/lib/alexandria/ui/{dialogs/new_book_dialog_manual.rb → new_book_dialog_manual.rb} +22 -19
- 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/{dialogs/misc_dialogs.rb → really_delete_dialog.rb} +7 -28
- data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +53 -48
- 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 +216 -200
- data/lib/alexandria/version.rb +4 -19
- data/lib/alexandria/web_themes.rb +21 -21
- data/po/Makefile +2 -2
- data/po/cs.po +992 -875
- data/po/cy.po +961 -874
- data/po/de.po +990 -865
- data/po/el.po +989 -865
- data/po/es.po +985 -861
- data/po/fr.po +987 -867
- data/po/ga.po +908 -820
- data/po/gl.po +980 -860
- data/po/it.po +986 -864
- data/po/ja.po +969 -849
- data/po/mk.po +984 -860
- data/po/nb.po +979 -859
- data/po/nl.po +983 -860
- data/po/pl.po +1018 -971
- data/po/pt.po +988 -857
- data/po/pt_BR.po +983 -863
- data/po/ru.po +994 -871
- data/po/sk.po +989 -867
- data/po/sv.po +976 -856
- data/po/uk.po +972 -858
- data/po/zh_TW.po +974 -854
- data/schemas/alexandria.schemas +24 -2
- data/share/alexandria/glade/acquire_dialog__builder.glade +1 -1
- data/share/alexandria/glade/book_properties_dialog__builder.glade +1 -1
- data/share/alexandria/glade/main_app__builder.glade +6 -21
- data/share/alexandria/glade/new_book_dialog__builder.glade +1 -1
- data/share/alexandria/glade/preferences_dialog__builder.glade +1 -1
- data/share/gnome/help/alexandria/C/introduction.xml +0 -4
- 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 -4
- data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
- data/spec/alexandria/book_providers/thalia_provider_spec.rb +119 -0
- data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
- data/spec/alexandria/book_providers_spec.rb +62 -156
- data/spec/alexandria/book_spec.rb +12 -10
- data/spec/alexandria/console_spec.rb +6 -11
- data/spec/alexandria/export_library_spec.rb +47 -58
- data/spec/alexandria/library_spec.rb +121 -109
- data/spec/alexandria/library_store_spec.rb +8 -8
- 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 +15 -15
- 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 +59 -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 +51 -0
- data/spec/alexandria/ui/{dialogs/new_book_dialog_spec.rb → new_book_dialog_spec.rb} +4 -4
- 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 +44 -20
- data/spec/end_to_end/basic_run_spec.rb +21 -38
- data/spec/fixtures/cover.jpg +0 -0
- data/spec/spec_helper.rb +54 -10
- data/tasks/setup.rb +2 -2
- data/tasks/spec.rake +11 -11
- data/util/rake/fileinstall.rb +38 -35
- data/util/rake/gettextgenerate.rb +7 -7
- data/util/rake/omfgenerate.rb +7 -7
- metadata +158 -45
- data/dogtail/basic_run_test.py +0 -9
- data/lib/alexandria/book_providers/renaud.rb +0 -155
- data/lib/alexandria/book_providers/thalia.rb +0 -198
- data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -63
- data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -176
- data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -62
- data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -563
- data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -61
- data/lib/alexandria/ui/dialogs/smart_library_properties_dialog_base.rb +0 -423
- data/spec/alexandria/scanners/cuecat_spec.rb +0 -67
- data/spec/alexandria/ui/dialogs_spec.rb +0 -162
- 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,78 +146,63 @@ 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(
|
159
|
+
next unless item.get("itemattributes/productgroup") == "Book"
|
172
160
|
|
173
|
-
atts = item.search_and_convert(
|
174
|
-
title = normalize(atts.get(
|
161
|
+
atts = item.search_and_convert("itemattributes")
|
162
|
+
title = normalize(atts.get("title"))
|
175
163
|
|
176
|
-
media = normalize(atts.get(
|
177
|
-
media = nil if media ==
|
164
|
+
media = normalize(atts.get("binding"))
|
165
|
+
media = nil if media == "Unknown Binding"
|
178
166
|
|
179
|
-
isbn = normalize(atts.get(
|
167
|
+
isbn = normalize(atts.get("isbn"))
|
180
168
|
isbn = (Library.canonicalise_ean(isbn) if isbn && Library.valid_isbn?(isbn))
|
181
169
|
# hack, extract year by regexp (not Y10K compatible :-)
|
182
|
-
/([1-9][0-9]{3})/ =~ atts.get(
|
170
|
+
/([1-9][0-9]{3})/ =~ atts.get("publicationdate")
|
183
171
|
publishing_year = Regexp.last_match[1] ? Regexp.last_match[1].to_i : nil
|
184
172
|
book = Book.new(title,
|
185
|
-
atts.get_array(
|
173
|
+
atts.get_array("author").map { |x| normalize(x) },
|
186
174
|
isbn,
|
187
|
-
normalize(atts.get(
|
175
|
+
normalize(atts.get("manufacturer")),
|
188
176
|
publishing_year,
|
189
177
|
media)
|
190
178
|
|
191
|
-
image_url = item.get(
|
179
|
+
image_url = item.get("mediumimage/url")
|
192
180
|
log.info { "Found at Amazon[#{request_locale}]: #{book.title}" }
|
193
181
|
results << [book, image_url]
|
194
182
|
end
|
195
183
|
if type == SEARCH_BY_ISBN
|
196
184
|
if results.size == 1
|
197
|
-
|
185
|
+
results.first
|
198
186
|
else
|
199
|
-
|
200
|
-
query_isbn_canon = Library.canonicalise_ean(criterion)
|
201
|
-
results.each do |rslt|
|
202
|
-
book = rslt[0]
|
203
|
-
book_isbn_canon = Library.canonicalise_ean(book.isbn)
|
204
|
-
return rslt if query_isbn_canon == book_isbn_canon
|
205
|
-
|
206
|
-
log.debug { "rejected possible result #{book}" }
|
207
|
-
end
|
208
|
-
# gone through all and no ISBN match, so just return first result
|
209
|
-
log.info { 'no more results to check. Returning first result, just an approximation' }
|
210
|
-
return results.first
|
187
|
+
exact_match_or_first(criterion, results)
|
211
188
|
end
|
212
189
|
else
|
213
|
-
|
190
|
+
results
|
214
191
|
end
|
215
192
|
end
|
216
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
|
+
|
217
203
|
def url(book)
|
218
204
|
isbn = Library.canonicalise_isbn(book.isbn)
|
219
|
-
url =
|
220
|
-
when 'fr'
|
221
|
-
'http://www.amazon.fr/exec/obidos/ASIN/%s'
|
222
|
-
when 'uk'
|
223
|
-
'http://www.amazon.co.uk/exec/obidos/ASIN/%s'
|
224
|
-
when 'de'
|
225
|
-
'http://www.amazon.de/exec/obidos/ASIN/%s'
|
226
|
-
when 'ca'
|
227
|
-
'http://www.amazon.ca/exec/obidos/ASIN/%s'
|
228
|
-
when 'jp'
|
229
|
-
'http://www.amazon.jp/exec/obidos/ASIN/%s'
|
230
|
-
when 'us'
|
231
|
-
'http://www.amazon.com/exec/obidos/ASIN/%s'
|
232
|
-
end
|
205
|
+
url = LOCALE_URLS.fetch(prefs["locale"])
|
233
206
|
url % isbn
|
234
207
|
rescue StandardError => ex
|
235
208
|
log.warn { "Cannot create url for book #{book}; #{ex.message}" }
|
@@ -237,9 +210,30 @@ module Alexandria
|
|
237
210
|
end
|
238
211
|
|
239
212
|
def normalize(str)
|
240
|
-
str = str.squeeze(
|
213
|
+
str = str.squeeze(" ").strip unless str.nil?
|
241
214
|
str
|
242
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
|
243
237
|
end
|
244
238
|
end
|
245
239
|
end
|
@@ -1,397 +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
|
-
|
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
|
83
28
|
|
84
|
-
|
85
|
-
|
29
|
+
@@options = {}
|
30
|
+
@@debug = false
|
86
31
|
|
87
|
-
|
88
|
-
# For other search type other than keywords, please specify :type => [search type param name].
|
89
|
-
def self.item_search(terms, opts = {})
|
90
|
-
opts[:operation] = 'ItemSearch'
|
91
|
-
opts[:search_index] = opts[:search_index] || 'Books'
|
92
|
-
|
93
|
-
type = opts.delete(:type)
|
94
|
-
if type
|
95
|
-
opts[type.to_sym] = terms
|
96
|
-
else
|
97
|
-
opts[:keywords] = terms
|
98
|
-
end
|
32
|
+
@@secret_access_key = ""
|
99
33
|
|
100
|
-
|
101
|
-
|
34
|
+
# Default search options
|
35
|
+
def self.options
|
36
|
+
@@options
|
37
|
+
end
|
102
38
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
opts[:item_id] = item_id
|
39
|
+
def self.secret_access_key=(key)
|
40
|
+
@@secret_access_key = key
|
41
|
+
end
|
107
42
|
|
108
|
-
|
109
|
-
|
43
|
+
# Set default search options
|
44
|
+
def self.options=(opts)
|
45
|
+
@@options = opts
|
46
|
+
end
|
110
47
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
end
|
48
|
+
# Get debug flag.
|
49
|
+
def self.debug
|
50
|
+
@@debug
|
51
|
+
end
|
116
52
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
log.debug { "Request URL: #{request_url}" }
|
53
|
+
# Set debug flag to true or false.
|
54
|
+
def self.debug=(dbg)
|
55
|
+
@@debug = dbg
|
56
|
+
end
|
122
57
|
|
123
|
-
|
124
|
-
|
125
|
-
raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
|
58
|
+
def self.configure(&_proc)
|
59
|
+
yield @@options
|
126
60
|
end
|
127
61
|
|
128
|
-
|
129
|
-
|
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
|
130
75
|
|
131
|
-
|
132
|
-
class Response
|
133
|
-
# XML input is in string format
|
134
|
-
def initialize(xml)
|
135
|
-
@doc = Hpricot(xml)
|
76
|
+
send_request(opts)
|
136
77
|
end
|
137
78
|
|
138
|
-
#
|
139
|
-
|
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
|
140
83
|
|
141
|
-
|
142
|
-
def is_valid_request?
|
143
|
-
(@doc / 'isvalid').inner_html == 'True'
|
84
|
+
send_request(opts)
|
144
85
|
end
|
145
86
|
|
146
|
-
#
|
147
|
-
def
|
148
|
-
|
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
|
149
91
|
end
|
150
92
|
|
151
|
-
#
|
152
|
-
|
153
|
-
|
154
|
-
|
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
|
155
105
|
|
156
|
-
|
157
|
-
def items
|
158
|
-
@items ||= (@doc / 'item').map { |item| Element.new(item) }
|
159
|
-
@items
|
106
|
+
Response.new(res.body)
|
160
107
|
end
|
161
108
|
|
162
|
-
#
|
163
|
-
|
164
|
-
|
165
|
-
|
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
|
166
115
|
|
167
|
-
|
168
|
-
|
169
|
-
@item_page ||= (@doc / 'itemsearchrequest/itempage').inner_html.to_i
|
170
|
-
@item_page
|
171
|
-
end
|
116
|
+
# Return Hpricot object.
|
117
|
+
attr_reader :doc
|
172
118
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
133
|
+
|
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
|
178
156
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
157
|
+
# Return total pages.
|
158
|
+
def total_pages
|
159
|
+
@total_pages ||= (@doc / "totalpages").inner_html.to_i
|
160
|
+
@total_pages
|
161
|
+
end
|
183
162
|
end
|
184
|
-
end
|
185
163
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
# puts s
|
195
|
-
# end
|
196
|
-
# end
|
197
|
-
|
198
|
-
def self.prepare_url(opts)
|
199
|
-
country = opts.delete(:country)
|
200
|
-
country = country.nil? ? 'us' : country
|
201
|
-
request_url = SERVICE_URLS[country.to_sym]
|
202
|
-
raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
|
203
|
-
|
204
|
-
qs = ''
|
205
|
-
opts.each { |k, v|
|
206
|
-
next unless v
|
207
|
-
|
208
|
-
v = v.join(',') if v.is_a? Array
|
209
|
-
qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
|
210
|
-
}
|
211
|
-
url = "#{request_url}#{qs}"
|
212
|
-
# puts ">>> base url >> #{url}"
|
213
|
-
signed_url = sign_request(url)
|
214
|
-
# puts ">>> SIGNED >> #{signed_url}"
|
215
|
-
signed_url
|
216
|
-
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
|
217
172
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
|
222
|
-
end
|
173
|
+
qs = ""
|
174
|
+
opts.each do |k, v|
|
175
|
+
next unless v
|
223
176
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
d = Digest::SHA256.new
|
230
|
-
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)
|
231
182
|
end
|
232
183
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
opad_xor = ''
|
238
|
-
for i in 0..key.size - 1
|
239
|
-
ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
|
240
|
-
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 }
|
241
188
|
end
|
242
189
|
|
243
|
-
|
244
|
-
|
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
|
245
198
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
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
|
251
208
|
|
252
|
-
|
253
|
-
|
254
|
-
d2.update(opad)
|
255
|
-
d2.update(msg_hash)
|
256
|
-
d2.digest
|
257
|
-
end
|
209
|
+
ipad = ipad_xor + ipad[key.size..-1]
|
210
|
+
opad = opad_xor + opad[key.size..-1]
|
258
211
|
|
259
|
-
|
260
|
-
|
212
|
+
# inner hash
|
213
|
+
d1 = Digest::SHA256.new
|
214
|
+
d1.update(ipad)
|
215
|
+
d1.update(message)
|
216
|
+
msg_hash = d1.digest
|
261
217
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
218
|
+
# outer hash
|
219
|
+
d2 = Digest::SHA256.new
|
220
|
+
d2.update(opad)
|
221
|
+
d2.update(msg_hash)
|
222
|
+
d2.digest
|
223
|
+
end
|
268
224
|
|
269
|
-
|
270
|
-
|
271
|
-
stamp = t.strftime('%Y-%m-%dT%H:%M:%SZ')
|
272
|
-
param_string += "&Timestamp=#{stamp}"
|
225
|
+
def self.sign_request(request)
|
226
|
+
raise AmazonNotConfiguredError unless @@secret_access_key
|
273
227
|
|
274
|
-
|
275
|
-
|
276
|
-
|
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]
|
277
234
|
|
278
|
-
|
279
|
-
|
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}"
|
280
239
|
|
281
|
-
|
282
|
-
|
240
|
+
# Step 2 : URL-encode
|
241
|
+
param_string = param_string.gsub(",", "%2C").gsub(":", "%3A")
|
242
|
+
# NOTE : take care not to double-encode
|
283
243
|
|
284
|
-
|
285
|
-
|
244
|
+
# Step 3 : Split the parameter/value pairs
|
245
|
+
params = param_string.split("&")
|
286
246
|
|
287
|
-
|
288
|
-
|
247
|
+
# Step 4 : Sort params
|
248
|
+
params.sort!
|
289
249
|
|
290
|
-
|
250
|
+
# Step 5 : Rejoin the param string
|
251
|
+
canonical_param_string = params.join("&")
|
291
252
|
|
292
|
-
|
293
|
-
|
294
|
-
base64_sig = [sig].pack('m').strip
|
253
|
+
# Steps 6 & 7: Prepend HTTP request info
|
254
|
+
string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
|
295
255
|
|
296
|
-
|
297
|
-
|
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
|
298
259
|
|
299
|
-
|
300
|
-
|
301
|
-
end
|
302
|
-
end
|
260
|
+
# Step 9 : URL-encode + and = in sig
|
261
|
+
base64_sig = CGI.escape(base64_sig)
|
303
262
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
def initialize(element)
|
308
|
-
@element = element
|
263
|
+
# Step 10 : Add the URL encoded signature to your request
|
264
|
+
"http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
|
265
|
+
end
|
309
266
|
end
|
310
267
|
|
311
|
-
#
|
312
|
-
|
313
|
-
|
314
|
-
|
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
|
315
274
|
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
275
|
+
# Returns Hpricot::Elments object
|
276
|
+
def elem
|
277
|
+
@element
|
278
|
+
end
|
320
279
|
|
321
|
-
|
322
|
-
|
280
|
+
# Find Hpricot::Elements matching the given path. Example: element/"author".
|
281
|
+
def /(path)
|
282
|
+
elements = @element / path
|
283
|
+
return nil if elements.empty?
|
323
284
|
|
324
|
-
|
325
|
-
|
326
|
-
def search_and_convert(path)
|
327
|
-
elements = self./(path)
|
328
|
-
return unless elements
|
285
|
+
elements
|
286
|
+
end
|
329
287
|
|
330
|
-
|
331
|
-
|
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
|
332
294
|
|
333
|
-
|
334
|
-
|
295
|
+
elements = elements.map { |element| Element.new(element) }
|
296
|
+
return elements.first if elements.size == 1
|
335
297
|
|
336
|
-
|
337
|
-
|
338
|
-
Element.get(@element, path)
|
339
|
-
end
|
298
|
+
elements
|
299
|
+
end
|
340
300
|
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
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
|
345
305
|
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
306
|
+
# Get the unescaped HTML text of the given path.
|
307
|
+
def get_unescaped(path = "")
|
308
|
+
Element.get_unescaped(@element, path)
|
309
|
+
end
|
350
310
|
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
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)
|
320
|
+
end
|
355
321
|
|
356
|
-
|
357
|
-
|
358
|
-
|
322
|
+
# Similar to #get, except an element object must be passed-in.
|
323
|
+
def self.get(element, path = "")
|
324
|
+
return unless element
|
359
325
|
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
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
|
366
332
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
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
|
372
338
|
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
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
|
386
353
|
end
|
387
|
-
end
|
388
354
|
|
389
|
-
|
390
|
-
|
391
|
-
|
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
|
392
359
|
|
393
|
-
result = element.at(path)
|
394
|
-
if result
|
395
360
|
hash = {}
|
396
361
|
result = result.children
|
397
362
|
result.each do |item|
|
@@ -399,10 +364,10 @@ module Amazon
|
|
399
364
|
end
|
400
365
|
hash
|
401
366
|
end
|
402
|
-
end
|
403
367
|
|
404
|
-
|
405
|
-
|
368
|
+
def to_s
|
369
|
+
elem&.to_s
|
370
|
+
end
|
406
371
|
end
|
407
372
|
end
|
408
373
|
end
|