Skip to content

Instantly share code, notes, and snippets.

@vti
Created October 21, 2011 06:54
Show Gist options
  • Save vti/1303256 to your computer and use it in GitHub Desktop.
Save vti/1303256 to your computer and use it in GitHub Desktop.
Export your Bootylicious comments to Disqus
#!/usr/bin/env perl
use strict;
use warnings;
my ($base_url, $articles_dir) = @ARGV;
$base_url && $articles_dir or die "Usage: $0 <base_url> <articles_directory>";
opendir(my $dh, $articles_dir) || die "can't opendir $articles_dir: $!";
my @files = grep { -f "$articles_dir/$_" } readdir($dh);
closedir $dh;
my @articles;
foreach my $file (sort @files) {
if ($file =~ m/\.pod$/) {
my ($date, $slug) = $file =~ m/^(.*?)-(.*?)\./;
my ($year, $month, $day, $time) = $date =~ m/^(\d\d\d\d)(\d\d)(\d\d)T(.*)/;
$date = "$year-$month-$day $time";
open my $fh, '<', "$articles_dir/$file" or die $!;
my $content = do {local $/; <$fh>};
close $fh;
my ($title) = $content =~ m/Title: (.*)/;
$month =~ s/^0//;
push @articles,
{ created => $date,
title => $title,
file => $file,
slug => $slug,
url => "$base_url/articles/$year/$month/$slug.html"
};
}
else {
my ($id) = $file =~ m/-(\d+)$/;
my $content =
do { local $/; open my $fh, '<', "$articles_dir/$file" or die $!; <$fh> };
my ($author) = $content =~ m/Author: (.*)/;
my ($email) = $content =~ m/Email: (.*)/;
if ($file =~ s/-(\d)$//) {
$file = "$file-0$1";
}
$email ||= "anonymous\@$base_url";
$content =~ s/^.*\n\n//ms;
$content =~ s/\r//msg;
my ($date, $slug) = $file =~ m/^(.*?)-(.*?)\./;
my ($year, $month, $day, $time) = $date =~ m/^(\d\d\d\d)(\d\d)(\d\d)T(.*)/;
$date = "$year-$month-$day $time";
push @{$articles[-1]->{comments}},
{ id => $id,
author => $author,
email => $email,
file => $file,
content => $content,
created => $date
};
}
}
foreach my $article (@articles) {
$article->{comments} = [sort @{$article->{comments} || []}];
}
print <<'EOF';
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dsq="http://www.disqus.com/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.0/"
>
<channel>
EOF
foreach my $article (reverse @articles) {
next unless @{$article->{comments}};
print <<"EOF";
<item>
<!-- title of article -->
<title>$article->{title}</title>
<!-- absolute URI to article -->
<link>$article->{url}</link>
<!-- thread body; use cdata; html allowed (though will be formatted to DISQUS specs) -->
<content:encoded><![CDATA[ ]]></content:encoded>
<!-- value used within disqus_identifier; usually internal identifier of article -->
<dsq:thread_identifier>$article->{url}</dsq:thread_identifier>
<!-- creation date of thread (article), in GMT -->
<wp:post_date_gmt>$article->{created}</wp:post_date_gmt>
<!-- open/closed values are acceptable -->
<wp:comment_status>open</wp:comment_status>
EOF
foreach my $comment (@{$article->{comments}}) {
print <<"EOF";
<wp:comment>
<!-- internal id of comment -->
<wp:comment_id>$comment->{id}</wp:comment_id>
<!-- author display name -->
<wp:comment_author>$comment->{author}</wp:comment_author>
<!-- author email address -->
<wp:comment_author_email>$comment->{email}</wp:comment_author_email>
<!-- author url, optional -->
<wp:comment_author_url></wp:comment_author_url>
<!-- author ip address -->
<wp:comment_author_IP></wp:comment_author_IP>
<!-- comment datetime, in GMT -->
<wp:comment_date_gmt>$comment->{created}</wp:comment_date_gmt>
<!-- comment body; use cdata; html allowed (though will be formatted to DISQUS specs) -->
<wp:comment_content><![CDATA[$comment->{content}]]></wp:comment_content>
<!-- is this comment approved? 0/1 -->
<wp:comment_approved>1</wp:comment_approved>
<!-- parent id (match up with wp:comment_id) -->
<wp:comment_parent>0</wp:comment_parent>
</wp:comment>
EOF
}
print <<'EOF';
</item>
EOF
}
print <<'EOF';
</channel>
</rss>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment