2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-21 14:29:04 -05:00
|
|
|
module RetrieveTitle
|
2018-01-28 22:36:52 -06:00
|
|
|
CRAWL_TIMEOUT = 1
|
2023-12-01 01:03:06 -06:00
|
|
|
UNRECOVERABLE_ERRORS = [
|
|
|
|
Net::ReadTimeout,
|
|
|
|
FinalDestination::SSRFError,
|
|
|
|
FinalDestination::UrlEncodingError,
|
|
|
|
]
|
2017-07-21 14:29:04 -05:00
|
|
|
|
2022-05-23 05:52:06 -05:00
|
|
|
def self.crawl(url, max_redirects: nil, initial_https_redirect_ignore_limit: false)
|
|
|
|
fetch_title(
|
|
|
|
url,
|
|
|
|
max_redirects: max_redirects,
|
2023-01-09 06:10:19 -06:00
|
|
|
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
|
2022-05-23 05:52:06 -05:00
|
|
|
)
|
2023-12-01 01:03:06 -06:00
|
|
|
rescue *UNRECOVERABLE_ERRORS
|
|
|
|
# ¯\_(ツ)_/¯
|
2017-07-21 14:29:04 -05:00
|
|
|
end
|
|
|
|
|
2021-01-04 13:32:08 -06:00
|
|
|
def self.extract_title(html, encoding = nil)
|
2017-07-21 14:29:04 -05:00
|
|
|
title = nil
|
2023-01-09 06:10:19 -06:00
|
|
|
return nil if html =~ /<title>/ && html !~ %r{</title>}
|
2017-07-21 14:29:04 -05:00
|
|
|
|
2022-08-23 00:03:57 -05:00
|
|
|
doc = nil
|
|
|
|
begin
|
2023-01-09 06:10:19 -06:00
|
|
|
doc = Nokogiri.HTML5(html, nil, encoding)
|
2022-08-23 00:03:57 -05:00
|
|
|
rescue ArgumentError
|
2022-08-23 00:14:24 -05:00
|
|
|
# invalid HTML (Eg: too many attributes, status tree too deep) - ignore
|
|
|
|
# Error in nokogumbo is not specialized, uses generic ArgumentError
|
|
|
|
# see: https://www.rubydoc.info/gems/nokogiri/Nokogiri/HTML5#label-Error+reporting
|
2022-08-23 00:03:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if doc
|
2023-01-09 06:10:19 -06:00
|
|
|
title = doc.at("title")&.inner_text
|
2017-08-02 13:27:21 -05:00
|
|
|
|
2017-09-28 08:29:50 -05:00
|
|
|
# A horrible hack - YouTube uses `document.title` to populate the title
|
|
|
|
# for some reason. For any other site than YouTube this wouldn't be worth it.
|
|
|
|
if title == "YouTube" && html =~ /document\.title *= *"(.*)";/
|
2023-01-20 12:52:49 -06:00
|
|
|
title = Regexp.last_match[1].sub(/ - YouTube\z/, "")
|
2017-09-28 08:29:50 -05:00
|
|
|
end
|
|
|
|
|
2017-08-02 13:27:21 -05:00
|
|
|
if !title && node = doc.at('meta[property="og:title"]')
|
2023-01-09 06:10:19 -06:00
|
|
|
title = node["content"]
|
2017-07-21 14:29:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if title.present?
|
2023-01-09 06:10:19 -06:00
|
|
|
title.gsub!(/\n/, " ")
|
|
|
|
title.gsub!(/ +/, " ")
|
2017-07-21 14:29:04 -05:00
|
|
|
title.strip!
|
|
|
|
return title
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def self.max_chunk_size(uri)
|
2022-02-09 15:53:27 -06:00
|
|
|
# Exception for sites that leave the title until very late.
|
2023-01-20 12:52:49 -06:00
|
|
|
if uri.host =~
|
|
|
|
/(^|\.)amazon\.(com|ca|co\.uk|es|fr|de|it|com\.au|com\.br|cn|in|co\.jp|com\.mx)\z/
|
2023-01-09 06:10:19 -06:00
|
|
|
return 500
|
|
|
|
end
|
2023-01-20 12:52:49 -06:00
|
|
|
return 300 if uri.host =~ /(^|\.)youtube\.com\z/ || uri.host =~ /(^|\.)youtu\.be\z/
|
|
|
|
return 50 if uri.host =~ /(^|\.)github\.com\z/
|
2017-07-21 14:29:04 -05:00
|
|
|
|
2021-09-03 02:45:58 -05:00
|
|
|
# default is 20k
|
|
|
|
20
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2018-01-28 22:36:52 -06:00
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
# Fetch the beginning of a HTML document at a url
|
2022-05-23 05:52:06 -05:00
|
|
|
def self.fetch_title(url, max_redirects: nil, initial_https_redirect_ignore_limit: false)
|
2023-01-09 06:10:19 -06:00
|
|
|
fd =
|
|
|
|
FinalDestination.new(
|
|
|
|
url,
|
|
|
|
timeout: CRAWL_TIMEOUT,
|
|
|
|
stop_at_blocked_pages: true,
|
|
|
|
max_redirects: max_redirects,
|
|
|
|
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
|
2023-12-03 19:36:42 -06:00
|
|
|
headers: {
|
|
|
|
Accept: "text/html,*/*",
|
|
|
|
},
|
2023-01-09 06:10:19 -06:00
|
|
|
)
|
2018-01-28 22:36:52 -06:00
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
current = nil
|
|
|
|
title = nil
|
2021-01-04 13:32:08 -06:00
|
|
|
encoding = nil
|
2018-01-28 22:36:52 -06:00
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
fd.get do |_response, chunk, uri|
|
2021-06-24 09:23:39 -05:00
|
|
|
unless Net::HTTPRedirection === _response
|
2022-03-22 13:13:27 -05:00
|
|
|
throw :done if uri.blank?
|
|
|
|
|
2021-06-24 09:23:39 -05:00
|
|
|
if current
|
|
|
|
current << chunk
|
|
|
|
else
|
|
|
|
current = chunk
|
|
|
|
end
|
2018-01-28 22:36:52 -06:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if !encoding && content_type = _response["content-type"]&.strip&.downcase
|
2021-06-24 09:23:39 -05:00
|
|
|
if content_type =~ /charset="?([a-z0-9_-]+)"?/
|
|
|
|
encoding = Regexp.last_match(1)
|
2023-01-09 06:10:19 -06:00
|
|
|
encoding = nil if !Encoding.list.map(&:name).map(&:downcase).include?(encoding)
|
2021-01-04 13:32:08 -06:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
|
2021-06-24 09:23:39 -05:00
|
|
|
max_size = max_chunk_size(uri) * 1024
|
|
|
|
title = extract_title(current, encoding)
|
|
|
|
throw :done if title || max_size < current.length
|
|
|
|
end
|
2017-07-21 14:29:04 -05:00
|
|
|
end
|
2019-11-14 14:10:51 -06:00
|
|
|
title
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2017-07-21 14:29:04 -05:00
|
|
|
end
|