Getting Married and Posting to Blogger

As of June 20th, I am now married. It was also my birthday and Father's Day (we just found out she's pregnant!), so I am not only a very lucky man, but I'm going to be totally screwed on presents.

Ceremony

And to make things relevant, I recently asked how to post to blogger.com from Perl. I was also asked to share the solution. Here it is.

Egor Shipovalov on the london.pm mailing list posted some working code he used to post to blogger.com from emacs, but it didn't work for me. Seems one of the required modules, Net::Google::GData was accidentally removed from the CPAN (it was been readded after I contacted the author) and after I grabbed it from the backpan, the primary module, WWW::Blogger, failed horribly with the message "Your config file /Users/ovid/.www_blogger_rc is readable by others!". I thought that was odd because I didn't have such a file, but the module created it for me. I had to edit that to add my username and password to get WWW::Blogger to work. This is not documented and, apparently, that's deliberate.

It's also the only way to supply your credentials to the module. The following line does not allow you to authenticate, even though one might think so from the docs:

WWW::Blogger::XML::API::set_account( $username, $password );

In any event, I finally managed to get it working and while what follows is hardly generic, it does post to blogger.

#!/usr/bin/env perl

use Modern::Perl;

use LWP::Simple;
use XML::XPath;
use WWW::Blogger::XML;

use Readonly;
Readonly my $BLOG_ID => ...;

my $post_body = <<'END';
<entry xmlns='http://www.w3.org/2005/Atom'>
  <title type='text'>Post!</title>
  <content type='xhtml'>
    <!-- content start -->
    <p>This is a post</p>
    <p>Are we good to go now?</p>
    <!-- content end -->
  </content>
  <category scheme="http://www.blogger.com/atom/ns#" term="testing" />
  <category scheme="http://www.blogger.com/atom/ns#" term="again" />
</entry>
END

# This works because I have a ~/.www_blogger_rc file
# Create the request.
my $request = HTTP::Request->new(
    'POST',
    $WWW::Blogger::XML::API::url . "/feeds/$BLOG_ID/posts/default/",
    [ Content_Type => 'application/atom+xml' ], $post_body,
);

my $result = WWW::Blogger::XML::API::ua_request($request);
die $result->status_line unless $result->is_success;
my $xpath = XML::XPath->new( xml => $result->content );
my @hrefs =
  $xpath->findnodes_as_string('string(/entry/link[@rel="alternate"]/@href)');
if ( @hrefs > 1 ) {
    warn $result->content;
    require Data::Dumper;
    warn Data::Dumper::Dumper( \@hrefs );
    die "PANIC: More than one response href found";
}
say $hrefs[0];

Good luck with that!

7 Comments

Congrats... on, well, everything :)

"so I am not only a very lucky man, but I'm going to be totally screwed on presents"

Hehe, I know the feeling :) (wedding date is 2 days away from the my birthday)

Congratulations on everything, including getting the post-to-blogger code working :)

Most of it seems straight-forward, but what is the "die "PANIC: More than one response href found";" part for?

I got married on Leap Day...thinking an anniversary every 4 years. We had two weddings. So now every 4 years...we celebrate to anniversaries.

Congrats by the way!

Happy belated wedding!

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/