#!/usr/bin/perl
#
# upload_to_facebook: upload photos to facebook from the command line
#
# Gerald Oskoboiny, 16 Mar 2008
#
# app info: http://www.facebook.com/apps/application.php?id=11891229337
#
# source: http://impressive.net/people/gerald/2008/03/upload_to_facebook
# 
# $Id: upload_to_facebook,v 1.3 2019/10/14 21:47:46 gerald Exp $
#

my $USAGE = qq{
usage: upload_to_facebook -aid album_id < list_of_files

(the WFA_SECRET environment variable needs to be set but I don't know where
end-users are supposed to get these. Can I just include the one facebook
gave me directly in the code? Or does it need to be kept secret?)};

use strict;
use warnings;
use WWW::Facebook::API;
use Data::Dumper;
use Getopt::Long;

my $temp = "/tmp/upload_to_facebook_$$.jpg";

my $aid;		# album id
GetOptions("aid=i" => \$aid) or die $USAGE;
die $USAGE if not defined $aid or $aid < 1 or not defined $ENV{WFA_SECRET};

my $client = WWW::Facebook::API->new(
    debug => 0,
    desktop => 1,
    api_key => '58528ba237ad5377bd7e7a13a2e21ece',
    secret => $ENV{WFA_SECRET}
);

###########################################################################
# @@ ideally, do this only when necessary
# (tried to figure out how to reuse token/session info, but gave up)
my $token = $client->auth->login( browser => 'firefox', sleep => 5 );
$client->auth->get_session( $token );

# my $albums = $client->photos->get_albums;
# print Dumper $albums;

while (my $file = <>) {

    chomp($file);

    if ( ! -f $file ) {
	print STDERR "input file [$file] does not exist; skipping\n";
	next;
    }

    my $caption = "";

    # obtain a caption for each photo based on the associated RDF metadata
    # (most users will want to change this to do something else)
    my $rdffile = $file;
       $rdffile =~ s/(-[a-z]+)?\.jpg/.rdf/;
    if ( -f $rdffile ) {
	my $title = `grep -h dc.title $rdffile | cut -d\\> -f2 | cut -d\\< -f1`;
	my $desc  = `grep -h dc.desc  $rdffile | cut -d\\> -f2 | cut -d\\< -f1`;
	chomp($title); chomp($desc);
	if ( $desc =~ m,^$title, ) {	# is title redundant with description?
	    $caption = $desc;		# if so, omit title, just use desc
	}
	else {				# concatenate title and desc
	    $caption = $title;
	    $caption .= "\n\n" . $desc if length($desc);
	}
	chomp($caption);
    }

    my $cap = $caption;
       $cap  =~ s/\n+/ /g;	# remove newlines so printf doesn't whine
    printf "uploading [...]%0.20s, %-0.42s\n", substr($file, -20, 20), $cap;

    # add a copyright/watermark to each photo before uploading
#    system "cp -a $file $temp && add_watermark $temp";
     system "cp -a $file $temp";

    my $data = undef;
    open( FILE, "< $temp" ) or warn "error reading from $temp: $!";
    read( FILE, $data, 99999999 );
    close( FILE ) or warn "error closing file $file: $!";

    my $response = $client->photos->upload(
	aid => $aid,
	caption => $caption,
	data => $data
    );

}

print "\n";
print "uploads done, check it http://www.facebook.com/album.php?aid=$aid\n";

unlink( $temp ) if -f $temp;
exit;

