2021-11-05 22:23:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class LinkDetailsExtractor
|
|
|
|
include ActionView::Helpers::TagHelper
|
2022-02-08 01:41:17 +00:00
|
|
|
include LanguagesHelper
|
2021-11-05 22:23:05 +00:00
|
|
|
|
2022-02-07 17:16:31 +00: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 12:50:51 +00:00
|
|
|
CDATA_JUNK_PATTERN = %r{^\s*(
|
|
|
|
(/\*\s*<!\[CDATA\[\s*\*/) # Block comment style opening
|
2022-02-07 17:16:31 +00:00
|
|
|
|
|
2023-06-06 12:50:51 +00:00
|
|
|
(//\s*<!\[CDATA\[) # Single-line comment style opening
|
2022-02-07 17:16:31 +00:00
|
|
|
|
|
2023-06-06 12:50:51 +00:00
|
|
|
(/\*\s*\]\]>\s*\*/) # Block comment style closing
|
2022-02-07 17:16:31 +00:00
|
|
|
|
|
2023-06-06 12:50:51 +00:00
|
|
|
(//\s*\]\]>) # Single-line comment style closing
|
|
|
|
)\s*$}x
|
2022-02-07 17:16:31 +00:00
|
|
|
|
2021-11-05 22:23:05 +00:00
|
|
|
class StructuredData
|
2021-11-25 12:07:38 +00:00
|
|
|
SUPPORTED_TYPES = %w(
|
|
|
|
NewsArticle
|
|
|
|
WebPage
|
|
|
|
).freeze
|
|
|
|
|
2021-11-05 22:23:05 +00:00
|
|
|
def initialize(data)
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
def headline
|
|
|
|
json['headline']
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
json['description']
|
|
|
|
end
|
|
|
|
|
2021-11-25 12:07:38 +00:00
|
|
|
def language
|
2023-10-16 13:24:14 +00:00
|
|
|
lang = json['inLanguage']
|
2023-12-07 09:05:08 +00:00
|
|
|
lang = lang.first if lang.is_a?(Array)
|
2023-10-16 13:24:14 +00:00
|
|
|
lang.is_a?(Hash) ? (lang['alternateName'] || lang['name']) : lang
|
2021-11-25 12:07:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
json['@type']
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:23:05 +00: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
|
|
|
|
author['name']
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_url
|
|
|
|
author['url']
|
|
|
|
end
|
|
|
|
|
|
|
|
def publisher_name
|
|
|
|
publisher['name']
|
|
|
|
end
|
|
|
|
|
2021-11-25 12:07:38 +00:00
|
|
|
def publisher_logo
|
|
|
|
publisher.dig('logo', 'url')
|
|
|
|
end
|
|
|
|
|
2022-02-07 17:16:31 +00:00
|
|
|
def valid?
|
|
|
|
json.present?
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:23:05 +00: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 12:07:38 +00:00
|
|
|
def root_array(root)
|
|
|
|
root.is_a?(Array) ? root : [root]
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:23:05 +00:00
|
|
|
def json
|
2021-11-25 12:07:38 +00:00
|
|
|
@json ||= root_array(Oj.load(@data)).find { |obj| SUPPORTED_TYPES.include?(obj['@type']) } || {}
|
2021-11-05 22:23:05 +00: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 13:41:51 +00:00
|
|
|
image_description: image_alt || '',
|
2021-11-05 22:23:05 +00:00
|
|
|
type: type,
|
2021-11-25 12:07:38 +00:00
|
|
|
link_type: link_type,
|
2021-11-05 22:23:05 +00: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 12:07:38 +00:00
|
|
|
language: language,
|
2023-07-25 11:40:35 +00:00
|
|
|
published_at: published_at.presence,
|
2021-11-05 22:23:05 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
player_url.present? ? :video : :link
|
|
|
|
end
|
|
|
|
|
2021-11-25 12:07:38 +00:00
|
|
|
def link_type
|
|
|
|
if structured_data&.type == 'NewsArticle' || opengraph_tag('og:type') == 'article'
|
|
|
|
:article
|
|
|
|
else
|
|
|
|
:unknown
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:23:05 +00:00
|
|
|
def html
|
2023-05-22 10:25:56 +00: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 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def width
|
|
|
|
opengraph_tag('twitter:player:width')
|
|
|
|
end
|
|
|
|
|
|
|
|
def height
|
|
|
|
opengraph_tag('twitter:player:height')
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
2022-02-07 17:16:31 +00:00
|
|
|
html_entities.decode(structured_data&.headline || opengraph_tag('og:title') || document.xpath('//title').map(&:content).first)
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2022-02-07 17:16:31 +00:00
|
|
|
html_entities.decode(structured_data&.description || opengraph_tag('og:description') || meta_tag('description'))
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
2023-07-24 11:47:28 +00:00
|
|
|
def published_at
|
|
|
|
structured_data&.date_published || opengraph_tag('article:published_time')
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:23:05 +00:00
|
|
|
def image
|
|
|
|
valid_url_or_nil(opengraph_tag('og:image'))
|
|
|
|
end
|
|
|
|
|
2023-08-03 13:41:51 +00:00
|
|
|
def image_alt
|
|
|
|
opengraph_tag('og:image:alt')
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:23:05 +00:00
|
|
|
def canonical_url
|
2022-02-07 17:16:31 +00:00
|
|
|
valid_url_or_nil(link_tag('canonical') || opengraph_tag('og:url'), same_origin_only: true) || @original_url.to_s
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_name
|
2022-02-07 17:16:31 +00:00
|
|
|
html_entities.decode(structured_data&.publisher_name || opengraph_tag('og:site_name'))
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_url
|
|
|
|
valid_url_or_nil(host_to_url(opengraph_tag('og:site')))
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2022-02-07 17:16:31 +00:00
|
|
|
html_entities.decode(structured_data&.author_name || opengraph_tag('og:author') || opengraph_tag('og:author:username'))
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_url
|
|
|
|
structured_data&.author_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def embed_url
|
|
|
|
valid_url_or_nil(opengraph_tag('twitter:player:stream'))
|
|
|
|
end
|
|
|
|
|
2021-11-25 12:07:38 +00:00
|
|
|
def language
|
2023-02-20 01:28:40 +00:00
|
|
|
valid_locale_or_nil(structured_data&.language || opengraph_tag('og:locale') || document.xpath('//html').pick('lang'))
|
2021-11-25 12:07:38 +00: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 22:23:05 +00: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 12:50:51 +00:00
|
|
|
str.start_with?(%r{https?://}) ? str : "http://#{str}"
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_url_or_nil(str, same_origin_only: false)
|
2022-03-25 18:31:35 +00:00
|
|
|
return if str.blank? || str == 'null'
|
2021-11-05 22:23:05 +00: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 01:28:40 +00:00
|
|
|
document.xpath("//link[@rel=\"#{name}\"]").pick('href')
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def opengraph_tag(name)
|
2023-02-20 01:28:40 +00:00
|
|
|
document.xpath("//meta[@property=\"#{name}\" or @name=\"#{name}\"]").pick('content')
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def meta_tag(name)
|
2023-02-20 01:28:40 +00:00
|
|
|
document.xpath("//meta[@name=\"#{name}\"]").pick('content')
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def structured_data
|
2023-02-18 22:09:40 +00: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 17:16:31 +00:00
|
|
|
|
2023-02-18 22:09:40 +00:00
|
|
|
next if json_ld.blank?
|
2022-02-07 17:16:31 +00:00
|
|
|
|
2023-02-18 22:09:40 +00:00
|
|
|
structured_data = StructuredData.new(html_entities.decode(json_ld))
|
2022-02-07 17:16:31 +00:00
|
|
|
|
2023-02-18 22:09:40 +00:00
|
|
|
next unless structured_data.valid?
|
2022-02-07 17:16:31 +00:00
|
|
|
|
2023-02-18 22:09:40 +00:00
|
|
|
structured_data
|
|
|
|
rescue Oj::ParseError, EncodingError
|
|
|
|
Rails.logger.debug { "Invalid JSON-LD in #{@original_url}" }
|
|
|
|
next
|
|
|
|
end.first
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def document
|
|
|
|
@document ||= Nokogiri::HTML(@html, nil, encoding)
|
|
|
|
end
|
|
|
|
|
|
|
|
def encoding
|
|
|
|
@encoding ||= begin
|
|
|
|
guess = detector.detect(@html, @html_charset)
|
|
|
|
guess&.fetch(:confidence, 0).to_i > 60 ? guess&.fetch(:encoding, nil) : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detector
|
|
|
|
@detector ||= CharlockHolmes::EncodingDetector.new.tap do |detector|
|
|
|
|
detector.strip_tags = true
|
|
|
|
end
|
|
|
|
end
|
2022-02-07 17:16:31 +00:00
|
|
|
|
|
|
|
def html_entities
|
|
|
|
@html_entities ||= HTMLEntities.new
|
|
|
|
end
|
2021-11-05 22:23:05 +00:00
|
|
|
end
|