15 行
841 B
Ruby
15 行
841 B
Ruby
class PostMetadataFetcher
|
|
def self.fetch raw_url
|
|
uri, = Preview::UrlSafety.validate(raw_url)
|
|
response = Preview::HttpFetcher.fetch(uri.to_s, max_bytes: Preview::ThumbnailFetcher::HTML_MAX_BYTES)
|
|
metadata = Preview::HtmlMetadataExtractor.extract(response)
|
|
document = Nokogiri::HTML.parse(response.body)
|
|
content = -> name { document.at_css("meta[property='#{ name }'], meta[name='#{ name }']")&.[]('content')&.strip.presence }
|
|
duration = content.call('og:video:duration') || content.call('video:duration')
|
|
{ title: metadata[:title],
|
|
thumbnail_base: Preview::KnownSiteExtractor.thumbnail_url(uri) || metadata[:image_url],
|
|
original_created_from: content.call('article:published_time') || content.call('date'),
|
|
duration: duration&.to_f&.then { _1.positive? ? (_1 * 1_000).round : nil } }
|
|
end
|
|
end
|