forked from AkkomaGang/akkoma
* federation (ap, salmon) * media (rich media, media proxy) * upload (uploader proxy) Each "part" will stop fighting others ones -- a huge federation outbound could before make the media proxy fail to checkout a connection in time. splitted media and uploaded media for the good reason than an upload pool will have all connections to the same host (the uploader upstream). it also has a longer default retention period for connections.
31 lines
831 B
Elixir
31 lines
831 B
Elixir
defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
|
|
def parse(html, _data) do
|
|
with elements = [_ | _] <- get_discovery_data(html),
|
|
{:ok, oembed_url} <- get_oembed_url(elements),
|
|
{:ok, oembed_data} <- get_oembed_data(oembed_url) do
|
|
{:ok, oembed_data}
|
|
else
|
|
_e -> {:error, "No OEmbed data found"}
|
|
end
|
|
end
|
|
|
|
defp get_discovery_data(html) do
|
|
html |> Floki.find("link[type='application/json+oembed']")
|
|
end
|
|
|
|
defp get_oembed_url(nodes) do
|
|
{"link", attributes, _children} = nodes |> hd()
|
|
|
|
{:ok, Enum.into(attributes, %{})["href"]}
|
|
end
|
|
|
|
defp get_oembed_data(url) do
|
|
{:ok, %Tesla.Env{body: json}} = Pleroma.HTTP.get(url, [], pool: :media)
|
|
|
|
{:ok, data} = Jason.decode(json)
|
|
|
|
data = data |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
|
|
|
|
{:ok, data}
|
|
end
|
|
end
|