Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jima (1016)

jima
  (email not shown publicly)
http://www.empty-handed.com/

Born 1968. Not dead yet.

Journal of jima (1016)

Monday September 04, 2006
12:08 PM

Notes on posting via Perl to Blogger

[ #30881 ]

One of the features that the new code will have is the ability to automatically create a new posting for a specified blog, in order to publicly display the finished collaborative art. Some blogs, such as Movable Type, have pretty straightforward API documentation: http://www.movabletype.org/mt-static/docs/mtmanual_programmatic.html Others, like Blogger, take a bit of trial and error to get working. Here's the code that I just got working for my module that posts to an existing Blogger weblog (you'll have to put in the user name, password and numeric blog ID string into the code yourself, of course):

my($ua) = LWP::UserAgent->new;

my($entry) = <<FOO;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entry xmlns="http://purl.org/atom/ns#">
  <title mode="escaped" type="text/plain">atom test</title>
  <issued>2006-09-04T00:00:00Z</issued>
  <generator url="http://anexquisitecorpse.net">An Exquisite Corpse backend code</generator>
  <content type="application/xhtml+xml">
    <div xmlns="http://www.w3.org/1999/xhtml">Testing the Atom API</div>
  </content>
</entry>
    FOO

my($h) = HTTP::Headers->new;
$h->header('Content-type', 'application/xml');
$h->header('Host', 'www.blogger.com');
$h->authorization_basic('username','password');  # << replace with your own details, obv

my($req) = HTTP::Request->new(

        POST => 'https://www.blogger.com/atom/12345678',  # << replace with your own blogger ID, found in the header of your blogspot homepage
        $h,
        $entry
);

my($resp) = $ua->request($req);

print $resp->as_string;

You should get back a big meaty chunk of XML that will include, among other things, the formatted entry returned from the blog and a link tag that contains the URL for the new entry. Here's what that looks like:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns="http://purl.org/atom/ns#">
    <link href="https://www.blogger.com/atom/12345678/1234567890" rel="service.edit" title="atom test" type="application/atom+xml"/>
    <author>
        <name>your name here</name>
    </author>
    <issued>2006-09-03T17:00:00-07:00</issued>
    <modified>2006-09-04T16:12:56Z</modified>
    <created>2006-09-04T16:12:56Z</created>
    <link href="http://whatever.blogspot.com/whatever" rel="alternate" title="atom test" type="text/html"/>
    <id>tag:blogger.com,1999:blog-12345678.post-1234567890</id>
    <title mode="escaped" type="text/html">atom test</title>
    <content type="application/xhtml+xml" xml:base="http://whatever.blogspot.com" xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">Testing the Atom API</div>
        </div>
    </content>
    <draft xmlns="http://purl.org/atom-blog/ns#">false</draft>
</entry>

Still to be figured out: what standard-issue XML and HTTP modules I can use to parse these requests and responses, and how much of the XML I might need to hard-code into the modules.

And while we're on the subject of Blogger, can I just say that it's a big pain having to code for a standard that's currently in production (the Atom API), while at the same time being told to plan for an upgraded publishing spec (the GData API: http://code.google.com/apis/gdata/blogger.html) that I can't even test yet because it's still in beta. Well, maybe someday I'll get to test it, but for now I'll just stick with the Atom API, because that's what I've got to work with right now.