Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created August 8, 2011 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharifulin/1131507 to your computer and use it in GitHub Desktop.
Save sharifulin/1131507 to your computer and use it in GitHub Desktop.
Export LJ posts to Posterous through API using Mojolicious
#!/usr/bin/env perl
use common::sense;
# BEGIN { $ENV{MOJO_USERAGENT_DEBUG}++ };
use Mojo::IOLoop;
use Mojo::UserAgent;
use Mojo::Util 'b64_encode';
use MongoDB;
use Data::Dumper;
my $ua = Mojo::UserAgent->new;
my $t = Mojo::IOLoop->trigger;
$ua->ioloop->connect_timeout(180);
$t ->ioloop->connect_timeout(180);
my $conn = MongoDB::Connection->new(host => 'localhost', port => 27017);
my $mdb = $conn->blog;
my $conf = {
api_token => '*********',
site_id => '3355080',
username => 'sharifulin@gmail.com',
password => '********',
};
my $auth = 'Basic ' . b64_encode( "$conf->{username}:$conf->{password}", '' );
my $limit = 20;
my $max = $mdb->posts->count;
my($i, $count);
while ($count <= $max) {
my $posts = $mdb->posts->find({post_id => { '$exists' => 0 }})->sort({date => -1})->limit($limit, $limit*$i++);
while (my $p = $posts->next) {
warn "$p->{link}\n";
$t->begin;
$ua->post_form(
"http://posterous.com/api/2/sites/$conf->{site_id}/posts",
{
api_token => $conf->{api_token},
'post[title]' => $p->{title},
'post[body]' => qq($p->{text}<br/>\nOriginal post <a href="$p->{link}">$p->{link}</a>\n),
'post[tags]' => $p->{tags } ? join(', ', @{$p->{tags}}) : '',
'post[display_date]' => $p->{date },
'post[source]' => $p->{link },
'post[autopost]' => 0,
# 'post[is_private]' => 1,
},
{ Authorization => $auth },
sub {
my $res = pop->res;
my $post_id = 0;
if ($res->code == 201) {
my $body = $res->body;
$post_id = [ $body =~ /"id":(\d+)/ ]->[0];
}
else {
warn 'ERROR ' . $res->code . "\n";
}
$mdb->posts->update({ _id => $p->{_id} }, { '$set' => {post_id => $post_id} }, { safe => 1 });
$count++;
$t->end;
}
);
};
$t->start;
warn "$count\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment