akkoma/lib/pleroma/web/rich_media/parsers/ogp.ex

31 lines
769 B
Elixir
Raw Normal View History

2019-01-01 13:26:40 -07:00
defmodule Pleroma.Web.RichMedia.Parsers.OGP do
def parse(html, data) do
with elements = [_ | _] <- get_elements(html),
ogp_data =
Enum.reduce(elements, data, fn el, acc ->
attributes = normalize_attributes(el)
Map.merge(acc, attributes)
end) do
{:ok, ogp_data}
else
_e -> {:error, "No OGP metadata found"}
end
end
defp get_elements(html) do
html |> Floki.find("meta[property^='og:']")
end
2019-01-01 13:29:47 -07:00
defp normalize_attributes(html_node) do
{_tag, attributes, _children} = html_node
2019-01-01 13:26:40 -07:00
data =
Enum.into(attributes, %{}, fn {name, value} ->
{name, String.trim_leading(value, "og:")}
end)
%{String.to_atom(data["property"]) => data["content"]}
end
end