2020-02-18 08:57:00 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Mobilizon.Service.RichMedia.Parsers.OGP do
|
|
|
|
@moduledoc """
|
|
|
|
Module to parse OpenGraph data in HTML pages
|
|
|
|
"""
|
|
|
|
require Logger
|
|
|
|
alias Mobilizon.Service.RichMedia.Parsers.MetaTagsParser
|
|
|
|
|
2021-01-22 18:14:52 +01:00
|
|
|
@opengraph_properties [
|
|
|
|
:title,
|
|
|
|
:type,
|
|
|
|
:image,
|
|
|
|
:url,
|
|
|
|
:audio,
|
|
|
|
:description,
|
|
|
|
:determiner,
|
|
|
|
:locale,
|
|
|
|
:"locale:alternate",
|
|
|
|
:site_name,
|
|
|
|
:video,
|
|
|
|
:"image:url",
|
|
|
|
:"image.secure_url",
|
|
|
|
:"image:type",
|
|
|
|
:"image:width",
|
|
|
|
:"image:height",
|
|
|
|
:"image:alt"
|
|
|
|
]
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec parse(String.t(), map()) :: {:ok, map()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def parse(html, data) do
|
|
|
|
Logger.debug("Using OpenGraph card parser")
|
|
|
|
|
|
|
|
with {:ok, data} <-
|
|
|
|
MetaTagsParser.parse(
|
|
|
|
html,
|
|
|
|
data,
|
|
|
|
"og",
|
|
|
|
"No OGP metadata found",
|
2021-01-22 18:14:52 +01:00
|
|
|
:property,
|
|
|
|
:content,
|
|
|
|
@opengraph_properties
|
2020-02-18 08:57:00 +01:00
|
|
|
) do
|
|
|
|
data = transform_tags(data)
|
|
|
|
Logger.debug("Data found with OpenGraph card parser")
|
|
|
|
{:ok, data}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec transform_tags(map()) :: map()
|
2020-02-18 08:57:00 +01:00
|
|
|
defp transform_tags(data) do
|
|
|
|
data
|
2021-01-22 18:14:52 +01:00
|
|
|
|> Enum.reject(fn {_, v} -> is_nil(v) end)
|
|
|
|
|> Map.new()
|
|
|
|
|> Map.update(:image_remote_url, Map.get(data, :image), & &1)
|
|
|
|
|> Map.update(:width, get_integer_value(data, :"image:width"), & &1)
|
|
|
|
|> Map.update(:height, get_integer_value(data, :"image:height"), & &1)
|
2020-06-25 18:47:17 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec get_integer_value(map(), atom()) :: integer() | nil
|
2020-06-25 18:47:17 +02:00
|
|
|
defp get_integer_value(data, key) do
|
2020-06-26 14:42:40 +02:00
|
|
|
with value when not is_nil(value) <- Map.get(data, key),
|
2020-06-25 18:47:17 +02:00
|
|
|
{value, ""} <- Integer.parse(value) do
|
|
|
|
value
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|