parsers configurable
[akkoma] / lib / pleroma / web / rich_media / parser.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.RichMedia.Parser do
6 @hackney_options [
7 pool: :media,
8 recv_timeout: 2_000,
9 max_body: 2_000_000,
10 with_body: true
11 ]
12
13 defp parsers do
14 Pleroma.Config.get([:rich_media, :parsers])
15 end
16
17 def parse(nil), do: {:error, "No URL provided"}
18
19 if Pleroma.Config.get(:env) == :test do
20 def parse(url), do: parse_url(url)
21 else
22 def parse(url) do
23 try do
24 Cachex.fetch!(:rich_media_cache, url, fn _ ->
25 {:commit, parse_url(url)}
26 end)
27 rescue
28 e ->
29 {:error, "Cachex error: #{inspect(e)}"}
30 end
31 end
32 end
33
34 defp parse_url(url) do
35 try do
36 {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url, [], adapter: @hackney_options)
37
38 html
39 |> maybe_parse()
40 |> clean_parsed_data()
41 |> check_parsed_data()
42 rescue
43 e ->
44 {:error, "Parsing error: #{inspect(e)}"}
45 end
46 end
47
48 defp maybe_parse(html) do
49 Enum.reduce_while(parsers(), %{}, fn parser, acc ->
50 case parser.parse(html, acc) do
51 {:ok, data} -> {:halt, data}
52 {:error, _msg} -> {:cont, acc}
53 end
54 end)
55 end
56
57 defp check_parsed_data(%{title: title} = data) when is_binary(title) and byte_size(title) > 0 do
58 {:ok, data}
59 end
60
61 defp check_parsed_data(data) do
62 {:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
63 end
64
65 defp clean_parsed_data(data) do
66 data
67 |> Enum.reject(fn {key, val} ->
68 with {:ok, _} <- Jason.encode(%{key => val}) do
69 false
70 else
71 _ -> true
72 end
73 end)
74 |> Map.new()
75 end
76 end