Skip to content

A platform for community discussion. Free, open, simple.

FIX: Use oEmbed for YouTube oneboxing (#35959)

Martin Brennan2b04fbeb3053
diff --git a/lib/onebox/engine/standard_embed.rb b/lib/onebox/engine/standard_embed.rb
index e3175d6247d..15dbea87fda 100644
--- a/lib/onebox/engine/standard_embed.rb
+++ b/lib/onebox/engine/standard_embed.rb
@@ -32,6 +32,8 @@ module Onebox
add_oembed_provider(%r{vimeo\.com/}, "https://vimeo.com/api/oembed.json")
# NYT requires login so use oembed only
add_oembed_provider(%r{nytimes\.com/}, "https://www.nytimes.com/svc/oembed/json/")
+ # YouTube's oEmbed for reliable metadata (thumbnails, titles)
+ add_oembed_provider(/youtube\.com|youtu\.be/, "https://www.youtube.com/oembed")
def always_https?
AllowlistedGenericOnebox.host_matches(uri, AllowlistedGenericOnebox.https_hosts) || super
diff --git a/lib/onebox/engine/youtube_onebox.rb b/lib/onebox/engine/youtube_onebox.rb
index 10953c1706b..d3c571c0446 100644
--- a/lib/onebox/engine/youtube_onebox.rb
+++ b/lib/onebox/engine/youtube_onebox.rb
@@ -22,11 +22,30 @@ module Onebox
end
def parse_embed_response
- return unless video_id
return @parse_embed_response if defined?(@parse_embed_response)
- embed_url = self.class.embed_url(video_id)
- @embed_doc ||= Onebox::Helpers.fetch_html_doc(embed_url)
+ # Only try oEmbed for video URLs (not channels, playlists, etc.)
+ # This is a fast check before making the HTTP request
+ if video_id
+ # Try oEmbed first (most reliable)
+ begin
+ oembed = get_oembed
+ if oembed.data[:thumbnail_url] && oembed.data[:title]
+ @parse_embed_response = {
+ image: oembed.data[:thumbnail_url],
+ title: oembed.data[:title],
+ }
+ return @parse_embed_response
+ end
+ rescue StandardError
+ # Fall through to other methods
+ end
+
+ # Try parsing embed page JSON (legacy, can be broken
+ # or missing in some cases)
+ embed_url = self.class.embed_url(video_id)
+ @embed_doc ||= Onebox::Helpers.fetch_html_doc(embed_url)
+ end
begin
script_tag =
@@ -42,11 +61,10 @@ module Onebox
title = renderer["title"]["runs"].first["text"]
image = "https://img.youtube.com/vi/#{video_id}/hqdefault.jpg"
+
+ @parse_embed_response = { image:, title: }
rescue StandardError
- return
end
-
- @parse_embed_response = { image:, title: }
end
def placeholder_html
diff --git a/spec/lib/onebox/engine/youtube_onebox_spec.rb b/spec/lib/onebox/engine/youtube_onebox_spec.rb
index 21c07ca4d8c..b8617e1463c 100644
--- a/spec/lib/onebox/engine/youtube_onebox_spec.rb
+++ b/spec/lib/onebox/engine/youtube_onebox_spec.rb
@@ -1,6 +1,15 @@
# frozen_string_literal: true
RSpec.describe Onebox::Engine::YoutubeOnebox do
+ let(:oembed_standard_response) do
+ {
+ title: "96neko - orange",
+ author_name: "96neko",
+ type: "video",
+ thumbnail_url: "https://i.ytimg.com/vi/21Lk4YiASMo/hqdefault.jpg",
+ }.to_json
+ end
+
before do
stub_request(
:get,
@@ -54,6 +63,64 @@ RSpec.describe Onebox::Engine::YoutubeOnebox do
status: 200,
body: onebox_response("youtube-shorts"),
)
+
+ stub_request(:get, "https://www.youtube.com/watch?v=KCyIfcevExE").to_return(
+ status: 200,
+ body: onebox_response("youtube"),
+ )
+
+ stub_request(:get, "https://youtube.com/shorts/VvoFuaLAslw").to_return(
+ status: 200,
+ body: onebox_response("youtube-shorts"),
+ )
+
+ # Stub oEmbed API calls for video URLs
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=https://www\.youtube\.com/watch},
+ ).to_return(status: 200, body: oembed_standard_response)
+ # Make oEmbed fail for KCyIfcevExE to test fallback to embed page parsing
+ # Must come after the general stub to override it
+ stub_request(
+ :get,
+ "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=KCyIfcevExE",
+ ).to_return(status: 404)
+ stub_request(:get, %r{https://www\.youtube\.com/oembed\?url=https://youtu\.be}).to_return(
+ status: 200,
+ body: oembed_standard_response,
+ )
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=http://www\.youtube\.com/watch},
+ ).to_return(status: 200, body: oembed_standard_response)
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=https://www\.youtube\.com/live},
+ ).to_return(status: 200, body: oembed_standard_response)
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=https://youtube\.com/shorts},
+ ).to_return(
+ status: 200,
+ body: {
+ title: "POMBO",
+ thumbnail_url: "https://i.ytimg.com/vi/VvoFuaLAslw/hqdefault.jpg",
+ }.to_json,
+ )
+
+ # Channels, playlists, etc. don't support oEmbed - return 404
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=https://www\.youtube\.com/channel},
+ ).to_return(status: 404)
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=http://www\.youtube\.com/user},
+ ).to_return(status: 404)
+ stub_request(
+ :get,
+ %r{https://www\.youtube\.com/oembed\?url=https://www\.youtube\.com/playlist},
+ ).to_return(status: 404)
end
it "adds wmode=opaque" do
@@ -207,4 +274,65 @@ RSpec.describe Onebox::Engine::YoutubeOnebox do
it "generates a thumbnail for videos" do
expect(Onebox.preview("https://www.youtube.com/watch?v=21Lk4YiASMo").to_s).to match("<img")
end
+
+ describe "oEmbed support" do
+ let(:video_url) { "https://www.youtube.com/watch?v=wC10VWDTzmU" }
+ let(:oembed_url) { "https://www.youtube.com/oembed?url=#{video_url}" }
+ let(:oembed_response) do
+ {
+ title: "Bob Dylan - Gotta Serve Somebody (Official Audio)",
+ author_name: "BobDylanVEVO",
+ author_url: "https://www.youtube.com/@BobDylanVEVO",
+ type: "video",
+ height: 113,
+ width: 200,
+ version: "1.0",
+ provider_name: "YouTube",
+ provider_url: "https://www.youtube.com/",
+ thumbnail_height: 360,
+ thumbnail_width: 480,
+ thumbnail_url: "https://i.ytimg.com/vi/wC10VWDTzmU/hqdefault.jpg",
+ html:
+ '<iframe width="200" height="113" src="https://www.youtube.com/embed/wC10VWDTzmU?feature=oembed"></iframe>',
+ }.to_json
+ end
+
+ before do
+ stub_request(:get, oembed_url).to_return(status: 200, body: oembed_response)
+ stub_request(:get, video_url).to_return(status: 200, body: onebox_response("youtube"))
+ end
+
+ it "uses oEmbed API for metadata" do
+ onebox = Onebox::Engine::YoutubeOnebox.new(video_url)
+ result = onebox.send(:parse_embed_response)
+
+ expect(result).to be_present
+ expect(result[:title]).to eq("Bob Dylan - Gotta Serve Somebody (Official Audio)")
+ expect(result[:image]).to eq("https://i.ytimg.com/vi/wC10VWDTzmU/hqdefault.jpg")
+ end
+
+ it "includes oEmbed data in placeholder_html" do
+ placeholder = Onebox.preview(video_url).placeholder_html
+
+ expect(placeholder).to include("Bob Dylan - Gotta Serve Somebody (Official Audio)")
+ expect(placeholder).to include("https://i.ytimg.com/vi/wC10VWDTzmU/hqdefault.jpg")
+ expect(placeholder).to match(/<img/)
+ end
+
+ it "falls back to OpenGraph when oEmbed fails" do
+ stub_request(:get, oembed_url).to_return(status: 404)
+ stub_request(:get, "https://www.youtube.com/embed/wC10VWDTzmU").to_return(
+ status: 200,
+ body: onebox_response("youtube-embed"),
+ )
+
+ onebox = Onebox::Engine::YoutubeOnebox.new(video_url)
+ result = onebox.send(:parse_embed_response)
+
+ # Should fall back but won't get data from the broken embed page
+ # The fallback to get_opengraph happens in placeholder_html
+ placeholder = onebox.placeholder_html
+ expect(placeholder).to match(/<img/)
+ end
+ end
end
diff --git a/spec/lib/oneboxer_spec.rb b/spec/lib/oneboxer_spec.rb
index 10a10695645..496a6fd0ae2 100644
--- a/spec/lib/oneboxer_spec.rb
+++ b/spec/lib/oneboxer_spec.rb
@@ -717,6 +717,17 @@ RSpec.describe Oneboxer do
status: 200,
body: html,
)
+ stub_request(
+ :get,
+ "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=dQw4w9WgXcQ",
+ ).to_return(
+ status: 200,
+ body: {
+ title: "Onebox1 - ceci n'est pas un titre",
+ author_name: "Blah",
+ thumbnail_url: "https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg",
+ }.to_json,
+ )
stub_request(:any, "https://www.youtube.com/embed/dQw4w9WgXcQ").to_return(
status: 403,
body: nil,
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index a31109b1684..999b2aca9bd 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -270,6 +270,10 @@ RSpec.describe TopicEmbed do
headers: {
},
)
+ stub_request(
+ :get,
+ "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=K56soYl0U1w",
+ ).to_return(status: 200, body: "", headers: {})
imported_post =
TopicEmbed.import(