1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
6 def parse(html, _data) do
7 with elements = [_ | _] <- get_discovery_data(html),
8 {:ok, oembed_url} <- get_oembed_url(elements),
9 {:ok, oembed_data} <- get_oembed_data(oembed_url) do
12 _e -> {:error, "No OEmbed data found"}
16 defp get_discovery_data(html) do
17 html |> Floki.find("link[type='application/json+oembed']")
20 defp get_oembed_url(nodes) do
21 {"link", attributes, _children} = nodes |> hd()
23 {:ok, Enum.into(attributes, %{})["href"]}
26 defp get_oembed_data(url) do
27 {:ok, %Tesla.Env{body: json}} = Pleroma.HTTP.get(url, [], adapter: [pool: :media])
29 {:ok, data} = Jason.decode(json)
31 data = data |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)