Add OGP parser

This commit is contained in:
Maxim Filippov 2019-01-01 23:26:40 +03:00
commit 2aab4e03c3
7 changed files with 86 additions and 1 deletions

View file

@ -0,0 +1,3 @@
defmodule Pleroma.Web.RichMedia.Data do
defstruct [:title, :type, :image, :url, :description]
end

View file

@ -0,0 +1,14 @@
defmodule Pleroma.Web.RichMedia.Parser do
@parsers [Pleroma.Web.RichMedia.Parsers.OGP]
def parse(url) do
{:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
Enum.reduce_while(@parsers, %Pleroma.Web.RichMedia.Data{}, fn parser, acc ->
case parser.parse(html, acc) do
{:ok, data} -> {:halt, data}
{:error, _msg} -> {:cont, acc}
end
end)
end
end

View file

@ -0,0 +1,30 @@
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
defp normalize_attributes(tuple) do
{_tag, attributes, _children} = tuple
data =
Enum.into(attributes, %{}, fn {name, value} ->
{name, String.trim_leading(value, "og:")}
end)
%{String.to_atom(data["property"]) => data["content"]}
end
end