Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
diff --git a/changelog.d/rich-media-youtube-oembed.fix b/changelog.d/rich-media-youtube-oembed.fix
new file mode 100644
index 000000000..8824717a6
--- /dev/null
+++ b/changelog.d/rich-media-youtube-oembed.fix
@@ -0,0 +1 @@
+Rich media: get YouTube cards from the YouTube oEmbed endpoint, as YouTube often blocks page fetches from servers
diff --git a/lib/pleroma/web/rich_media/parser.ex b/lib/pleroma/web/rich_media/parser.ex
index f1445a301..e4e03319e 100644
--- a/lib/pleroma/web/rich_media/parser.ex
+++ b/lib/pleroma/web/rich_media/parser.ex
@@ -4,6 +4,7 @@
defmodule Pleroma.Web.RichMedia.Parser do
alias Pleroma.Web.RichMedia.Helpers
+ alias Pleroma.Web.RichMedia.Parsers.OEmbed
import Pleroma.Web.Metadata.Utils, only: [scrub_html_and_truncate: 2]
require Logger
@@ -31,6 +32,15 @@ defmodule Pleroma.Web.RichMedia.Parser do
end
defp parse_url(url) do
+ with {:ok, data} <- OEmbed.direct_lookup(url),
+ {:ok, data} <- data |> clean_parsed_data() |> check_parsed_data() do
+ {:ok, data}
+ else
+ _ -> parse_page(url)
+ end
+ end
+
+ defp parse_page(url) do
with {:ok, body} <- Helpers.rich_media_get(url),
{:ok, html} <- Floki.parse_document(body) do
html
diff --git a/lib/pleroma/web/rich_media/parsers/o_embed.ex b/lib/pleroma/web/rich_media/parsers/o_embed.ex
index 8fffe1d0e..e1c9f7888 100644
--- a/lib/pleroma/web/rich_media/parsers/o_embed.ex
+++ b/lib/pleroma/web/rich_media/parsers/o_embed.ex
@@ -3,6 +3,25 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
+ # YouTube often serves server IPs a captcha redirect or a page without
+ # metadata, while its oEmbed endpoint keeps working. Ask it directly.
+ @youtube_hosts ~w(youtube.com www.youtube.com m.youtube.com music.youtube.com youtu.be)
+ @youtube_endpoint "https://www.youtube.com/oembed"
+
+ @doc """
+ Fetches oEmbed data straight from the provider endpoint for known hosts
+ (YouTube), without loading the page first.
+ """
+ @spec direct_lookup(String.t()) :: {:ok, map()} | :error
+ def direct_lookup(url) do
+ with %URI{host: host} when is_binary(host) <- URI.parse(url),
+ true <- String.downcase(host) in @youtube_hosts do
+ get_oembed_data(@youtube_endpoint <> "?" <> URI.encode_query(url: url, format: "json"))
+ else
+ _ -> :error
+ end
+ end
+
def parse(html, _data) do
with elements = [_ | _] <- get_discovery_data(html),
oembed_url when is_binary(oembed_url) <- get_oembed_url(elements),
diff --git a/test/fixtures/rich_media/youtube_oembed.json b/test/fixtures/rich_media/youtube_oembed.json
new file mode 100644
index 000000000..a8037bb02
--- /dev/null
+++ b/test/fixtures/rich_media/youtube_oembed.json
@@ -0,0 +1 @@
+{"title":"Test Video Title","author_name":"Test Channel","author_url":"https://www.youtube.com/@testchannel","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/YOUTUBETEST1/hqdefault.jpg","html":"<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/YOUTUBETEST1?feature=oembed\" frameborder=\"0\" allowfullscreen title=\"Test Video Title\"></iframe>"}
diff --git a/test/pleroma/web/rich_media/parser_test.exs b/test/pleroma/web/rich_media/parser_test.exs
index d516193ec..2a38ee733 100644
--- a/test/pleroma/web/rich_media/parser_test.exs
+++ b/test/pleroma/web/rich_media/parser_test.exs
@@ -98,6 +98,32 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
}}
end
+ describe "YouTube" do
+ test "uses the oEmbed endpoint without fetching the watch page" do
+ # The watch page has no mock: YouTube often answers server IPs with a
+ # captcha redirect, so the card must not depend on it.
+ assert {:ok, data} = Parser.parse("https://www.youtube.com/watch?v=YOUTUBETEST1")
+
+ assert %{
+ "title" => "Test Video Title",
+ "author_name" => "Test Channel",
+ "provider_name" => "YouTube",
+ "image" => "https://i.ytimg.com/vi/YOUTUBETEST1/hqdefault.jpg",
+ "url" => "https://www.youtube.com/watch?v=YOUTUBETEST1"
+ } = data
+ end
+
+ test "works for youtu.be links" do
+ assert {:ok, %{"title" => "Test Video Title", "url" => "https://youtu.be/YOUTUBETEST1"}} =
+ Parser.parse("https://youtu.be/YOUTUBETEST1")
+ end
+
+ test "falls back to the page when the oEmbed endpoint fails" do
+ assert {:ok, %{"title" => "The Rock"}} =
+ Parser.parse("https://www.youtube.com/watch?v=NOOEMBED")
+ end
+ end
+
test "does not fetch oEmbed URLs that fail page URL validation" do
assert {:error, :invalid_metadata} = Parser.parse("https://example.com/oembed-internal")
end
diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex
index 93786973c..0c3ab0c60 100644
--- a/test/support/http_request_mock.ex
+++ b/test/support/http_request_mock.ex
@@ -1515,6 +1515,33 @@ defmodule HttpRequestMock do
}}
end
+ @youtube_oembed_json [
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DYOUTUBETEST1&format=json",
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fyoutu.be%2FYOUTUBETEST1&format=json"
+ ]
+
+ def get(url, _, _, _) when url in @youtube_oembed_json do
+ {:ok,
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/json"}],
+ body: File.read!("test/fixtures/rich_media/youtube_oembed.json")
+ }}
+ end
+
+ def get(
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNOOEMBED&format=json",
+ _,
+ _,
+ _
+ ) do
+ {:ok, %Tesla.Env{status: 404, body: "Not Found"}}
+ end
+
+ def get("https://www.youtube.com/watch?v=NOOEMBED", _, _, _) do
+ {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
+ end
+
def get("https://example.com/twitter-card", _, _, _) do
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}}
end
@@ -1783,6 +1810,7 @@ defmodule HttpRequestMock do
"https://example.com/ogp-missing-data",
"https://example.com/ogp-missing-title",
"https://example.com/twitter-card",
+ "https://www.youtube.com/watch?v=NOOEMBED",
"https://google.com/",
"https://pleroma.local/notice/9kCP7V",
"https://yahoo.com/",
@@ -1793,6 +1821,19 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: ""}}
end
+ def head(url, _, _, _) when url in @youtube_oembed_json do
+ {:ok, %Tesla.Env{status: 200, headers: [{"content-type", "application/json"}]}}
+ end
+
+ def head(
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNOOEMBED&format=json",
+ _,
+ _,
+ _
+ ) do
+ {:ok, %Tesla.Env{status: 404}}
+ end
+
def head("https://example.com/oembed.json", _, _, _) do
{:ok, %Tesla.Env{status: 200, headers: [{"content-type", "application/json; charset=utf-8"}]}}
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 17, 8:54 PM (15 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1839802
Default Alt Text
(7 KB)

Event Timeline