Add OEmbed parser
This commit is contained in:
parent
19b6a82393
commit
b8a77c5d70
5 changed files with 77 additions and 1 deletions
|
|
@ -1,5 +1,9 @@
|
|||
defmodule Pleroma.Web.RichMedia.Parser do
|
||||
@parsers [Pleroma.Web.RichMedia.Parsers.OGP, Pleroma.Web.RichMedia.Parsers.TwitterCard]
|
||||
@parsers [
|
||||
Pleroma.Web.RichMedia.Parsers.OGP,
|
||||
Pleroma.Web.RichMedia.Parsers.TwitterCard,
|
||||
Pleroma.Web.RichMedia.Parsers.OEmbed
|
||||
]
|
||||
|
||||
if Mix.env() == :test do
|
||||
def parse(url), do: parse_url(url)
|
||||
|
|
|
|||
27
lib/pleroma/web/rich_media/parsers/oembed_parser.ex
Normal file
27
lib/pleroma/web/rich_media/parsers/oembed_parser.ex
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
|
||||
def parse(html, _data) do
|
||||
with elements = [_ | _] <- get_discovery_data(html),
|
||||
{:ok, oembed_url} <- get_oembed_url(elements),
|
||||
{:ok, oembed_data} <- get_oembed_data(oembed_url) do
|
||||
{:ok, oembed_data}
|
||||
else
|
||||
_e -> {:error, "No OEmbed data found"}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_discovery_data(html) do
|
||||
html |> Floki.find("link[type='application/json+oembed']")
|
||||
end
|
||||
|
||||
defp get_oembed_url(nodes) do
|
||||
{"link", attributes, _children} = nodes |> hd()
|
||||
|
||||
{:ok, Enum.into(attributes, %{})["href"]}
|
||||
end
|
||||
|
||||
defp get_oembed_data(url) do
|
||||
{:ok, %Tesla.Env{body: json}} = Pleroma.HTTP.get(url)
|
||||
|
||||
{:ok, Poison.decode!(json)}
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue