Fix OEmbed preview parser

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-10-20 09:36:26 +02:00
parent efd95044c2
commit 2ad043a91d
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 14 additions and 8 deletions

View File

@ -196,10 +196,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
} }
} = _resolution } = _resolution
) do ) do
case Parser.parse(resource_url) do with {:ok, data} when is_map(data) <- Parser.parse(resource_url) do
{:ok, data} when is_map(data) ->
{:ok, struct(Metadata, data)} {:ok, struct(Metadata, data)}
else
{:error, _err} -> {:error, _err} ->
Logger.warn("Error while fetching preview from #{inspect(resource_url)}") Logger.warn("Error while fetching preview from #{inspect(resource_url)}")
{:error, :unknown_resource} {:error, :unknown_resource}

View File

@ -189,8 +189,11 @@ defmodule Mobilizon.Service.RichMedia.Parser do
defp maybe_parse(html) do defp maybe_parse(html) do
Enum.reduce_while(parsers(), %{}, fn parser, acc -> Enum.reduce_while(parsers(), %{}, fn parser, acc ->
case parser.parse(html, acc) do case parser.parse(html, acc) do
{:ok, data} -> {:halt, data} {:ok, data} ->
{:error, _msg} -> {:cont, acc} {:halt, data}
{:error, _msg} ->
{:cont, acc}
end end
end) end)
end end

View File

@ -21,7 +21,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do
with elements = [_ | _] <- get_discovery_data(html), with elements = [_ | _] <- get_discovery_data(html),
{:ok, oembed_url} <- get_oembed_url(elements), {:ok, oembed_url} <- get_oembed_url(elements),
{:ok, oembed_data} <- get_oembed_data(oembed_url), {:ok, oembed_data} <- get_oembed_data(oembed_url),
oembed_data <- filter_oembed_data(oembed_data) do {:ok, oembed_data} <- filter_oembed_data(oembed_data) do
Logger.debug("Data found with OEmbed parser") Logger.debug("Data found with OEmbed parser")
Logger.debug(inspect(oembed_data)) Logger.debug(inspect(oembed_data))
{:ok, oembed_data} {:ok, oembed_data}
@ -55,7 +55,9 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do
{:error, "No type declared for OEmbed data"} {:error, "No type declared for OEmbed data"}
"link" -> "link" ->
Map.put(data, :image_remote_url, Map.get(data, :thumbnail_url)) data
|> Map.put(:image_remote_url, Map.get(data, :thumbnail_url))
|> (&{:ok, &1}).()
"photo" -> "photo" ->
if Map.get(data, :url, "") == "" do if Map.get(data, :url, "") == "" do
@ -65,6 +67,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do
|> Map.put(:image_remote_url, Map.get(data, :url)) |> Map.put(:image_remote_url, Map.get(data, :url))
|> Map.put(:width, Map.get(data, :width, 0)) |> Map.put(:width, Map.get(data, :width, 0))
|> Map.put(:height, Map.get(data, :height, 0)) |> Map.put(:height, Map.get(data, :height, 0))
|> (&{:ok, &1}).()
end end
"video" -> "video" ->
@ -75,6 +78,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do
|> Map.put(:width, Map.get(data, :width, 0)) |> Map.put(:width, Map.get(data, :width, 0))
|> Map.put(:height, Map.get(data, :height, 0)) |> Map.put(:height, Map.get(data, :height, 0))
|> Map.put(:image_remote_url, Map.get(data, :thumbnail_url)) |> Map.put(:image_remote_url, Map.get(data, :thumbnail_url))
|> (&{:ok, &1}).()
"rich" -> "rich" ->
{:error, "OEmbed data has rich type, which we don't support"} {:error, "OEmbed data has rich type, which we don't support"}