#!/usr/local/bin/perl -w
###########################################
# tumblr-post - Post Foscam snapshot
# Mike Schilli, 2013 (m@perlmeister.com)
###########################################
use strict;
use Sysadm::Install qw(:all);
use YAML qw( LoadFile );
use WWW::Tumblr;
use WWW::Mechanize;

my( $home )  = glob "~";
my $cfg_file = "$home/.tumblr.yml";

my $ua = WWW::Mechanize->new();
my $picfile = "snapshot.jpg";
my $url = "http://XXX.myfoscam.org" .
  ":5148/snapshot.cgi";
my $site = "schtonk.tumblr.com";
my $cam_user = "XXX";
my $cam_pass = "YYY";

$ua->credentials( $cam_user, $cam_pass );

my $resp = $ua->get( $url );
blurt $resp->content(), $picfile;

my $cfg = LoadFile( $cfg_file );

my $t = WWW::Tumblr->new(
  consumer_key => $cfg->{ consumer_key },
  secret_key   => $cfg->{ secret_key },
  token        => $cfg->{ token },
  token_secret => $cfg->{ token_secret },
);
 
my $blog = $t->blog( $site );

my $post = $blog->post(
  type => 'photo', data => [$picfile] ,
);

if( $post ) {
   print "I have published post id: " .
      $post->{id}, "\n"; 
} else {
   die $blog->error;
}
