lib/onebox/engine/amazon_onebox.rb in onebox-1.5.35 vs lib/onebox/engine/amazon_onebox.rb in onebox-1.5.36
- old
+ new
@@ -28,20 +28,23 @@
def match
@match ||= @url.match(/(?:d|g)p\/(?:product\/)?(?<id>[^\/]+)(?:\/|$)/mi)
end
def image
- case
- when raw.css("#main-image").any?
- ::JSON.parse(
- raw.css("#main-image").first
- .attributes["data-a-dynamic-image"]
- .value
- ).keys.first
- when raw.css("#landingImage").any?
- raw.css("#landingImage").first["src"]
+ if (main_image = raw.css("#main-image")) && main_image.any?
+ attributes = main_image.first.attributes
+
+ return attributes["data-a-hires"] if attributes["data-a-hires"]
+
+ if attributes["data-a-dynamic-image"]
+ return ::JSON.parse(attributes["data-a-dynamic-image"].value).keys.first
+ end
end
+
+ if (landing_image = raw.css("#landingImage")) && landing_image.any?
+ landing_image.first["src"]
+ end
end
def data
result = { link: link,
title: raw.css("title").inner_text,
@@ -49,14 +52,17 @@
result[:by_info] = raw.at("#by-line")
result[:by_info] = Onebox::Helpers.clean(result[:by_info].inner_html) if result[:by_info]
# get item price (Amazon markup is inconsistent, deal with it)
- if raw.css("#priceblock_ourprice .restOfPrice")[0] && raw.css("#priceblock_ourprice .restOfPrice")[0].inner_text
- result[:price] = "#{raw.css("#priceblock_ourprice .restOfPrice")[0].inner_text}#{raw.css("#priceblock_ourprice .buyingPrice")[0].inner_text}.#{raw.css("#priceblock_ourprice .restOfPrice")[1].inner_text}"
- else
- result[:price] = raw.css("#priceblock_ourprice").inner_text
- end
+ result[:price] =
+ if raw.css("#priceblock_ourprice .restOfPrice")[0] && raw.css("#priceblock_ourprice .restOfPrice")[0].inner_text
+ "#{raw.css("#priceblock_ourprice .restOfPrice")[0].inner_text}#{raw.css("#priceblock_ourprice .buyingPrice")[0].inner_text}.#{raw.css("#priceblock_ourprice .restOfPrice")[1].inner_text}"
+ elsif raw.css("#priceblock_dealprice") && (dealprice = raw.css("#priceblock_dealprice span")[0])
+ dealprice.inner_text
+ else
+ raw.css("#priceblock_ourprice").inner_text
+ end
summary = raw.at("#productDescription")
result[:description] = summary.inner_text if summary
result
end