Posting to blogger.com?

Egor Shipovalov kogdaugodno at gmail.com
Wed Jun 2 14:38:54 BST 2010


Here it goes. It's not exactly generic because it's designed to be
called from Emacs org-mode, but I hope it will be useful for you.

# BEGIN CODE

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
use LWP::Simple;
use HTML::Entities;
use URI::Escape;
use XML::Simple;
use WWW::Blogger::XML;
use File::Slurp;
use Data::Dumper;


# Get the options from command line.
Getopt::Long::GetOptions(\ my %opts, 'org', map "$_=s", qw/blog title
categories file/);

my $post_body = $opts{file} ? File::Slurp::read_file($opts{file}) : join '', <>;

if ($opts{org}) {
    # Convert tags to categories.
    if ($post_body =~ s!(?:&nbsp;)*<span class="tag">(.+?)</span>!!s) {
        $opts{categories} = $1;
        $opts{categories} =~ s!&nbsp;!,!g;
        $opts{categories} =~ s!___!-!g;
        $opts{categories} =~ s!__! !g;
        $opts{categories} = HTML::Entities::decode_entities($opts{categories});
    }

    # Extract post title.
    $post_body =~ s!\s*<h2[^>]*>(.+?)\s*</h2>!!s;
    $opts{title} = HTML::Entities::decode_entities($1);
}

# Search for a post with such title. Atom API doesn't seem to allow searching,
# so do a normal web search.
my $content = LWP::Simple::get(
    "http://$opts{blog}.blogspot.com?q="
    . URI::Escape::uri_escape("\"$opts{title}\"")
);
my ($blog_id) = $content =~ /[?&]blogID=(\d+)/;

# Look if a post with specified title is present in search results.
my $post_id;
while ($content =~ m!<a name='(\d+)'></a>.+?<a [^>]+>([^<]+)!sg) {
    next unless uc HTML::Entities::decode_entities($2) eq uc $opts{title};

    # If $post_id is already defined, this must be another post with the same
    # title. Therefore, the title isn't unique. Die.
    die "Multiple posts found with the title \"$opts{title}\": $post_id, $2"
        if defined $post_id;

    # This is the first match, assign the post id.
    $post_id = $1;
}

# Build post as structure for conversion to XML.
my %body = (
    'xmlns' => 'http://www.w3.org/2005/Atom',
    'content' => {
        content => $post_body,
        type    => 'html',
    },
    title => {
        content => $opts{title},
        type    => 'text',
    },
    category => [
        map {
                scheme => 'http://www.blogger.com/atom/ns#',
                term   => $_,
            },
            split /\s*,\s*/, ($opts{categories} || '')
    ],
);

# Create the request.
my $request = HTTP::Request->new(
    $post_id ? 'PUT' : 'POST',
    $WWW::Blogger::XML::API::url . "/feeds/$blog_id/posts/default/" .
($post_id || ''),
    [ Content_Type => 'application/atom+xml' ],
    XML::Simple::XMLout(\%body, RootName => 'entry'),
);

# Execute it, dying on failure.
my $result = WWW::Blogger::XML::API::ua_request($request);
die $result->status_line unless $result->is_success;

# END OF CODE

On Mon, May 31, 2010 at 2:29 PM, Ovid <publiustemp-londonpm at yahoo.com> wrote:
> Does anyone have any *working* code (I've seen plenty of failing examples) they can share which demonstrates making a successful post to blogger?  I've failed with both Net::Blogger and Atompub (http://stackoverflow.com/questions/2942998/cant-post-with-perls-netblogger).
>
> Cheers,
> Ovid
> --
> Buy the book         - http://www.oreilly.com/catalog/perlhks/
> Tech blog            - http://blogs.perl.org/users/ovid/
> Twitter              - http://twitter.com/OvidPerl
> Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
>
>
>
>



More information about the london.pm mailing list