From 5524fb4929615b17dcc1fd0f8f51d2ab13dc9993 Mon Sep 17 00:00:00 2001 From: sethherr Date: Fri, 1 Apr 2016 10:25:34 -0500 Subject: [PATCH] Add no_deleted option to disqus import Disqus preserves deleted comments, provide an option to skip importing them --- script/import_scripts/disqus.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/script/import_scripts/disqus.rb b/script/import_scripts/disqus.rb index 06d9af1a1fb..05b8a778df4 100644 --- a/script/import_scripts/disqus.rb +++ b/script/import_scripts/disqus.rb @@ -7,7 +7,7 @@ class ImportScripts::Disqus < ImportScripts::Base verify_file(options[:file]) @post_as_user = get_post_as_user(options[:post_as]) @dry_run = options[:dry_run] - @parser = DisqusSAX.new(options[:strip]) + @parser = DisqusSAX.new(options[:strip], options[:no_deleted]) doc = Nokogiri::XML::SAX::Parser.new(@parser) doc.parse_file(options[:file]) @parser.normalize @@ -71,10 +71,11 @@ end class DisqusSAX < Nokogiri::XML::SAX::Document attr_accessor :posts, :threads - def initialize(strip) + def initialize(strip, no_deleted = false) @inside = {} @posts = {} @threads = {} + @no_deleted = no_deleted @strip = strip end @@ -86,6 +87,8 @@ class DisqusSAX < Nokogiri::XML::SAX::Document when 'thread' id = Hash[attrs]['dsq:id'] if @post + # Skip this post if it's deleted and no_deleted is true + return if @no_deleted && @post[:is_deleted].to_s == 'true' thread = @threads[id] thread[:posts] << @post else @@ -121,6 +124,7 @@ class DisqusSAX < Nokogiri::XML::SAX::Document record(@post, :author_name, str, 'author', 'name') record(@post, :author_anonymous, str, 'author', 'isAnonymous') record(@post, :created_at, str, 'createdAt') + record(@post, :is_deleted, str, 'isDeleted') record(@thread, :link, str, 'link') record(@thread, :title, str, 'title') @@ -189,6 +193,10 @@ OptionParser.new do |opts| options[:post_as] = value end + opts.on('-D', '--no_deleted', 'Do not post deleted comments') do + options[:no_deleted] = true + end + opts.on('-s', '--strip=TEXT', 'Text to strip from titles') do |value| options[:strip] = value end