Skip to Content

I’m very interested in playing with blogs not just as a communication channel but also as a technology. So far though I haven’t actually gotten as far as posting entries using any of the blog APIs out there like Blogger or MetaWeblog. Certainly some of this has to do with my fear and/or dislike of Perl. However I did have a nice experience with it last year playing around with RSS so it’s that time again. Last time I extracted the titles and links of my posts here in an html include file for use in the side bar of my personal site, but I want something more. As my personal site is running on MovableType I decided to see if I couldn’t grab the RSS feed of my posts here and post the entry description with a link back to SDN on my personal blog. Perl didn’t turn out to be much of a problem this time but poor documentation sure did.

Before we go there lets start with the basics. Here is the intial setup for the program. This defines the modules we need and the location of the feed to parse. The example here is mine, but you can get anyone elses from the Authors page.

use LWP::Simple;

use XML::Simple;

use NET::Blogger;

  1. get the feed<br/>

$url=”http://weblogs.sdn.sap.com/pub/q/weblog_rss_author?x-author=23&x-ver=1.0“;

$feed_to_parse = get ($url) or die “That’s not a feed man!”;

$parser = XML::Simple->new();

$rss = $parser->XMLin(“$feed_to_parse”);

Next we need to setup the class for accessing MovableType. You will obviously need to change the path to your own XML-RPC server and enter your own user/pass.

$mt = Net::Blogger->new(engine => 'movabletype');

  1. set our proxy (the URL of our XML-RPC server)<br/>

$mt->Proxy(“http://www.example.com/bin/mt/mt-xmlrpc.cgi“);<br/>

  1. replace with your username and password<br/>

$mt->Username(“user”);

$mt->Password(“******”);

$mt->BlogId(1);

Note the BlogId field above. You can get the proper value for this by looking at the url when you are maintaing your site, it will look something like this, http://www.example.com/bin/mt/mt.cgi?__mode=menu&blog_id=1. Here is a code snippet you can use to print it out if you’d prefer. Once I got the ID I removed this from my final program.

$blogs = $mt->getUsersBlogs();

for $b ( @$blogs ) {

  printf("[%02d] %s\n\t%s\n", $b->{blogid}, $b->

, $b->)

}

If you’s like to set a category for your entry your best bet is to find the category ID with the following code, again once I had this value I removed the printout of the list it from the final version. However the section that sets the category does need to stay, note I’m setting all of my posts to the same category on my personal site, SAP of which the ID happens to be 11.

#Print the category list

$categorylist = $mt->mt()->getCategoryList();

for my $category (@{ $categorylist } ) {

  print "\n", $category->{ 'categoryId' }, ")\t", $category->{ 'categoryName' },"\n";

}

#Setting the default category

$category_hash =  11; # My own value, yours will be different<br/>

$category_hash = 1; # Sets as the primary category

@category_list = (\%category_hash);

Now we get to the main meat of this. Not a lot to explain here though, the code is fairly well commented. Basically we are parsing the RSS feed and building up entries that we post using the metaWeblog API of NET::Blogger though we do use a couple of calls specific to MT as well.

  1. RSS 1.0<br/>

foreach my $item (@{$rss->{‘item’}}) {

You may notice the weirdness above around the dateCreated. Well, to begin with the format returned by the SDN feeds is not what MovableType claims they want, ISO.8601 format. I wasted a lot of time trying to grok Perl date routines on this before I found this support article that explained the problem, what is expected is a date in the format of YYYYMMDDTHH:MM:SS. I’m sure there is an elegant way to do this in Perl, but I’m even more sure I’m tired of looking for it for now.

I know I’ve seen enough posts around here on Perl that I’m fairly sure this has entertained at least a few of you. Have fun with it.

BTW, I’d like to call you attention to this article by Jason Gessner that was invaluable for learning how to work with this API.

</p>

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply