2021-11-05 23:23:05 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class LinkDetailsExtractor
|
|
|
|
include ActionView::Helpers::TagHelper
|
2022-02-08 02:41:17 +01:00
|
|
|
include LanguagesHelper
|
2021-11-05 23:23:05 +01:00
|
|
|
|
2022-02-07 18:16:31 +01:00
|
|
|
# Some publications wrap their JSON-LD data in their <script> tags
|
|
|
|
# in commented-out CDATA blocks, they need to be removed before
|
|
|
|
# attempting to parse JSON
|
2023-06-06 14:50:51 +02:00
|
|
|
CDATA_JUNK_PATTERN = %r{^\s*(
|
|
|
|
(/\*\s*<!\[CDATA\[\s*\*/) # Block comment style opening
|
2022-02-07 18:16:31 +01:00
|
|
|
|
|
2023-06-06 14:50:51 +02:00
|
|
|
(//\s*<!\[CDATA\[) # Single-line comment style opening
|
2022-02-07 18:16:31 +01:00
|
|
|
|
|
2023-06-06 14:50:51 +02:00
|
|
|
(/\*\s*\]\]>\s*\*/) # Block comment style closing
|
2022-02-07 18:16:31 +01:00
|
|
|
|
|
2023-06-06 14:50:51 +02:00
|
|
|
(//\s*\]\]>) # Single-line comment style closing
|
|
|
|
)\s*$}x
|
2022-02-07 18:16:31 +01:00
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
class StructuredData
|
2021-11-25 13:07:38 +01:00
|
|
|
SUPPORTED_TYPES = %w(
|
|
|
|
NewsArticle
|
|
|
|
WebPage
|
|
|
|
).freeze
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def initialize(data)
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
def headline
|
|
|
|
json['headline']
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
json['description']
|
|
|
|
end
|
|
|
|
|
2021-11-25 13:07:38 +01:00
|
|
|
def language
|
2023-10-16 15:24:14 +02:00
|
|
|
lang = json['inLanguage']
|
2023-12-07 10:05:08 +01:00
|
|
|
lang = lang.first if lang.is_a?(Array)
|
2023-10-16 15:24:14 +02:00
|
|
|
lang.is_a?(Hash) ? (lang['alternateName'] || lang['name']) : lang
|
2021-11-25 13:07:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
json['@type']
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def image
|
|
|
|
obj = first_of_value(json['image'])
|
|
|
|
|
|
|
|
return obj['url'] if obj.is_a?(Hash)
|
|
|
|
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
|
|
|
|
def date_published
|
|
|
|
json['datePublished']
|
|
|
|
end
|
|
|
|
|
|
|
|
def date_modified
|
|
|
|
json['dateModified']
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2024-07-08 18:04:36 +02:00
|
|
|
name = author['name']
|
|
|
|
name.is_a?(Array) ? name.join(', ') : name
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_url
|
|
|
|
author['url']
|
|
|
|
end
|
|
|
|
|
|
|
|
def publisher_name
|
|
|
|
publisher['name']
|
|
|
|
end
|
|
|
|
|
2021-11-25 13:07:38 +01:00
|
|
|
def publisher_logo
|
|
|
|
publisher.dig('logo', 'url')
|
|
|
|
end
|
|
|
|
|
2022-02-07 18:16:31 +01:00
|
|
|
def valid?
|
|
|
|
json.present?
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def author
|
|
|
|
first_of_value(json['author']) || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def publisher
|
|
|
|
first_of_value(json['publisher']) || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_of_value(arr)
|
|
|
|
arr.is_a?(Array) ? arr.first : arr
|
|
|
|
end
|
|
|
|
|
2021-11-25 13:07:38 +01:00
|
|
|
def root_array(root)
|
|
|
|
root.is_a?(Array) ? root : [root]
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def json
|
2024-08-06 09:52:47 +02:00
|
|
|
@json ||= root_array(Oj.load(@data))
|
|
|
|
.map { |node| JSON::LD::API.compact(node, 'https://schema.org') }
|
|
|
|
.find { |node| SUPPORTED_TYPES.include?(node['type']) } || {}
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(original_url, html, html_charset)
|
|
|
|
@original_url = Addressable::URI.parse(original_url)
|
|
|
|
@html = html
|
|
|
|
@html_charset = html_charset
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_preview_card_attributes
|
|
|
|
{
|
|
|
|
title: title || '',
|
|
|
|
description: description || '',
|
|
|
|
image_remote_url: image,
|
2023-08-03 15:41:51 +02:00
|
|
|
image_description: image_alt || '',
|
2021-11-05 23:23:05 +01:00
|
|
|
type: type,
|
2021-11-25 13:07:38 +01:00
|
|
|
link_type: link_type,
|
2021-11-05 23:23:05 +01:00
|
|
|
width: width || 0,
|
|
|
|
height: height || 0,
|
|
|
|
html: html || '',
|
|
|
|
provider_name: provider_name || '',
|
|
|
|
provider_url: provider_url || '',
|
|
|
|
author_name: author_name || '',
|
|
|
|
author_url: author_url || '',
|
|
|
|
embed_url: embed_url || '',
|
2021-11-25 13:07:38 +01:00
|
|
|
language: language,
|
2023-07-25 13:40:35 +02:00
|
|
|
published_at: published_at.presence,
|
2021-11-05 23:23:05 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
player_url.present? ? :video : :link
|
|
|
|
end
|
|
|
|
|
2021-11-25 13:07:38 +01:00
|
|
|
def link_type
|
|
|
|
if structured_data&.type == 'NewsArticle' || opengraph_tag('og:type') == 'article'
|
|
|
|
:article
|
|
|
|
else
|
|
|
|
:unknown
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def html
|
2023-05-22 12:25:56 +02:00
|
|
|
player_url.present? ? content_tag(:iframe, nil, src: player_url, width: width, height: height, allowfullscreen: 'true', allowtransparency: 'true', scrolling: 'no', frameborder: '0') : nil
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def width
|
|
|
|
opengraph_tag('twitter:player:width')
|
|
|
|
end
|
|
|
|
|
|
|
|
def height
|
|
|
|
opengraph_tag('twitter:player:height')
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
2024-07-05 15:28:52 +02:00
|
|
|
html_entities_decode(structured_data&.headline || opengraph_tag('og:title') || document.xpath('//title').map(&:content).first)&.strip
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2024-07-05 13:54:38 +02:00
|
|
|
html_entities_decode(structured_data&.description || opengraph_tag('og:description') || meta_tag('description'))
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
2023-07-24 13:47:28 +02:00
|
|
|
def published_at
|
|
|
|
structured_data&.date_published || opengraph_tag('article:published_time')
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def image
|
|
|
|
valid_url_or_nil(opengraph_tag('og:image'))
|
|
|
|
end
|
|
|
|
|
2023-08-03 15:41:51 +02:00
|
|
|
def image_alt
|
|
|
|
opengraph_tag('og:image:alt')
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def canonical_url
|
2022-02-07 18:16:31 +01:00
|
|
|
valid_url_or_nil(link_tag('canonical') || opengraph_tag('og:url'), same_origin_only: true) || @original_url.to_s
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_name
|
2024-07-05 13:54:38 +02:00
|
|
|
html_entities_decode(structured_data&.publisher_name || opengraph_tag('og:site_name'))
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_url
|
|
|
|
valid_url_or_nil(host_to_url(opengraph_tag('og:site')))
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2024-07-05 13:54:38 +02:00
|
|
|
html_entities_decode(structured_data&.author_name || opengraph_tag('og:author') || opengraph_tag('og:author:username'))
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_url
|
|
|
|
structured_data&.author_url
|
|
|
|
end
|
|
|
|
|
2024-05-29 01:34:33 +02:00
|
|
|
def author_account
|
|
|
|
opengraph_tag('fediverse:creator')
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def embed_url
|
|
|
|
valid_url_or_nil(opengraph_tag('twitter:player:stream'))
|
|
|
|
end
|
|
|
|
|
2021-11-25 13:07:38 +01:00
|
|
|
def language
|
2023-02-20 02:28:40 +01:00
|
|
|
valid_locale_or_nil(structured_data&.language || opengraph_tag('og:locale') || document.xpath('//html').pick('lang'))
|
2021-11-25 13:07:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def icon
|
|
|
|
valid_url_or_nil(structured_data&.publisher_icon || link_tag('apple-touch-icon') || link_tag('shortcut icon'))
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def player_url
|
|
|
|
valid_url_or_nil(opengraph_tag('twitter:player'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def host_to_url(str)
|
|
|
|
return if str.blank?
|
|
|
|
|
2023-06-06 14:50:51 +02:00
|
|
|
str.start_with?(%r{https?://}) ? str : "http://#{str}"
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_url_or_nil(str, same_origin_only: false)
|
2022-03-25 19:31:35 +01:00
|
|
|
return if str.blank? || str == 'null'
|
2021-11-05 23:23:05 +01:00
|
|
|
|
|
|
|
url = @original_url + Addressable::URI.parse(str)
|
|
|
|
|
|
|
|
return if url.host.blank? || !%w(http https).include?(url.scheme) || (same_origin_only && url.host != @original_url.host)
|
|
|
|
|
|
|
|
url.to_s
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_tag(name)
|
2023-02-20 02:28:40 +01:00
|
|
|
document.xpath("//link[@rel=\"#{name}\"]").pick('href')
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def opengraph_tag(name)
|
2023-02-20 02:28:40 +01:00
|
|
|
document.xpath("//meta[@property=\"#{name}\" or @name=\"#{name}\"]").pick('content')
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def meta_tag(name)
|
2023-02-20 02:28:40 +01:00
|
|
|
document.xpath("//meta[@name=\"#{name}\"]").pick('content')
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def structured_data
|
2023-02-18 23:09:40 +01:00
|
|
|
# Some publications have more than one JSON-LD definition on the page,
|
|
|
|
# and some of those definitions aren't valid JSON either, so we have
|
|
|
|
# to loop through here until we find something that is the right type
|
|
|
|
# and doesn't break
|
|
|
|
@structured_data ||= document.xpath('//script[@type="application/ld+json"]').filter_map do |element|
|
|
|
|
json_ld = element.content&.gsub(CDATA_JUNK_PATTERN, '')
|
2022-02-07 18:16:31 +01:00
|
|
|
|
2023-02-18 23:09:40 +01:00
|
|
|
next if json_ld.blank?
|
2022-02-07 18:16:31 +01:00
|
|
|
|
2024-07-05 13:54:38 +02:00
|
|
|
structured_data = StructuredData.new(html_entities_decode(json_ld))
|
2022-02-07 18:16:31 +01:00
|
|
|
|
2023-02-18 23:09:40 +01:00
|
|
|
next unless structured_data.valid?
|
2022-02-07 18:16:31 +01:00
|
|
|
|
2023-02-18 23:09:40 +01:00
|
|
|
structured_data
|
|
|
|
rescue Oj::ParseError, EncodingError
|
|
|
|
Rails.logger.debug { "Invalid JSON-LD in #{@original_url}" }
|
|
|
|
next
|
|
|
|
end.first
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def document
|
2024-06-21 14:51:10 +02:00
|
|
|
@document ||= detect_encoding_and_parse_document
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
2024-06-21 14:51:10 +02:00
|
|
|
def detect_encoding_and_parse_document
|
2024-07-10 16:25:39 +02:00
|
|
|
[detect_encoding, nil, header_encoding].uniq.each do |encoding|
|
2024-06-21 14:51:10 +02:00
|
|
|
document = Nokogiri::HTML(@html, nil, encoding)
|
|
|
|
return document if document.to_s.valid_encoding?
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
2024-07-05 13:54:38 +02:00
|
|
|
Nokogiri::HTML(@html, nil, 'UTF-8')
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|
|
|
|
|
2024-06-21 14:51:10 +02:00
|
|
|
def detect_encoding
|
|
|
|
guess = detector.detect(@html, @html_charset)
|
|
|
|
guess&.fetch(:confidence, 0).to_i > 60 ? guess&.fetch(:encoding, nil) : nil
|
|
|
|
end
|
|
|
|
|
2024-07-10 16:25:39 +02:00
|
|
|
def header_encoding
|
|
|
|
Encoding.find(@html_charset).name if @html_charset
|
|
|
|
rescue ArgumentError
|
|
|
|
# Encoding from HTTP header is not recognized by ruby
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2021-11-05 23:23:05 +01:00
|
|
|
def detector
|
|
|
|
@detector ||= CharlockHolmes::EncodingDetector.new.tap do |detector|
|
|
|
|
detector.strip_tags = true
|
|
|
|
end
|
|
|
|
end
|
2022-02-07 18:16:31 +01:00
|
|
|
|
2024-07-05 13:54:38 +02:00
|
|
|
def html_entities_decode(string)
|
|
|
|
return if string.nil?
|
|
|
|
|
2024-07-08 18:04:36 +02:00
|
|
|
unicode_string = string.to_s.encode('UTF-8')
|
2024-07-05 13:54:38 +02:00
|
|
|
raise EncodingError, 'cannot convert string to valid UTF-8' unless unicode_string.valid_encoding?
|
|
|
|
|
|
|
|
html_entities.decode(unicode_string)
|
|
|
|
end
|
|
|
|
|
2022-02-07 18:16:31 +01:00
|
|
|
def html_entities
|
2024-05-05 04:24:10 +02:00
|
|
|
@html_entities ||= HTMLEntities.new(:expanded)
|
2022-02-07 18:16:31 +01:00
|
|
|
end
|
2021-11-05 23:23:05 +01:00
|
|
|
end
|