Fix rich media parsers

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-05-03 14:52:37 +02:00
parent 46120b16b6
commit 5b36e71581
5 changed files with 39 additions and 17 deletions

View File

@@ -29,11 +29,19 @@ defmodule Mobilizon.Service.RichMedia.Parsers.Fallback do
end
defp get_page(html, :title) do
html |> Floki.find("html head title") |> List.first() |> Floki.text() |> String.trim()
html
|> Floki.parse_document!()
|> Floki.find("html title")
|> List.first()
|> Floki.text()
|> String.trim()
end
defp get_page(html, :description) do
case html |> Floki.find("html head meta[name='description']") |> List.first() do
case html
|> Floki.parse_document!()
|> Floki.find("html meta[name='description']")
|> List.first() do
nil -> ""
elem -> elem |> Floki.attribute("content") |> List.first() |> String.trim()
end

View File

@@ -36,7 +36,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do
end
defp get_elements(html, key_name, prefix) do
html |> Floki.find("meta[#{to_string(key_name)}^='#{prefix}:']")
html |> Floki.parse_document!() |> Floki.find("meta[#{to_string(key_name)}^='#{prefix}:']")
end
defp normalize_attributes(html_node, prefix, key_name, value_name, allowed_attributes) do
@@ -83,14 +83,26 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do
defp maybe_put_description(meta, _), do: meta
@spec get_page_title(String.t()) :: String.t()
defp get_page_title(html) do
html |> Floki.find("html head title") |> List.first() |> Floki.text()
with {:ok, document} <- Floki.parse_document(html),
elem when not is_nil(elem) <- document |> Floki.find("html head title") |> List.first(),
title when is_binary(title) <- Floki.text(elem) do
title
else
_ -> ""
end
end
@spec get_page_description(String.t()) :: String.t()
defp get_page_description(html) do
case html |> Floki.find("html head meta[name='description']") |> List.first() do
nil -> ""
elem -> Floki.attribute(elem, "content")
with {:ok, document} <- Floki.parse_document(html),
elem when not is_nil(elem) <-
document |> Floki.find("html head meta[name='description']") |> List.first(),
description when is_binary(description) <- Floki.attribute(elem, "content") do
description
else
_ -> ""
end
end
end

View File

@@ -32,7 +32,9 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do
end
defp get_discovery_data(html) do
html |> Floki.find("link[type='application/json+oembed']")
with {:ok, document} <- Floki.parse_document(html) do
Floki.find(document, "link[type='application/json+oembed']")
end
end
defp get_oembed_url(nodes) do