2 # fb_upload uploads photos to facebook, and is released under
3 # the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2011 by Don Armstrong <don@donarmstrong.com>.
16 fb_upload - upload photos to facebook
20 fb_upload [options] image1 [image2...]
21 find . -type f |fb_upload [options]
24 --new, -n Create a new album with specified name
25 --aid, -a ID of existing album to upload to
26 --debug, -d debugging level (Default 0)
27 --help, -h display this help
28 --man, -m display manual
36 Name of new album to create and upload to. Exactly one of B<--new> or
37 B<--aid> must be specified.
41 ID of existing album to upload to. Exactly one of B<--new> or B<--aid>
46 Debug verbosity. (Default 0)
50 Display brief usage information.
60 use WWW::Facebook::API;
62 use File::Slurp qw(read_file);
67 my %options = (debug => 0,
73 'aid|id|a=s','new|n=s',
75 'debug|d+','help|h|?','man|m');
77 pod2usage() if $options{help};
78 pod2usage({verbose=>2}) if $options{man};
80 $DEBUG = $options{debug};
83 if (1 != grep { exists $options{$_} && defined $options{$_} } qw(aid new)) {
84 push @USAGE_ERRORS,"Exactly one of --aid or --new must be passed";
87 if (exists $options{aid} and defined $options{aid} and $options{aid} !~ /^\d+$/) {
88 push @USAGE_ERRORS,"--aid must be all numeric";
91 my $config_file = User->Home.'/.fb_photo_upload';
92 if (! -r $config_file) {
93 push @USAGE_ERRORS, "The configuration file '$config_file' doesn't exist or is not readable.";
96 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
99 WWW::Facebook::API->new(debug => $options{debug},
102 or die "Unable to create new facebook API";
103 $client->config($config_file);
104 # because there's no way to set the configuration file before new, and
105 # ->config doesn't do this.
106 $client->_set_from_outside();
107 my $token = $client->auth->login(browser => 'sensible-browser');
108 $client->auth->get_session($token);
113 if (exists $options{new} and defined $options{new}) {
115 $client->photos->create_album(name => $options{new},
116 (exists $options{desc} && defined $options{desc}) ? (desc => $options{desc}) : (),
118 $aid = $response->{aid};
119 if (not defined $aid) {
120 die "Unable to create album";
124 # probably should check that this is a valid album, but whatever.
125 $aid = $options{aid};
131 # probably should use \0 instead on \n, but whatever.
141 for my $photo (@photos) {
142 print "Uploading $photo";
145 while (not $response =
146 $client->photos->upload(aid => $aid,
147 data => scalar read_file($photo),
149 filename => basename($photo),
152 print " [failure $try] ";
157 die "Upload failure at photo $photo";
159 print " to ".$response->{link}."\n";