#!/usr/bin/env ruby # RSS 0.2/2.0/Atom converter to typo by Lennon Day-Reynolds # Shamelessly copied from RSS-only converter by Chris Lee # Extended to import also comments in RSS 2 format by Abhishek Balaria require File.dirname(__FILE__) + '/../../config/environment' require 'optparse' begin require 'feed_tools' rescue LoadError STDERR.puts <<-EOF This converter requires feedtools to be installed. Please run `gem install feedtools` and try again. EOF exit 1 end #This class represents a comment read from the XML file class RSSComment def initialize (xml) @xml = xml end def author @author = FeedTools::Author.new @author.name = FeedTools::XmlHelper.try_xpaths(@xml, ['author']).text if @author.name.nil? @author.name = "User" end @author.href = FeedTools::XmlHelper.try_xpaths(@xml, ['url']).text @author.email = FeedTools::XmlHelper.try_xpaths(@xml, ['email']).text @author end def created created_att = FeedTools::XmlHelper.try_xpaths(@xml, ['date']).text @created = Time.parse(created_att).gmtime @created end def content @content = FeedTools::XmlHelper.try_xpaths(@xml, ['content']).text @content end def remote_host @remote_host = FeedTools::XmlHelper.try_xpaths(@xml, ['ip']).text @remote_host end end class FeedMigrate attr_accessor :options def initialize self.options = {} self.parse_options self.convert_entries end def convert_entries feed = FeedTools::Feed.open(self.options[:url]) puts "Converting #{feed.items.length} entries..." feed.items.each do |item| puts "Converting '#{item.title}'" a = Article.new a.author = self.options[:author] a.title = item.title a.body = item.description a.created_at = item.published # lets publish it now a.published_at = a.created_at a.state = ContentState::Factory.new('published') a.publish! # and now lets also import and publish the comments for this article if comments = item.find_all_nodes('comment') comments.each do |comm| c = RSSComment.new(comm) f = Comment.new f.article_id = a.id f.author = c.author.name f.body = c.content f.state = ContentState::Factory.new('published') f.ip = c.remote_host f.created_at = c.created f.updated_at = c.created f.published_at = c.created puts "Imported comment by : #{f.author}" f.publish! end end end end def parse_options OptionParser.new do |opt| opt.banner = 'Usage: feed.rb [options]' opt.on('-a', '--author AUTHOR', 'Username of author in typo') do |a| self.options[:author] = a end opt.on('-u', '--url URL', 'URL of RSS feed to import.') do |u| self.options[:url] = u end opt.on_tail('-h', '--help', 'Show this message.') do puts opt exit end opt.parse!(ARGV) end unless self.options.include?(:author) and self.options.include?(:url) puts 'See feed.rb --help for help.' exit end end end FeedMigrate.new