1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Parser do
7 Pleroma.Web.RichMedia.Parsers.OGP,
8 Pleroma.Web.RichMedia.Parsers.TwitterCard,
9 Pleroma.Web.RichMedia.Parsers.OEmbed
12 def parse(nil), do: {:error, "No URL provided"}
14 if Mix.env() == :test do
15 def parse(url), do: parse_url(url)
18 with {:ok, data} <- Cachex.fetch(:rich_media_cache, url, fn _ -> parse_url(url) end) do
22 {:error, "Parsing error"}
27 defp parse_url(url) do
29 {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
31 html |> maybe_parse() |> get_parsed_data()
34 {:error, "Parsing error"}
38 defp maybe_parse(html) do
39 Enum.reduce_while(@parsers, %{}, fn parser, acc ->
40 case parser.parse(html, acc) do
41 {:ok, data} -> {:halt, data}
42 {:error, _msg} -> {:cont, acc}
47 defp get_parsed_data(data) when data == %{} do
48 {:error, "No metadata found"}
51 defp get_parsed_data(data) do