Add no_deleted option to disqus import

Disqus preserves deleted comments, provide an option to skip importing them
This commit is contained in:
sethherr 2016-04-01 10:25:34 -05:00
parent 1481b3420e
commit 5524fb4929

View file

@ -7,7 +7,7 @@ class ImportScripts::Disqus < ImportScripts::Base
verify_file(options[:file]) verify_file(options[:file])
@post_as_user = get_post_as_user(options[:post_as]) @post_as_user = get_post_as_user(options[:post_as])
@dry_run = options[:dry_run] @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 = Nokogiri::XML::SAX::Parser.new(@parser)
doc.parse_file(options[:file]) doc.parse_file(options[:file])
@parser.normalize @parser.normalize
@ -71,10 +71,11 @@ end
class DisqusSAX < Nokogiri::XML::SAX::Document class DisqusSAX < Nokogiri::XML::SAX::Document
attr_accessor :posts, :threads attr_accessor :posts, :threads
def initialize(strip) def initialize(strip, no_deleted = false)
@inside = {} @inside = {}
@posts = {} @posts = {}
@threads = {} @threads = {}
@no_deleted = no_deleted
@strip = strip @strip = strip
end end
@ -86,6 +87,8 @@ class DisqusSAX < Nokogiri::XML::SAX::Document
when 'thread' when 'thread'
id = Hash[attrs]['dsq:id'] id = Hash[attrs]['dsq:id']
if @post 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 = @threads[id]
thread[:posts] << @post thread[:posts] << @post
else else
@ -121,6 +124,7 @@ class DisqusSAX < Nokogiri::XML::SAX::Document
record(@post, :author_name, str, 'author', 'name') record(@post, :author_name, str, 'author', 'name')
record(@post, :author_anonymous, str, 'author', 'isAnonymous') record(@post, :author_anonymous, str, 'author', 'isAnonymous')
record(@post, :created_at, str, 'createdAt') record(@post, :created_at, str, 'createdAt')
record(@post, :is_deleted, str, 'isDeleted')
record(@thread, :link, str, 'link') record(@thread, :link, str, 'link')
record(@thread, :title, str, 'title') record(@thread, :title, str, 'title')
@ -189,6 +193,10 @@ OptionParser.new do |opts|
options[:post_as] = value options[:post_as] = value
end 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| opts.on('-s', '--strip=TEXT', 'Text to strip from titles') do |value|
options[:strip] = value options[:strip] = value
end end