projects
/
akkoma
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge branch 'develop' into activation-meta
[akkoma]
/
lib
/
pleroma
/
web
/
rich_media
/
parser.ex
diff --git
a/lib/pleroma/web/rich_media/parser.ex
b/lib/pleroma/web/rich_media/parser.ex
index b69b2be610a6383ed5f5c97a92f3db4c0fe3ea02..ef5ead2daf1fdcad1309591344223c07513f5136 100644
(file)
--- a/
lib/pleroma/web/rich_media/parser.ex
+++ b/
lib/pleroma/web/rich_media/parser.ex
@@
-1,13
+1,11
@@
# Pleroma: A lightweight social networking server
# Pleroma: A lightweight social networking server
-# Copyright © 2017-20
19
Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-20
20
Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parser do
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parser do
- @
hackney_
options [
+ @options [
pool: :media,
pool: :media,
- recv_timeout: 2_000,
- max_body: 2_000_000,
- with_body: true
+ max_body: 2_000_000
]
defp parsers do
]
defp parsers do
@@
-55,8
+53,8
@@
defmodule Pleroma.Web.RichMedia.Parser do
ttl_setters: [MyModule]
"""
def set_ttl_based_on_image({:ok, data}, url) do
ttl_setters: [MyModule]
"""
def set_ttl_based_on_image({:ok, data}, url) do
- with {:ok, nil} <- Cachex.ttl(:rich_media_cache, url)
do
- ttl = get_ttl_from_image(data, url)
+ with {:ok, nil} <- Cachex.ttl(:rich_media_cache, url)
,
+ ttl when is_number(ttl) <- get_ttl_from_image(data, url) do
Cachex.expire_at(:rich_media_cache, url, ttl * 1000)
{:ok, data}
else
Cachex.expire_at(:rich_media_cache, url, ttl * 1000)
{:ok, data}
else
@@
-77,29
+75,44
@@
defmodule Pleroma.Web.RichMedia.Parser do
end
defp parse_url(url) do
end
defp parse_url(url) do
+ opts =
+ if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
+ Keyword.merge(@options,
+ recv_timeout: 2_000,
+ with_body: true
+ )
+ else
+ @options
+ end
+
try do
try do
- {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url, [], adapter:
@hackney_option
s)
+ {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url, [], adapter:
opt
s)
html
html
+ |> parse_html()
|> maybe_parse()
|> maybe_parse()
+ |> Map.put("url", url)
|> clean_parsed_data()
|> check_parsed_data()
rescue
e ->
|> clean_parsed_data()
|> check_parsed_data()
rescue
e ->
- {:error, "Parsing error: #{inspect(e)}"}
+ {:error, "Parsing error: #{inspect(e)}
#{inspect(__STACKTRACE__)}
"}
end
end
end
end
+ defp parse_html(html), do: Floki.parse_document!(html)
+
defp maybe_parse(html) do
Enum.reduce_while(parsers(), %{}, fn parser, acc ->
case parser.parse(html, acc) do
defp maybe_parse(html) do
Enum.reduce_while(parsers(), %{}, fn parser, acc ->
case parser.parse(html, acc) do
-
{:ok, data
} -> {:halt, data}
-
{:error, _msg}
-> {:cont, acc}
+
data when data != %{
} -> {:halt, data}
+
_
-> {:cont, acc}
end
end)
end
end
end)
end
- defp check_parsed_data(%{title: title} = data) when is_binary(title) and byte_size(title) > 0 do
+ defp check_parsed_data(%{"title" => title} = data)
+ when is_binary(title) and title != "" do
{:ok, data}
end
{:ok, data}
end
@@
-110,11
+123,7
@@
defmodule Pleroma.Web.RichMedia.Parser do
defp clean_parsed_data(data) do
data
|> Enum.reject(fn {key, val} ->
defp clean_parsed_data(data) do
data
|> Enum.reject(fn {key, val} ->
- with {:ok, _} <- Jason.encode(%{key => val}) do
- false
- else
- _ -> true
- end
+ not match?({:ok, _}, Jason.encode(%{key => val}))
end)
|> Map.new()
end
end)
|> Map.new()
end