A platform for community discussion. Free, open, simple.
FIX: Stop following canonical links in Amazon oneboxes
Régis Hanolb4bbff0939d6
diff --git a/lib/onebox/engine/amazon_onebox.rb b/lib/onebox/engine/amazon_onebox.rbindex 20349dbcd3c..21e91620645 100644--- a/lib/onebox/engine/amazon_onebox.rb+++ b/lib/onebox/engine/amazon_onebox.rb@@ -16,14 +16,10 @@ module Onebox)def url- @raw ||= nil-- if @raw- canonical_link = @raw.at('//link[@rel="canonical"]/@href')- return canonical_link.to_s if canonical_link- end-if match && match[:id]+ canonical_link = @raw&.at('//link[@rel="canonical"]/@href').to_s+ return canonical_link if canonical_link.match?(%r{/dp/#{match[:id]}\z}i)+id =Addressable::URI.encode_component(match[:id], Addressable::URI::CharacterClasses::PATH)return "https://www.amazon.#{tld}/dp/#{id}"@@ -78,6 +74,21 @@ module Onebox@match ||= @url.match(%r{(?:d|g)p/(?:product/|video/detail/)?(?<id>[A-Z0-9]+)(?:/|\?|$)}mi)end+ def raw+ @raw ||=+ Nokogiri.HTML(+ begin+ Onebox::Helpers.fetch_response(+ url,+ headers: http_params,+ raise_error_when_response_too_large: false,+ )+ rescue StandardError+ nil+ end,+ )+ end+def imageif (main_image = raw.css("#main-image")) && main_image.any?attributes = main_image.first.attributes@@ -92,7 +103,7 @@ module Oneboxif (landing_image = raw.css("#landingImage")) && landing_image.any?attributes = landing_image.first.attributes- if attributes["data-old-hires"]+ if attributes["data-old-hires"].to_s.present?return attributes["data-old-hires"].to_selsereturn landing_image.first["src"].to_sdiff --git a/spec/lib/onebox/engine/amazon_onebox_spec.rb b/spec/lib/onebox/engine/amazon_onebox_spec.rbindex acccca6fb7b..f38a7bb7318 100644--- a/spec/lib/onebox/engine/amazon_onebox_spec.rb+++ b/spec/lib/onebox/engine/amazon_onebox_spec.rb@@ -5,11 +5,6 @@ RSpec.describe Onebox::Engine::AmazonOnebox dobefore do@link = "https://www.amazon.com/Knit-Noro-Accessories-Colorful-Little/dp/193609620X"@uri = "https://www.amazon.com/dp/193609620X"-- stub_request(- :get,- "https://www.amazon.com/Seven-Languages-Weeks-Programming-Programmers/dp/193435659X",- ).to_return(status: 200, body: onebox_response("amazon"))endinclude_context "with engines"@@ -145,11 +140,6 @@ RSpec.describe Onebox::Engine::AmazonOnebox dostatus: 200,body: onebox_response("amazon-og"),)-- stub_request(:get, "https://www.amazon.com/Christine-Rebecca-Hall/dp/B01MFXN4Y2").to_return(- status: 200,- body: onebox_response("amazon-og"),- )enddescribe "#to_html" do@@ -180,11 +170,6 @@ RSpec.describe Onebox::Engine::AmazonOnebox dostatus: 200,body: onebox_response("amazon"),)-- stub_request(- :get,- "https://www.amazon.com/Seven-Languages-Weeks-Programming-Programmers/dp/193435659X",- ).to_return(status: 200, body: onebox_response("amazon"))enddescribe "#to_html" do@@ -214,11 +199,6 @@ RSpec.describe Onebox::Engine::AmazonOnebox dostatus: 200,body: onebox_response("amazon-ebook"),)-- stub_request(- :get,- "https://www.amazon.com/Seven-Languages-Weeks-Programming-Programmers-ebook/dp/B00AYQNR46",- ).to_return(status: 200, body: onebox_response("amazon-ebook"))enddescribe "#to_html" do@@ -285,11 +265,6 @@ RSpec.describe Onebox::Engine::AmazonOnebox dostatus: 200,body: onebox_response("amazon-alternate"),)-- stub_request(- :get,- "https://www.amazon.com/Lnchett-Nibbler-Quality-Attachment-Straight/dp/B07FQ7M16H",- ).to_return(status: 200, body: onebox_response("amazon-alternate"))enddescribe "#to_html" do@@ -310,6 +285,35 @@ RSpec.describe Onebox::Engine::AmazonOnebox doit "includes title" doexpect(html).to include("Quality Nibbler Drill Attachment...")end++ it "links to the canonical product page" do+ expect(html).to include(+ %(href="https://www.amazon.com/Lnchett-Nibbler-Quality-Attachment-Straight/dp/B07FQ7M16H"),+ )+ end+ end+ end++ describe "product page with a canonical link to another page" do+ let(:link) { "https://www.amazon.com/dp/B082SBHKN2" }+ let(:image) { "https://m.media-amazon.com/images/I/71-EGU00XgL.jpg" }+ let(:html) { described_class.new(link).to_html }++ before do+ stub_request(:get, link).to_return(+ status: 200,+ body:+ "<html><head><title>SwiftJet Car Wash Foam Gun</title><meta name='description' content='Foam gun'><link rel='canonical' href='https://www.amazon.com/clp/B082SBHKN2'></head><body><img id='landingImage' data-old-hires='' src='#{image}'></body></html>",+ )++ stub_request(:get, "https://www.amazon.com/clp/B082SBHKN2").to_return(+ status: 200,+ body: "<html><body><svg><title>open prime modal</title></svg></body></html>",+ )+ end++ it "renders the product page" do+ expect(html).to include("SwiftJet Car Wash Foam Gun", %(href="#{link}"), %(src="#{image}"))endendend