Flickr.pm

filter: Statementstmt 19.4% 27 / 139 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 42.1% 8 / 19 Podpod 63.6% 7 / 11 totaltotal 18.9% 42 / 222 CC 28 SCAR 38.0
CC
28
Cov
15%
store
110.0
retrieve
56.0
image_pool
56.0
CRAP 44.9
↑ Summary Flickr/Access.pm »
1 package Net::FS::Flickr;
2 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Acme::Steganography::Image::Png;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::FS::Flickr::Access;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::FS::Flickr::DefaultImage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our $VERSION = "0.1";
12 1 our $FILESTORE_VERSION = "0.1"; # this way we can track different revisions of filestore format
13  
14  
15 =head1 NAME
16  
17 Net::FS::Flickr - store and retrieve files on Flickr
18  
19 =head1 SYNOPSIS
20  
21 my $fs = Net::FS::Flickr->new( key => $key, secret => $secret );
22  
23 $fs->set_auth($auth_key); # see API KEYS AND AUTH KEY section
24 $fs->store("file.txt");
25 $fs->store("piccy.jpg", "renamed_piccy.jpg");
26  
27 open (FILE, ">output.jpg") || die "Couldn't write to file: $!\n";
28 binmode (FILE);
29 print FILE $fs->retrieve("renamed_piccy.jpg");
30 close (FILE);
31  
32 =head1 API KEYS AND AUTH KEY
33  
34 You will need to sign up for an API key and then get the corresponding
35 secret key. You can do that from here
36  
37 http://www.flickr.com/services/api/key.gne
38  
39 Finally you will need to get an auth key. As described here
40  
41 http://www.flickr.com/services/api/auth.howto.desktop.html
42  
43 the helper script C<flickrfs> supplied with this distribution can help with that.
44  
45 =head1 METHODS
46  
47 =cut
48  
49 =head2 new
50  
51 Takes a valid API key and a valid secret key
52  
53 =cut
54  
55 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
56 0 my $class = shift;
57 0 my %opts = @_;
58 0 my $flickr = Net::FS::Flickr::Access->new({ key => $opts{key}, secret => $opts{secret} });
59 0 my $writer = Acme::Steganography::Image::Png::RGB::556FS->new();
60 0 my $self = { _flickr => $flickr, _writer => $writer };
61 0 return bless $self, $class;
62 }
63  
64 =head2 files [nsid, email or username]
65  
66 Get a list of all the files on the system
67  
68 Given an nsid, username or email, use that. Otherwise use the nsid from
69 the auth token.
70  
71 =cut
72  
73 0 ▶sub files {
Subroutine files: not called CC=4 SCAR=30.0
Pod documented
74 0 my $self = shift;
75 0 my $nsid = shift;
76 0 ▶ if (!defined $nsid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $nsid) { } 0 0 0
77 0 $nsid = $self->get_nsid_from_token();
78 } else {
79 0 $nsid = $self->{_flickr}->get_nsid($nsid);
80 }
81  
82 0 my %files;
83 0 foreach my $s ($self->{_flickr}->list_sets()) {
84 0 ▶ next unless $s->{title} =~ m!^FlickrStore v[\d.]+ !;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"title"} =~ /^FlickrStore v[\d.]+ / 0 0 0
85 0 $files{$'}++;
86 }
87 0 return keys %files;
88 }
89  
90 =head2 versions <filename> [nsid, email or username]
91  
92 Returns a list of all the versions of a file
93  
94 Each item on the list is a hashref containing the date the file was saved
95 and the id of that version using the keys I<timestamp> and I<id> respectively.
96  
97 The list is sorted, latest version first.
98  
99 Because of the way Flickr stores sets, timestamp will always be 0;
100  
101 Given an nsid, username or email, use that. Otherwise use the nsid from
102 the auth token.
103  
104 =cut
105  
106 0 ▶sub versions {
Subroutine versions: not called CC=4 SCAR=30.0
Pod documented
107 0 my $self = shift;
108 0 my $file = shift;
109 0 my $nsid = shift;
110 0 ▶ if (!defined $nsid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $nsid) { } 0 0 0
111 0 $nsid = $self->get_nsid_from_token();
112 } else {
113 0 $nsid = $self->{_flickr}->get_nsid($nsid);
114 }
115  
116 0 my @versions;
117 0 foreach my $s ($self->{_flickr}->list_sets()) {
118 0 ▶ next unless $s->{title} =~ m!^FlickrStore v[\d.]+ $file$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"title"} =~ /^FlickrStore v[\d.]+ $file$/ 0 0 0
119 0 my $id = $s->{id};
120 0 my $timestamp = 0;
121 0 push @versions, { id => $id };
122 }
123 0 return @versions;
124 }
125  
126 =head2 retrieve <filename> [version]
127  
128 Get <filename> from Flickr.
129  
130 If the file has multiple versions then you can pass in a version number to get version
131 - 1 being the oldest. If you don't pass in a version then you get the latest.
132  
133  
134 =cut
135  
136 0 ▶sub retrieve {
Subroutine retrieve: not called CC=7 SCAR=40.3
Pod documented
137 0 my $self = shift;
138 0 my $file = shift;
139 0 my $version = shift;
140  
141 0 my @versions = $self->versions($file);
142  
143 0 ▶ die "Couldn't find $file\n" unless @versions;
Branch: 1 branch
BranchTrueFalseTotal
unless @versions 0 0 0
144  
145 0 my $id;
146 0 ▶ if (!defined $version) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $version) { } 0 0 0
elsif ($version > @versions or $version < 1) { } 0 0 0
147 0 $id = $versions[0]->{id};
148 ▶ } elsif ($version > @versions || $version < 1) {
Condition0%0/3
$version > @versions or $version < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$version > @versions or $version < 1
$version > @versions $version < 1
Truth table0%0/3
A or B
A= $version > @versionsB= $version < 1
ABresult
1X1
011
000
149 0 die "No such version $version\n";
150 } else {
151 0 $id = $versions[-$version]->{id};
152 }
153  
154 0 my $dir = tempdir( CLEANUP => 1 );
155 0 my $old = cwd;
156 0 chdir($dir);
157  
158 ## first get a list of all the photos in this set
159 0 my @photos = $self->{_flickr}->get_set_photos($id);
160  
161 ## then download them all to the temp directory (in order of upload time)
162 0 my $count = 1;
163 0 my @files;
164 0 foreach my $p (@photos) {
165 0 my $file = "${count}.png"; $count++;
166 0 my $url = "http://static.flickr.com/".$p->{server}."/".$p->{id}."_".$p->{secret}."_o.png";
167 0 my $rc = LWP::Simple::getstore($url, $file);
168 0 ▶ if (is_error($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_error($rc)) 0 0 0
169 0 die "Couldn't fetch $url - $rc";
170 }
171 0 push @files, $file;
172 }
173  
174 ## then fire-up our steganography stuff
175 0 my $data = $self->{_writer}->read_files(reverse @files);
176 0 chdir($old);
177 0 return $data;
178 }
179  
180 =head2 store <filename> [as]
181  
182 Store the file <filename> on Flickr. If a second filename is given then use that
183 as the name on Flickr
184  
185 This works by stashing the data in the least significant bits of as many images as
186 is need. by default an, err, default image is used. But you can set alternative
187 images using the C<image_pool()> method.
188  
189 =cut
190  
191 0 ▶sub store {
Subroutine store: not called CC=10 SCAR=47.0
Pod documented
192 0 my $self = shift;
193 0 my $file = shift;
194 0 ▶ my $as = shift; $as = $file unless defined $as;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $as 0 0 0
195 0 ▶ die "No such file $file\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
196  
197 0 my $name = "FlickrStore v$FILESTORE_VERSION $as";
198  
199 ## First take the file and generate the steganographic images
200 # create a temporary dir
201 0 my $dir = tempdir( CLEANUP => 1 );
202 0 my $old = cwd;
203 0 ▶ open (FILE, "$file") || die "Cannot read file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "$file" or die "Cannot read file ${file}: $!" 0 0 0
204 # read the file in
205 0 my $data;
206 0 { local $/ = undef; $data = <FILE>; }
207 0 close FILE;
208 0 $self->{_writer}->data(\$data); # warning - could take a while
209 0 chdir($dir);
210  
211
212 0 ▶ if (!exists $self->{_images} || 'ARRAY' ne ref($self->{_images}) || !@{$self->{_images}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"_images"} or "ARRAY" ne ref $self->{"_images"} or not @{$self->{"_images"};}) 0 0 0
Condition0%0/6
not exists $self->{"_images"} or "ARRAY" ne ref $self->{"_images"} or not @{$self->{"_images"};}
l!l&&r!l&&!r
000
not exists $self->{"_images"} or "ARRAY" ne ref $self->{"_images"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not exists $self->{"_images"} or "ARRAY" ne ref $self->{"_images"} or not @{$self->{"_images"};}
not exists $self->{"_images"} "ARRAY" ne ref $self->{"_images"} not @{$self->{"_images"};}
Truth table0%0/4
not(A) or B or not(C)
A= not exists $self->{"_images"}B= "ARRAY" ne ref $self->{"_images"}C= not @{$self->{"_images"};}
ABCresult
1XX1
01X1
0011
0000
213 0 $self->{_images} = [ Net::FS::Flickr::DefaultImage->restore ];
214 }
215 0 my $i = int(rand(scalar(@{$self->{_images}})-1));
216 0 my @filenames = $self->{_writer}->write_images($self->{_images}->[$i]);
217
218  
219 ## Then upload the files to Flickr, noting the IDs
220 0 my @ids;
221 0 foreach my $fn (@filenames) {
222 0 my $id = $self->{_flickr}->upload( photo => $fn, auth_token => $self->{_flickr}->{auth} );
223 0 ▶ die "Couldn't upload files\n" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
224 0 push @ids, $id;
225 }
226  
227 # change back
228 0 chdir($old);
229  
230 ## Then create a new set on your flickr account with the name set as the filename
231 0 my $set_id = $self->{_flickr}->new_set("$name", shift @ids);
232  
233 ## Then add all the previous images to the set
234 0 $self->{_flickr}->add_to_set($set_id, $_) for @ids;
235  
236 ## Profit!
237 0 return 1;
238  
239 }
240  
241 =head2 image_pool [image[s]]
242  
243 With no arguments, returns an array of all the images in the current image pool.
244  
245 If you pass in one or more filenames or Imager objects then those are set as the current pool.
246  
247 =cut
248  
249 0 ▶sub image_pool {
Subroutine image_pool: not called CC=7 SCAR=40.3
Pod documented
250 0 my $self = shift;
251 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
252 0 $self->{_images} = [];
253 }
254  
255 0 for (@_) {
256 0 ▶ if (ref($_) && $_->isa('Imager')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ and $_->isa('Imager')) { } 0 0 0
elsif (ref $_) { } 0 0 0
Condition0%0/3
ref $_ and $_->isa("Imager")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and $_->isa("Imager")
ref $_ $_->isa("Imager")
Truth table0%0/3
A and B
A= ref $_B= $_->isa("Imager")
ABresult
0X0
100
111
257 0 push @{$self->{_images}}, $_;
258 0 next;
259 } elsif (ref($_)) {
260 0 die "$_ is not an Imager object";
261 }
262 0 my $tmp = Imager->new;
263 0 ▶ $tmp->open( file => $_ ) or die $tmp->errstr();
Branch: 1 branch
BranchTrueFalseTotal
$tmp->open("file", $_) or die $tmp->errstr 0 0 0
264 0 push @{$self->{_images}}, $tmp;
265 }
266 0 return @{$self->{_images}};
267  
268 }
269  
270  
271  
272  
273  
274  
275  
276 =head2 set_auth <auth key>
277  
278 Set the app authorisation key.
279  
280 =cut
281  
282 0 ▶sub set_auth {
Subroutine set_auth: not called CC=1 SCAR=6.9
Pod documented
283 0 my $self = shift;
284 0 $self->{_flickr}->{auth} = shift;
285 }
286  
287 0 ▶sub get_frob {
Subroutine get_frob: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my $self = shift;
289 0 return $self->{_flickr}->get_frob;
290 }
291  
292 0 ▶sub request_auth_url {
Subroutine request_auth_url: not called CC=1 SCAR=6.9
Pod undocumented
293 0 my $self = shift;
294 0 return $self->{_flickr}->request_auth_url(@_);
295 }
296  
297 0 ▶sub get_token {
Subroutine get_token: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my $self = shift;
299 0 return $self->{_flickr}->get_token(@_);
300 }
301  
302 0 ▶sub get_nsid_from_token {
Subroutine get_nsid_from_token: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my $self = shift;
304 0 return $self->{_flickr}->get_nsid_from_token(@_);
305 }
306 1 1;
↑ Summary Flickr/Access.pm »