Last active
February 21, 2019 07:26
-
-
Save kako-jun/4f33c6b48ca3f6a660550ddd093f900f to your computer and use it in GitHub Desktop.
Disqusにコメントを一括削除する機能がなかったので、無理やり実行するRubyスクリプトやよ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
#!/usr/bin/ruby | |
require 'rexml/document' | |
# おまじない. | |
Dir.chdir( File.expand_path( File.dirname( __FILE__ ) ) ) | |
# ocraでexeにする場合は、trueに変えること. | |
EXE_MODE = false | |
# 引数なしはダメ. | |
if ARGV[0].nil? | |
exit | |
end | |
# Disqus XMLをパァァァアス. | |
disqus_xml_path = ARGV[0] | |
doc = REXML::Document.new( open( disqus_xml_path ) ) | |
# 書き込むWXRファイルを準備するよ. | |
if EXE_MODE | |
F = open( File.dirname( disqus_xml_path ) + "/wxr.xml", "w" ) | |
end | |
# 標準出力とファイル出力の切り替え用. | |
def puts( str ) | |
if EXE_MODE | |
F.puts str unless F.nil? | |
else | |
Kernel.puts str | |
end | |
end | |
# スレッドリストと、スレッドのIDリストを作るよ. | |
threads = {} | |
thread_ids = [] | |
doc.elements.each( "disqus/post" ) do |post| | |
# 削除済みコメント、スパムコメントを、ネコソギトルネード. | |
if post.elements["isDeleted"].text == "false" && post.elements["isSpam"].text == "false" | |
thread_id = post.elements["thread"].attributes["dsq:id"] | |
if threads[thread_id].nil? | |
threads[thread_id] = [] | |
thread_ids.push( thread_id ) | |
end | |
threads[thread_id].push( post ) | |
end | |
end | |
# ヘッダを書くよ. | |
puts '<?xml version="1.0" encoding="UTF-8"?>' | |
puts '<rss version="2.0"' | |
puts ' xmlns:content="http://purl.org/rss/1.0/modules/content/"' | |
puts ' xmlns:dsq="http://www.disqus.com/"' | |
puts ' xmlns:dc="http://purl.org/dc/elements/1.1/"' | |
puts ' xmlns:wp="http://wordpress.org/export/1.0/"' | |
puts '>' | |
puts '<channel>' | |
# コメント数が0でないスレッドの数だけ繰り返し. | |
thread_ids.each do |thread_id| | |
doc.elements.each( "disqus/thread" ) do |thread| | |
if thread.attributes["dsq:id"] == thread_id | |
# スレッドごと. | |
puts '' | |
puts '<item>' | |
puts '<title>' + thread.elements["title"].text + '</title>' | |
puts '<link>' + thread.elements["link"].text + '</link>' | |
puts '<content:encoded><![CDATA[]]></content:encoded>' | |
puts '<dsq:thread_identifier>' + thread_id + '</dsq:thread_identifier>' | |
puts '<wp:post_date_gmt>' + thread.elements["createdAt"].text.sub( "T", " ").sub( "Z", "" ) + '</wp:post_date_gmt>' | |
puts '<wp:comment_status>open</wp:comment_status>' | |
threads[thread_id].each do |post| | |
# コメントごと. | |
puts '' | |
puts '<wp:comment>' | |
puts ' <wp:comment_id>' + post.attributes["dsq:id"] + '</wp:comment_id>' | |
puts ' <wp:comment_author>' + post.elements["author/name"].text + '</wp:comment_author>' | |
puts ' <wp:comment_author_email>' + post.elements["author/email"].text + '</wp:comment_author_email>' | |
puts ' <wp:comment_author_url></wp:comment_author_url>' | |
puts ' <wp:comment_author_IP>' + post.elements['ipAddress'].text + '</wp:comment_author_IP>' | |
puts ' <wp:comment_date_gmt>' + post.elements["createdAt"].text.sub( "T", " ").sub( "Z", "" ) + '</wp:comment_date_gmt>' | |
puts ' <wp:comment_content>' + post.elements['message'].to_s.sub( "<message>", "").sub( "</message>", "" ) + '</wp:comment_content>' | |
puts ' <wp:comment_approved>1</wp:comment_approved>' | |
# DISQUSは、中身が空でもタグだけは作らないとイケない仕様だよ. | |
parent = post.elements['parent'] | |
if parent.nil? | |
puts ' <wp:comment_parent>0</wp:comment_parent>' | |
else | |
puts ' <wp:comment_parent>' + parent.attributes["dsq:id"] + '</wp:comment_parent>' | |
end | |
puts '</wp:comment>' | |
end | |
puts '</item>' | |
break | |
end | |
end | |
end | |
# 後始末. | |
puts '' | |
puts '</channel>' | |
puts '</rss>' | |
if EXE_MODE | |
F.close | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment