From 7722a8f3d6f794a500cfc48097b9e0b693d1b126 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Sun, 2 Jan 2011 08:20:16 +0000 Subject: [PATCH] add fb upload --- fb_upload | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100755 fb_upload diff --git a/fb_upload b/fb_upload new file mode 100755 index 0000000..beac104 --- /dev/null +++ b/fb_upload @@ -0,0 +1,155 @@ +#! /usr/bin/perl +# fb_upload uploads photos to facebook, and is released under +# the terms of the GPL version 2, or any later version, at your +# option. See the file README and COPYING for more information. +# Copyright 2011 by Don Armstrong . + + +use warnings; +use strict; + +use Getopt::Long; +use Pod::Usage; + +=head1 NAME + +fb_upload - upload photos to facebook + +=head1 SYNOPSIS + + fb_upload [options] image1 [image2...] + find . -type f |fb_upload [options] + + Options: + --new, -n Create a new album with specified name + --aid, -a ID of existing album to upload to + --debug, -d debugging level (Default 0) + --help, -h display this help + --man, -m display manual + +=head1 OPTIONS + +=over + +=item B<--new, -n> + +Name of new album to create and upload to. Exactly one of B<--new> or +B<--aid> must be specified. + +=item B<--aid, -a> + +ID of existing album to upload to. Exactly one of B<--new> or B<--aid> +must be specified. + +=item B<--debug, -d> + +Debug verbosity. (Default 0) + +=item B<--help, -h> + +Display brief usage information. + +=item B<--man, -m> + +Display this manual. + +=back + +=cut + +use WWW::Facebook::API; +use User; +use File::Slurp qw(read_file); +use File::Basename; + +use vars qw($DEBUG); + +my %options = (debug => 0, + help => 0, + man => 0, + ); + +GetOptions(\%options, + 'aid|id|a=s','new|n=s', + 'desc|d=s', + 'debug|d+','help|h|?','man|m'); + +pod2usage() if $options{help}; +pod2usage({verbose=>2}) if $options{man}; + +$DEBUG = $options{debug}; + +my @USAGE_ERRORS; +if (1 != grep { exists $options{$_} && defined $options{$_} } qw(aid new)) { + push @USAGE_ERRORS,"Exactly one of --aid or --new must be passed"; +} + +if (exists $options{aid} and defined $options{aid} and $options{aid} !~ /^\d+$/) { + push @USAGE_ERRORS,"--aid must be all numeric"; +} + +my $config_file = User->Home.'/.fb_photo_upload'; +if (! -r $config_file) { + push @USAGE_ERRORS, "The configuration file '$config_file' doesn't exist or is not readable."; +} + +pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; + +my $client = + WWW::Facebook::API->new(debug => $options{debug}, + desktop => 1. + ) + or die "Unable to create new facebook API"; +$client->config($config_file); +# because there's no way to set the configuration file before new, and +# ->config doesn't do this. +$client->_set_from_outside(); +my $token = $client->auth->login(browser => 'sensible-browser'); +$client->auth->get_session($token); + + + +my $aid; +if (exists $options{new} and defined $options{new}) { + my $response = + $client->photos->create_album(name => $options{new}, + (exists $options{desc} && defined $options{desc}) ? (desc => $options{desc}) : (), + ); + $aid = $response->{aid}; + if (not defined $aid) { + die "Unable to create album"; + } +} +else { + # probably should check that this is a valid album, but whatever. + $aid = $options{aid}; +} + +my @photos; + +if (! @ARGV) { + # probably should use \0 instead on \n, but whatever. + while (<>) { + chomp; + push @photos, $_; + } +} +else { + @photos = @ARGV; +} + +for my $photo (@photos) { + print "Uploading $photo"; + my $response = + $client->photos->upload(aid => $aid, + data => scalar read_file($photo), + caption => '', + filename => basename($photo), + ); + print " to ".$response->{link}."\n"; +} + + +__END__ + + -- 2.39.5