Merge branch 'load-all-dms' into 'develop'
[akkoma] / lib / pleroma / web / rich_media / parsers / oembed_parser.ex
1 defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
2 def parse(html, _data) do
3 with elements = [_ | _] <- get_discovery_data(html),
4 {:ok, oembed_url} <- get_oembed_url(elements),
5 {:ok, oembed_data} <- get_oembed_data(oembed_url) do
6 {:ok, oembed_data}
7 else
8 _e -> {:error, "No OEmbed data found"}
9 end
10 end
11
12 defp get_discovery_data(html) do
13 html |> Floki.find("link[type='application/json+oembed']")
14 end
15
16 defp get_oembed_url(nodes) do
17 {"link", attributes, _children} = nodes |> hd()
18
19 {:ok, Enum.into(attributes, %{})["href"]}
20 end
21
22 defp get_oembed_data(url) do
23 {:ok, %Tesla.Env{body: json}} = Pleroma.HTTP.get(url)
24
25 {:ok, Poison.decode!(json)}
26 end
27 end