Skip to content

Commit

Permalink
Add no_deleted option to disqus import
Browse files Browse the repository at this point in the history
Disqus preserves deleted comments, provide an option to skip importing them
  • Loading branch information
sethherr committed Apr 1, 2016
1 parent 1481b34 commit 5524fb4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions script/import_scripts/disqus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(options)
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
Expand Down Expand Up @@ -71,10 +71,11 @@ def get_post_as_user(username)
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

Expand All @@ -86,6 +87,8 @@ def start_element(name, attrs = [])
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
Expand Down Expand Up @@ -121,6 +124,7 @@ def characters(str)
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')
Expand Down Expand Up @@ -189,6 +193,10 @@ def normalize
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
Expand Down

0 comments on commit 5524fb4

Please sign in to comment.