2014-04-06 02:47:25 +08:00
|
|
|
require 'open-uri'
|
|
|
|
require 'nokogiri'
|
|
|
|
require 'excon'
|
2017-07-22 03:29:04 +08:00
|
|
|
require_dependency 'retrieve_title'
|
2017-10-06 11:39:00 +08:00
|
|
|
require_dependency 'topic_link'
|
2014-04-06 02:47:25 +08:00
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class CrawlTopicLink < Jobs::Base
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
raise Discourse::InvalidParameters.new(:topic_link_id) unless args[:topic_link_id].present?
|
2014-04-08 02:38:18 +08:00
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
topic_link = TopicLink.find_by(id: args[:topic_link_id], internal: false, crawled_at: nil)
|
2014-04-18 02:00:22 +08: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 14:53:28 +08:00
|
|
|
# topic could be deleted, so skip
|
|
|
|
if topic_embed && topic_embed.topic
|
2014-04-18 02:00:22 +08:00
|
|
|
TopicLink.where(id: topic_link.id).update_all(['title = ?, crawled_at = CURRENT_TIMESTAMP', topic_embed.topic.title[0..255]])
|
|
|
|
return
|
|
|
|
end
|
2014-04-08 04:03:47 +08:00
|
|
|
|
2014-04-18 02:00:22 +08:00
|
|
|
begin
|
2014-04-08 04:03:47 +08:00
|
|
|
crawled = false
|
|
|
|
|
2014-04-11 01:45:13 +08:00
|
|
|
# Special case: Images
|
|
|
|
# If the link is to an image, put the filename as the title
|
2017-06-22 18:54:42 +08:00
|
|
|
if FileHelper.is_image?(topic_link.url)
|
2014-04-11 01:45:13 +08: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
|
2017-07-22 03:29:04 +08:00
|
|
|
title = RetrieveTitle.crawl(topic_link.url)
|
|
|
|
if title.present?
|
|
|
|
crawled = (TopicLink.where(id: topic_link.id).update_all(['title = ?, crawled_at = CURRENT_TIMESTAMP', title[0..254]]) == 1)
|
2014-04-06 02:47:25 +08:00
|
|
|
end
|
|
|
|
end
|
2014-04-08 02:38:18 +08:00
|
|
|
rescue Exception
|
|
|
|
# If there was a connection error, do nothing
|
|
|
|
ensure
|
2014-04-11 01:19:38 +08:00
|
|
|
TopicLink.where(id: topic_link.id).update_all('crawled_at = CURRENT_TIMESTAMP') if !crawled && topic_link.present?
|
2014-04-08 02:38:18 +08:00
|
|
|
end
|
2014-04-06 02:47:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|