2013-12-31 14:37:43 -05:00
|
|
|
#
|
|
|
|
# Creates and Updates Topics based on an RSS or ATOM feed.
|
|
|
|
#
|
|
|
|
require 'digest/sha1'
|
|
|
|
require_dependency 'post_creator'
|
|
|
|
require_dependency 'post_revisor'
|
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class PollFeed < Jobs::Scheduled
|
2014-02-05 18:14:41 -05:00
|
|
|
every 1.hour
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
poll_feed if SiteSetting.feed_polling_enabled? &&
|
|
|
|
SiteSetting.feed_polling_url.present? &&
|
|
|
|
SiteSetting.embed_by_username.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def feed_key
|
|
|
|
@feed_key ||= "feed-modified:#{Digest::SHA1.hexdigest(SiteSetting.feed_polling_url)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def poll_feed
|
2014-05-06 09:41:59 -04:00
|
|
|
user = User.find_by(username_lower: SiteSetting.embed_by_username.downcase)
|
2013-12-31 14:37:43 -05:00
|
|
|
return if user.blank?
|
|
|
|
|
|
|
|
require 'simple-rss'
|
|
|
|
rss = SimpleRSS.parse open(SiteSetting.feed_polling_url)
|
|
|
|
|
|
|
|
rss.items.each do |i|
|
|
|
|
url = i.link
|
|
|
|
url = i.id if url.blank? || url !~ /^https?\:\/\//
|
|
|
|
|
2014-01-02 14:29:27 -05:00
|
|
|
content = i.content || i.description
|
|
|
|
if content
|
|
|
|
TopicEmbed.import(user, url, i.title, CGI.unescapeHTML(content.scrub))
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|