2014-04-05 14:47:25 -04:00
|
|
|
require 'open-uri'
|
|
|
|
require 'nokogiri'
|
|
|
|
require 'excon'
|
2017-05-22 12:23:04 -04:00
|
|
|
require 'final_destination'
|
2014-04-05 14:47:25 -04:00
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class CrawlTopicLink < Jobs::Base
|
|
|
|
|
2015-03-22 21:20:50 -04:00
|
|
|
class ReadEnough < StandardError; end
|
2014-04-05 14:47:25 -04:00
|
|
|
|
2014-04-08 16:39:47 -04:00
|
|
|
def self.max_chunk_size(uri)
|
|
|
|
# Amazon leaves the title until very late. Normally it's a bad idea to make an exception for
|
|
|
|
# one host but amazon is a big one.
|
2014-05-06 13:19:32 -04:00
|
|
|
return 80 if uri.host =~ /amazon\.(com|ca|co\.uk|es|fr|de|it|com\.au|com\.br|cn|in|co\.jp|com\.mx)$/
|
2014-04-08 16:39:47 -04:00
|
|
|
|
|
|
|
# Default is 10k
|
|
|
|
10
|
|
|
|
end
|
|
|
|
|
2014-04-05 14:47:25 -04:00
|
|
|
# Fetch the beginning of a HTML document at a url
|
|
|
|
def self.fetch_beginning(url)
|
2014-04-07 14:38:18 -04:00
|
|
|
# Never crawl in test mode
|
|
|
|
return if Rails.env.test?
|
|
|
|
|
2017-05-22 12:23:04 -04:00
|
|
|
fd = FinalDestination.new(url)
|
|
|
|
uri = fd.resolve
|
2014-04-05 14:47:25 -04:00
|
|
|
return "" unless uri
|
|
|
|
|
|
|
|
result = ""
|
2014-08-14 17:54:55 -04:00
|
|
|
streamer = lambda do |chunk, _, _|
|
2014-04-05 14:47:25 -04:00
|
|
|
result << chunk
|
|
|
|
|
|
|
|
# Using exceptions for flow control is really bad, but there really seems to
|
|
|
|
# be no sane way to get a stream to stop reading in Excon (or Net::HTTP for
|
|
|
|
# that matter!)
|
2014-04-08 16:39:47 -04:00
|
|
|
raise ReadEnough.new if result.size > (CrawlTopicLink.max_chunk_size(uri) * 1024)
|
2014-04-05 14:47:25 -04:00
|
|
|
end
|
2017-05-22 12:23:04 -04:00
|
|
|
Excon.get(uri.to_s, response_block: streamer, read_timeout: 20, headers: fd.request_headers)
|
2014-04-05 14:47:25 -04:00
|
|
|
result
|
|
|
|
|
2015-05-27 14:29:52 -04:00
|
|
|
rescue Excon::Errors::SocketError => ex
|
|
|
|
return result if ex.socket_error.is_a?(ReadEnough)
|
|
|
|
raise
|
2014-04-05 14:47:25 -04:00
|
|
|
rescue ReadEnough
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
raise Discourse::InvalidParameters.new(:topic_link_id) unless args[:topic_link_id].present?
|
2014-04-07 14:38:18 -04:00
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
topic_link = TopicLink.find_by(id: args[:topic_link_id], internal: false, crawled_at: nil)
|
2014-04-17 14:00:22 -04:00
|
|
|
return if topic_link.blank?
|
|
|
|
|
|
|
|
# Look for a topic embed for the URL. If it exists, use its title and don't crawl
|
|
|
|
topic_embed = TopicEmbed.where(embed_url: topic_link.url).includes(:topic).references(:topic).first
|
2015-05-06 02:53:28 -04:00
|
|
|
# topic could be deleted, so skip
|
|
|
|
if topic_embed && topic_embed.topic
|
2014-04-17 14:00:22 -04:00
|
|
|
TopicLink.where(id: topic_link.id).update_all(['title = ?, crawled_at = CURRENT_TIMESTAMP', topic_embed.topic.title[0..255]])
|
|
|
|
return
|
|
|
|
end
|
2014-04-07 16:03:47 -04:00
|
|
|
|
2014-04-17 14:00:22 -04:00
|
|
|
begin
|
2014-04-07 16:03:47 -04:00
|
|
|
crawled = false
|
|
|
|
|
2014-04-10 13:45:13 -04:00
|
|
|
# Special case: Images
|
|
|
|
# If the link is to an image, put the filename as the title
|
2017-06-22 06:54:42 -04:00
|
|
|
if FileHelper.is_image?(topic_link.url)
|
2014-04-10 13:45:13 -04:00
|
|
|
uri = URI(topic_link.url)
|
|
|
|
filename = File.basename(uri.path)
|
|
|
|
crawled = (TopicLink.where(id: topic_link.id).update_all(["title = ?, crawled_at = CURRENT_TIMESTAMP", filename]) == 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless crawled
|
|
|
|
# Fetch the beginning of the document to find the title
|
|
|
|
result = CrawlTopicLink.fetch_beginning(topic_link.url)
|
|
|
|
doc = Nokogiri::HTML(result)
|
|
|
|
if doc
|
|
|
|
title = doc.at('title').try(:inner_text)
|
2014-04-07 16:03:47 -04:00
|
|
|
if title.present?
|
2014-04-10 13:45:13 -04:00
|
|
|
title.gsub!(/\n/, ' ')
|
|
|
|
title.gsub!(/ +/, ' ')
|
|
|
|
title.strip!
|
|
|
|
if title.present?
|
2015-08-18 03:34:46 -04:00
|
|
|
crawled = (TopicLink.where(id: topic_link.id).update_all(['title = ?, crawled_at = CURRENT_TIMESTAMP', title[0..254]]) == 1)
|
2014-04-10 13:45:13 -04:00
|
|
|
end
|
2014-04-07 16:03:47 -04:00
|
|
|
end
|
2014-04-05 14:47:25 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-07 14:38:18 -04:00
|
|
|
rescue Exception
|
|
|
|
# If there was a connection error, do nothing
|
|
|
|
ensure
|
2014-04-10 13:19:38 -04:00
|
|
|
TopicLink.where(id: topic_link.id).update_all('crawled_at = CURRENT_TIMESTAMP') if !crawled && topic_link.present?
|
2014-04-07 14:38:18 -04:00
|
|
|
end
|
2014-04-05 14:47:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|