line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Shutterstock::LicensedMedia; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$WebService::Shutterstock::LicensedMedia::VERSION = '0.006'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Role for providing common functionality for licensed media |
7
|
|
|
|
|
|
|
|
8
|
9
|
|
|
9
|
|
52
|
use strict; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
296
|
|
9
|
9
|
|
|
9
|
|
65
|
use warnings; |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
210
|
|
10
|
9
|
|
|
9
|
|
46
|
use Moo::Role; |
|
9
|
|
|
|
|
15
|
|
|
9
|
|
|
|
|
81
|
|
11
|
9
|
|
|
9
|
|
8715
|
use Carp qw(croak); |
|
9
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
607
|
|
12
|
9
|
|
|
9
|
|
190
|
use WebService::Shutterstock::Exception; |
|
9
|
|
|
|
|
28
|
|
|
9
|
|
|
|
|
221
|
|
13
|
9
|
|
|
9
|
|
50
|
use LWP::UserAgent; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
4385
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has download_url => ( is => 'ro' ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub download { |
20
|
3
|
|
|
3
|
1
|
178
|
my $self = shift; |
21
|
3
|
|
|
|
|
12
|
my %args = @_; |
22
|
3
|
|
|
|
|
10
|
my @unknown_args = grep { !/^(file|directory)$/ } keys %args; |
|
2
|
|
|
|
|
16
|
|
23
|
|
|
|
|
|
|
|
24
|
3
|
50
|
|
|
|
13
|
croak "Invalid args: @unknown_args (expected either 'file' or 'download')" if @unknown_args; |
25
|
|
|
|
|
|
|
|
26
|
3
|
|
|
|
|
16
|
my $url = $self->download_url; |
27
|
3
|
|
|
|
|
6
|
my $destination; |
28
|
3
|
100
|
|
|
|
15
|
if($args{directory}){ |
|
|
100
|
|
|
|
|
|
29
|
1
|
|
|
|
|
2
|
$destination = $args{directory}; |
30
|
1
|
|
|
|
|
7
|
$destination =~ s{/$}{}; |
31
|
1
|
|
|
|
|
7
|
my($basename) = $url =~ m{.+/(.+)}; |
32
|
1
|
|
|
|
|
6
|
$destination .= "/$basename"; |
33
|
|
|
|
|
|
|
} elsif($args{file}){ |
34
|
1
|
|
|
|
|
2
|
$destination = $args{file}; |
35
|
|
|
|
|
|
|
} |
36
|
3
|
50
|
66
|
|
|
14
|
if(!defined $destination && !defined wantarray){ |
37
|
0
|
|
|
|
|
0
|
croak "Refusing to download media in void context without specifying a destination file or directory (specify ->download(file => \$some_file) or ->download(directory => \$some_dir)"; |
38
|
|
|
|
|
|
|
} |
39
|
3
|
|
|
|
|
22
|
my $ua = LWP::UserAgent->new; |
40
|
3
|
100
|
|
|
|
708
|
my $response = $ua->get( $url, ( $destination ? ( ':content_file' => $destination ) : () ) ); |
41
|
3
|
50
|
|
|
|
12188
|
if(my $died = $response->header('X-Died') ){ |
|
|
50
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
die WebService::Shutterstock::Exception->new( |
43
|
|
|
|
|
|
|
response => $response, |
44
|
|
|
|
|
|
|
error => "Unable to save media to $destination: $died" |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
} elsif($response->code == 200){ |
47
|
3
|
|
66
|
|
|
253
|
return $destination || $response->content; |
48
|
|
|
|
|
|
|
} else { |
49
|
0
|
|
|
|
|
|
die WebService::Shutterstock::Exception->new( |
50
|
|
|
|
|
|
|
response => $response, |
51
|
|
|
|
|
|
|
error => $response->status_line . ": unable to retrieve media", |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__ |