diff --git a/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex b/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex index 4a7c5eae0..fb79630e4 100644 --- a/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex +++ b/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex @@ -1,15 +1,19 @@ 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) + meta_data = + html + |> get_elements(key_name, prefix) + |> Enum.reduce(data, fn el, acc -> + attributes = normalize_attributes(el, prefix, key_name, value_name) - Map.merge(acc, attributes) - end) do - {:ok, meta_data} + Map.merge(acc, attributes) + end) + |> maybe_put_title(html) + + if Enum.empty?(meta_data) do + {:error, error_message} else - _e -> {:error, error_message} + {:ok, meta_data} end end @@ -27,4 +31,19 @@ defp normalize_attributes(html_node, prefix, key_name, value_name) do %{String.to_atom(data[key_name]) => data[value_name]} end + + defp maybe_put_title(%{title: _} = meta, _), do: meta + + defp maybe_put_title(meta, html) when meta != %{} do + case get_page_title(html) do + "" -> meta + title -> Map.put_new(meta, :title, title) + end + end + + defp maybe_put_title(meta, _), do: meta + + defp get_page_title(html) do + Floki.find(html, "title") |> Floki.text() + end end diff --git a/test/fixtures/rich_media/non_ogp_embed.html b/test/fixtures/rich_media/non_ogp_embed.html new file mode 100644 index 000000000..62a1d677a --- /dev/null +++ b/test/fixtures/rich_media/non_ogp_embed.html @@ -0,0 +1,1479 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Your Time: | ++ |
MyFreeCams Time: | ++ |
Get Listed on My Wall of Fame
+ElmosEgo 6570 Tks
+Rw2lite 4800 Tks
+Toastboi 2093 Tks
+Acoolahole 1850 Tks
+Gonodog 1299 Tks
+Pumpy_G 800 Tks
+Fowser 690 Tks
+Aquanautic 600 Tks
+Daveonthelake 535 Tks
+Wildpervert2 500 Tks
+Cloud10101 350 Tks
+Branson102 337 Tks
+TheCopperhead 329 Tks
+Mouche99 250 Tks
+The88drummer 233 Tks
+Stringtrees86 199 Tks
+Blazegordon 183 Tks
+Waiting_4 183 Tks
+Sam_mie 170 Tks
+UtterTripe 150 Tks
+Darth_penguin 150 Tks
+Playfullpurv 120 Tks
+Jordnsprings 103 Tks
+Travelinlover 100 Tks
+Da884 100 Tks
+ +Get Listed on My Wall of Fame
+ +LOVE YA ANGELS
+Monday - Outfits Strip
+Tusday - Raffle
+Wensday - Gamblers Night
+Thusday - Orgasmic Vibra or Dildos
+Friday - Wheel/Treat or Trick
+Saturday - Phrase
+Sunday - Keno and Boyfriend choice
+ + + +