1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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 oembed_url when is_binary(oembed_url) <- get_oembed_url(elements),
9 {:ok, oembed_data = %{"html" => html}} <- get_oembed_data(oembed_url) do
10 %{oembed_data | "html" => Pleroma.HTML.filter_tags(html)}
16 defp get_discovery_data(html) do
17 html |> Floki.find("link[type='application/json+oembed']")
20 defp get_oembed_url([{"link", attributes, _children} | _]) do
21 Enum.find_value(attributes, fn {k, v} -> if k == "href", do: v end)
24 defp get_oembed_data(url) do
25 with {:ok, %Tesla.Env{body: json}} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url) do