RSS import to WordPress

I have fixed the date part of my script and tidied it up a bit so that you can pass in the file and the status you want the posts to come in as on the command line. Run it like this,

rss_to_wordpress_import.pl rss_feed.xml draft

Ironically looking at the documentation WordPress does have a feature to import from RSS feeds. I’m guessing that it’s not included as part of their free hosted service because they don’t want spam blogs to have an easy source of input.


#!/usr/bin/perl

use strict;
use XML::Simple;

my $file = shift;
if(!$file)
{
	print "Usage $0 rss_feed.xml [draft|publish]\n";
	exit(1);
}
my $xs = XML::Simple->new();
my $rss_feed = $xs->XMLin($file);
my $status = shift;
if(!$status)
{
	$status = "draft";
}
my $items = $rss_feed->{channel}->{item};

# I set this as being above the last ID of the posts already on my blog
# I'm not sure if it matters.
my $num = 50; 
my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; 
foreach my $item (@$items)
{
	$item->{link} =~ /.*\/(.*?)$/;
	my $name = $1;
	$item->{pubDate} =~ /(\d\d) (\w\w\w) (\d\d\d\d) (\d\d:\d\d:\d\d) /;
	my $mon = 1;
	my $mon_name = $2;
	while($months[$mon-1] ne $mon_name)
	{
		$mon++;
	} 
	my $wp_date = sprintf "%4.4d-%2.2d-%2.2d %s", $3, $mon, $1, $4;
print << "END";
<item>
<title>$item->{title}</title>
<link>http://colinnewell.wordpress.com/?p=$num</link>
<pubDate>$item->{pubDate}</pubDate>
<dc:creator>colinnewell</dc:creator>
		<category><![CDATA[Uncategorized]]></category>
<guid isPermaLink="false"></guid>
<description></description>
<content:encoded><![CDATA[$item->{description}]]></content:encoded>
<wp:post_id>$num</wp:post_id>
<wp:post_date>$wp_date</wp:post_date>
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>$name</wp:post_name>
<wp:status>$status</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>post</wp:post_type>
</item>
END
	$num++;
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers