Merge branch 'feature/add-twitter-card-parser' into 'develop'

Add Twitter Card parser

See merge request pleroma/pleroma!643
This commit is contained in:
kaniini 2019-01-10 18:09:56 +00:00
commit 9be539f0c1
6 changed files with 72 additions and 27 deletions

View File

@ -1,5 +1,5 @@
defmodule Pleroma.Web.RichMedia.Parser do
@parsers [Pleroma.Web.RichMedia.Parsers.OGP]
@parsers [Pleroma.Web.RichMedia.Parsers.OGP, Pleroma.Web.RichMedia.Parsers.TwitterCard]
if Mix.env() == :test do
def parse(url), do: parse_url(url)

View File

@ -0,0 +1,30 @@
defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
def parse(html, data, prefix, error_message, key_name, value_name \\ "content") do
with elements = [_ | _] <- get_elements(html, key_name, prefix),
meta_data =
Enum.reduce(elements, data, fn el, acc ->
attributes = normalize_attributes(el, prefix, key_name, value_name)
Map.merge(acc, attributes)
end) do
{:ok, meta_data}
else
_e -> {:error, error_message}
end
end
defp get_elements(html, key_name, prefix) do
html |> Floki.find("meta[#{key_name}^='#{prefix}:']")
end
defp normalize_attributes(html_node, prefix, key_name, value_name) do
{_tag, attributes, _children} = html_node
data =
Enum.into(attributes, %{}, fn {name, value} ->
{name, String.trim_leading(value, "#{prefix}:")}
end)
%{String.to_atom(data[key_name]) => data[value_name]}
end
end

View File

@ -1,30 +1,11 @@
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(html_node) do
{_tag, attributes, _children} = html_node
data =
Enum.into(attributes, %{}, fn {name, value} ->
{name, String.trim_leading(value, "og:")}
end)
%{String.to_atom(data["property"]) => data["content"]}
Pleroma.Web.RichMedia.Parsers.MetaTagsParser.parse(
html,
data,
"og",
"No OGP metadata found",
"property"
)
end
end

View File

@ -0,0 +1,11 @@
defmodule Pleroma.Web.RichMedia.Parsers.TwitterCard do
def parse(html, data) do
Pleroma.Web.RichMedia.Parsers.MetaTagsParser.parse(
html,
data,
"twitter",
"No twitter card metadata found",
"name"
)
end
end

View File

@ -0,0 +1,5 @@
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@flickr" />
<meta name="twitter:title" content="Small Island Developing States Photo Submission" />
<meta name="twitter:description" content="View the album on Flickr." />
<meta name="twitter:image" content="https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg" />

View File

@ -9,6 +9,12 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
%{
method: :get,
url: "http://example.com/twitter-card"
} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
%{method: :get, url: "http://example.com/empty"} ->
%Tesla.Env{status: 200, body: "hello"}
end)
@ -30,4 +36,16 @@ test "parses ogp" do
url: "http://www.imdb.com/title/tt0117500/"
}}
end
test "parses twitter card" do
assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==
{:ok,
%{
card: "summary",
site: "@flickr",
image: "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
title: "Small Island Developing States Photo Submission",
description: "View the album on Flickr."
}}
end
end