2014-01-01 03:37:43 +08: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-06 07:14:41 +08:00
|
|
|
every 1.hour
|
|
|
|
|
2014-01-01 03:37:43 +08:00
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
poll_feed if SiteSetting.feed_polling_enabled? &&
|
2014-04-25 00:48:45 +08:00
|
|
|
SiteSetting.feed_polling_url.present?
|
2014-01-01 03:37:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def feed_key
|
|
|
|
@feed_key ||= "feed-modified:#{Digest::SHA1.hexdigest(SiteSetting.feed_polling_url)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def poll_feed
|
2014-04-25 00:48:45 +08:00
|
|
|
feed = Feed.new
|
|
|
|
import_topics(feed.topics)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import_topics(feed_topics)
|
|
|
|
feed_topics.each do |topic|
|
|
|
|
import_topic(topic)
|
|
|
|
end
|
|
|
|
end
|
2014-01-01 03:37:43 +08:00
|
|
|
|
2014-04-25 00:48:45 +08:00
|
|
|
def import_topic(topic)
|
|
|
|
if topic.user
|
|
|
|
TopicEmbed.import(topic.user, topic.url, topic.title, CGI.unescapeHTML(topic.content.scrub))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Feed
|
2014-01-01 03:37:43 +08:00
|
|
|
require 'simple-rss'
|
2014-07-02 11:53:04 +08:00
|
|
|
|
|
|
|
if SiteSetting.embed_username_key_from_feed.present?
|
|
|
|
SimpleRSS.item_tags << SiteSetting.embed_username_key_from_feed.to_sym
|
|
|
|
end
|
2014-01-01 03:37:43 +08:00
|
|
|
|
2014-04-25 00:48:45 +08:00
|
|
|
def initialize
|
|
|
|
@feed_url = SiteSetting.feed_polling_url
|
2014-08-20 05:51:14 +08:00
|
|
|
@feed_url = "http://#{@feed_url}" if @feed_url !~ /^https?\:\/\//
|
2014-04-25 00:48:45 +08:00
|
|
|
end
|
2014-01-01 03:37:43 +08:00
|
|
|
|
2014-04-25 00:48:45 +08:00
|
|
|
def topics
|
|
|
|
feed_topics = []
|
|
|
|
|
|
|
|
rss.items.each do |i|
|
|
|
|
current_feed_topic = FeedTopic.new(i)
|
|
|
|
feed_topics << current_feed_topic if current_feed_topic.content
|
2014-01-03 03:29:27 +08:00
|
|
|
end
|
2014-04-25 00:48:45 +08:00
|
|
|
|
|
|
|
return feed_topics
|
2014-01-01 03:37:43 +08:00
|
|
|
end
|
2014-04-25 00:48:45 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def rss
|
|
|
|
SimpleRSS.parse open(@feed_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class FeedTopic
|
|
|
|
def initialize(article_rss_item)
|
|
|
|
@article_rss_item = article_rss_item
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
link = @article_rss_item.link
|
|
|
|
if url?(link)
|
|
|
|
return link
|
|
|
|
else
|
|
|
|
return @article_rss_item.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
@article_rss_item.content || @article_rss_item.description
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
@article_rss_item.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
|
|
|
author_user || default_user
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def url?(link)
|
|
|
|
if link.blank? || link !~ /^https?\:\/\//
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_username
|
|
|
|
begin
|
|
|
|
@article_rss_item.send(SiteSetting.embed_username_key_from_feed.to_sym)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_user
|
|
|
|
find_user(SiteSetting.embed_by_username.downcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_user
|
|
|
|
return nil if !author_username.present?
|
|
|
|
|
|
|
|
find_user(author_username)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_user(user_name)
|
|
|
|
User.where(username_lower: user_name).first
|
|
|
|
end
|
|
|
|
|
2014-01-01 03:37:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-04-25 00:48:45 +08:00
|
|
|
|
2014-01-01 03:37:43 +08:00
|
|
|
end
|