]> git.donarmstrong.com Git - bin.git/blob - fb_upload
add reset usb bus command
[bin.git] / fb_upload
1 #! /usr/bin/perl
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>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long;
12 use Pod::Usage;
13
14 =head1 NAME
15
16 fb_upload - upload photos to facebook
17
18 =head1 SYNOPSIS
19
20  fb_upload [options] image1 [image2...]
21  find . -type f |fb_upload [options]
22
23  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
29
30 =head1 OPTIONS
31
32 =over
33
34 =item B<--new, -n>
35
36 Name of new album to create and upload to. Exactly one of B<--new> or
37 B<--aid> must be specified.
38
39 =item B<--aid, -a>
40
41 ID of existing album to upload to. Exactly one of B<--new> or B<--aid>
42 must be specified.
43
44 =item B<--debug, -d>
45
46 Debug verbosity. (Default 0)
47
48 =item B<--help, -h>
49
50 Display brief usage information.
51
52 =item B<--man, -m>
53
54 Display this manual.
55
56 =back
57
58 =cut
59
60 use WWW::Facebook::API;
61 use User;
62 use File::Slurp qw(read_file);
63 use File::Basename;
64
65 use vars qw($DEBUG);
66
67 my %options = (debug           => 0,
68                help            => 0,
69                man             => 0,
70                );
71
72 GetOptions(\%options,
73            'aid|id|a=s','new|n=s',
74            'desc|d=s',
75            'debug|d+','help|h|?','man|m');
76
77 pod2usage() if $options{help};
78 pod2usage({verbose=>2}) if $options{man};
79
80 $DEBUG = $options{debug};
81
82 my @USAGE_ERRORS;
83 if (1 != grep { exists $options{$_} && defined $options{$_} } qw(aid new)) {
84      push @USAGE_ERRORS,"Exactly one of --aid or --new must be passed";
85 }
86
87 if (exists $options{aid} and defined $options{aid} and $options{aid} !~ /^\d+$/) {
88     push @USAGE_ERRORS,"--aid must be all numeric";
89 }
90
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.";
94 }
95
96 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
97
98 my $client =
99     WWW::Facebook::API->new(debug => $options{debug},
100                             desktop => 1.
101                            )
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);
109
110
111
112 my $aid;
113 if (exists $options{new} and defined $options{new}) {
114     my $response =
115         $client->photos->create_album(name => $options{new},
116                                       (exists $options{desc} && defined $options{desc}) ? (desc => $options{desc}) : (),
117                                       );
118     $aid = $response->{aid};
119     if (not defined $aid) {
120         die "Unable to create album";
121     }
122 }
123 else {
124     # probably should check that this is a valid album,  but whatever.
125     $aid = $options{aid};
126 }
127
128 my @photos;
129
130 if (! @ARGV) {
131     # probably should use \0 instead on \n, but whatever.
132     while (<>) {
133         chomp;
134         push @photos, $_;
135     }
136 }
137 else {
138     @photos = @ARGV;
139 }
140
141 for my $photo (@photos) {
142     print "Uploading $photo";
143     my $response;
144     my $try=0;
145     while (not $response =
146            $client->photos->upload(aid => $aid,
147                                    data => scalar read_file($photo),
148                                    caption => '',
149                                    filename => basename($photo),
150                                   )) {
151         $try++;
152         print " [failure $try] ";
153         sleep 3;
154         last if $try > 10;
155     }
156     if (not $response) {
157         die "Upload failure at photo $photo";
158     }
159     print " to ".$response->{link}."\n";
160 }
161
162
163 __END__
164
165