| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::PicApp; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# use 'our' on v5.6.0 |
|
4
|
3
|
|
|
3
|
|
198610
|
use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS $DEBUG); |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
217
|
|
|
5
|
3
|
|
|
3
|
|
3317
|
use XML::Simple; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use LWP::UserAgent; |
|
7
|
|
|
|
|
|
|
use Net::PicApp::Response; |
|
8
|
|
|
|
|
|
|
use URI::Escape; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$DEBUG = 0; |
|
11
|
|
|
|
|
|
|
$VERSION = '0.3'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use base qw(Class::Accessor); |
|
14
|
|
|
|
|
|
|
Net::PicApp->mk_accessors(qw(apikey url cache)); |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# We are exporting functions |
|
17
|
|
|
|
|
|
|
use base qw/Exporter/; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use constant { |
|
20
|
|
|
|
|
|
|
CONTRIB_CORBIS => 466, |
|
21
|
|
|
|
|
|
|
CONTRIB_ENTERTAINMENT_PRESS => 16797, |
|
22
|
|
|
|
|
|
|
CONTRIB_GETTY => 3, |
|
23
|
|
|
|
|
|
|
CONTRIB_IMAGE_SOURCE => 4, |
|
24
|
|
|
|
|
|
|
CONTRIB_JUPITER => 5, |
|
25
|
|
|
|
|
|
|
CONTRIB_NEWSCOM => 7387, |
|
26
|
|
|
|
|
|
|
CONTRIB_PACIFIC_COAST => 12342, |
|
27
|
|
|
|
|
|
|
CONTRIB_SPLASH => 4572, |
|
28
|
|
|
|
|
|
|
CAT_EDITORIAL => 2, |
|
29
|
|
|
|
|
|
|
CAT_CREATIVE => 3, |
|
30
|
|
|
|
|
|
|
CAT_ENTERTAINMENT => 4, |
|
31
|
|
|
|
|
|
|
CAT_NEWS => 5, |
|
32
|
|
|
|
|
|
|
CAT_SPORTS => 6, |
|
33
|
|
|
|
|
|
|
SORT_RELEVANT => 1, |
|
34
|
|
|
|
|
|
|
SORT_RECENT => 2, |
|
35
|
|
|
|
|
|
|
SORT_RANDOM => 6 |
|
36
|
|
|
|
|
|
|
}; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Methods to support: |
|
39
|
|
|
|
|
|
|
# * getimagedetails |
|
40
|
|
|
|
|
|
|
# * login |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Export list - to allow fine tuning of export table |
|
43
|
|
|
|
|
|
|
@EXPORT_OK = qw( search get_image_details publish login ); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
use strict; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub DESTROY { } |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$SIG{INT} = sub { die "Interrupted\n"; }; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$| = 1; # autoflush |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new { |
|
54
|
|
|
|
|
|
|
my $class = shift; |
|
55
|
|
|
|
|
|
|
my $params = shift; |
|
56
|
|
|
|
|
|
|
my $self = {}; |
|
57
|
|
|
|
|
|
|
foreach my $prop (qw/ apikey cache /) { |
|
58
|
|
|
|
|
|
|
if ( exists $params->{$prop} ) { |
|
59
|
|
|
|
|
|
|
$self->{$prop} = $params->{$prop}; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# else { |
|
63
|
|
|
|
|
|
|
# confess "You need to provide the $prop parameter!"; |
|
64
|
|
|
|
|
|
|
# } |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
|
67
|
|
|
|
|
|
|
$ua->agent("Net::PicApp/$VERSION"); |
|
68
|
|
|
|
|
|
|
$self->{ua} = $ua; |
|
69
|
|
|
|
|
|
|
$self->{url} = 'http://api.picapp.com/API/ws.asmx' unless $self->{url}; |
|
70
|
|
|
|
|
|
|
bless $self, $class; |
|
71
|
|
|
|
|
|
|
return $self; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub flush_cache { |
|
75
|
|
|
|
|
|
|
my $self = shift; |
|
76
|
|
|
|
|
|
|
return unless $self->cache; |
|
77
|
|
|
|
|
|
|
$self->cache->clear; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub search { |
|
81
|
|
|
|
|
|
|
my $self = shift; |
|
82
|
|
|
|
|
|
|
my ( $term, $options ) = @_; |
|
83
|
|
|
|
|
|
|
my $method; |
|
84
|
|
|
|
|
|
|
if ( $options->{'with_thumbnails'} ) { |
|
85
|
|
|
|
|
|
|
if ( $options->{'subcategory'} || $options->{'contributor'} ) { |
|
86
|
|
|
|
|
|
|
$method = 'SearchImagesWithThumbnailsContributorAndSubCategory'; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
else { |
|
89
|
|
|
|
|
|
|
$method = 'SearchImagesWithThumbnails'; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
else { |
|
93
|
|
|
|
|
|
|
if ( $options->{'subcategory'} || $options->{'contributor'} ) { |
|
94
|
|
|
|
|
|
|
$method = 'SearchWithContributorAndSubCategory'; |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
else { |
|
97
|
|
|
|
|
|
|
$method = 'Search'; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
my $url = $self->url . "/" . $method . "?ApiKey=" . $self->apikey; |
|
101
|
|
|
|
|
|
|
$url .= '&term=' . uri_escape($term); |
|
102
|
|
|
|
|
|
|
my $keys = { |
|
103
|
|
|
|
|
|
|
'category' => 'cats', |
|
104
|
|
|
|
|
|
|
'colors' => 'clrs', |
|
105
|
|
|
|
|
|
|
'orientation' => 'oris', |
|
106
|
|
|
|
|
|
|
'types' => 'types', |
|
107
|
|
|
|
|
|
|
'match_phrase' => 'mp', |
|
108
|
|
|
|
|
|
|
'post' => 'post', |
|
109
|
|
|
|
|
|
|
'sort' => 'sort', |
|
110
|
|
|
|
|
|
|
'page' => 'Page', |
|
111
|
|
|
|
|
|
|
'total_records' => 'totalRecords', |
|
112
|
|
|
|
|
|
|
}; |
|
113
|
|
|
|
|
|
|
foreach my $key ( keys %$keys ) { |
|
114
|
|
|
|
|
|
|
$url .= '&' |
|
115
|
|
|
|
|
|
|
. $keys->{$key} . '=' |
|
116
|
|
|
|
|
|
|
. ( $options->{$key} ? $options->{$key} : '' ); |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
if ( $method =~ /(contributor|category)/i ) { |
|
119
|
|
|
|
|
|
|
$keys = { |
|
120
|
|
|
|
|
|
|
'contributor' => 'contributorId', |
|
121
|
|
|
|
|
|
|
'subcategory' => 'subCategory' |
|
122
|
|
|
|
|
|
|
}; |
|
123
|
|
|
|
|
|
|
foreach my $key ( keys %$keys ) { |
|
124
|
|
|
|
|
|
|
$url .= '&' |
|
125
|
|
|
|
|
|
|
. $keys->{$key} . '=' |
|
126
|
|
|
|
|
|
|
. ( $options->{$key} ? $options->{$key} : '' ); |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $response; |
|
131
|
|
|
|
|
|
|
if (!$options->{no_cache} && $self->cache && $self->cache->exists($url)) { |
|
132
|
|
|
|
|
|
|
$response = $self->cache->thaw($url); |
|
133
|
|
|
|
|
|
|
bless $response, 'Net::PicApp::Response'; |
|
134
|
|
|
|
|
|
|
return $response; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$response = Net::PicApp::Response->new; |
|
138
|
|
|
|
|
|
|
$response->url_queried($url); |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Call PicApp |
|
141
|
|
|
|
|
|
|
my $req = HTTP::Request->new( GET => $url ); |
|
142
|
|
|
|
|
|
|
my $res = $self->{ua}->request($req); |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Check the outcome of the response |
|
145
|
|
|
|
|
|
|
if ( $res->is_success ) { |
|
146
|
|
|
|
|
|
|
my $content = $res->content; |
|
147
|
|
|
|
|
|
|
my $xml = eval { XMLin($content) }; |
|
148
|
|
|
|
|
|
|
if ($@) { |
|
149
|
|
|
|
|
|
|
print STDERR "ERROR: $@\n"; |
|
150
|
|
|
|
|
|
|
$response->error_message("Could not parse response: $@"); |
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
else { |
|
153
|
|
|
|
|
|
|
$response->init($xml); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
else { |
|
157
|
|
|
|
|
|
|
$response->error_message("Could not conduct query to: $url"); |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
if (!$options->{no_cache} && $self->cache && $response->is_success && $response->total_records > 0) { |
|
160
|
|
|
|
|
|
|
$self->cache->freeze( $url, $response ); |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
return $response; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub get_image_details { |
|
166
|
|
|
|
|
|
|
my $self = shift; |
|
167
|
|
|
|
|
|
|
my ( $id, $options ) = @_; |
|
168
|
|
|
|
|
|
|
die "No image id specified" unless $id; |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$options ||= {}; |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $url = $self->url . "/GetImageDetails?ApiKey=" . $self->apikey; |
|
173
|
|
|
|
|
|
|
$url .= '&ImageId=' . uri_escape($id); |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
my $response; |
|
176
|
|
|
|
|
|
|
if (!$options->{no_cache} && $self->cache) { |
|
177
|
|
|
|
|
|
|
$response = $self->cache->thaw($url); |
|
178
|
|
|
|
|
|
|
if ($response) { |
|
179
|
|
|
|
|
|
|
bless $response, 'Net::PicApp::Response'; |
|
180
|
|
|
|
|
|
|
return $response; |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
$response = Net::PicApp::Response->new; |
|
185
|
|
|
|
|
|
|
$response->url_queried($url); |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Call PicApp |
|
188
|
|
|
|
|
|
|
my $req = HTTP::Request->new( GET => $url ); |
|
189
|
|
|
|
|
|
|
my $res = $self->{ua}->request($req); |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Check the outcome of the response |
|
192
|
|
|
|
|
|
|
if ( $res->is_success ) { |
|
193
|
|
|
|
|
|
|
my $content = $res->content; |
|
194
|
|
|
|
|
|
|
my $xml = eval { XMLin($content) }; |
|
195
|
|
|
|
|
|
|
if ($@) { |
|
196
|
|
|
|
|
|
|
print STDERR "ERROR: $@\n"; |
|
197
|
|
|
|
|
|
|
$response->error_message("Could not parse response: $@"); |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
else { |
|
200
|
|
|
|
|
|
|
$response->init($xml); |
|
201
|
|
|
|
|
|
|
} |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
else { |
|
204
|
|
|
|
|
|
|
$response->error_message("Could not conduct query to: $url"); |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
if (!$options->{no_cache} && $self->cache && $response->is_success) { |
|
207
|
|
|
|
|
|
|
$self->cache->freeze( $url, $response ); |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
return $response; |
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub publish { |
|
213
|
|
|
|
|
|
|
my $self = shift; |
|
214
|
|
|
|
|
|
|
my ( $id, $term, $email, $options ) = @_; |
|
215
|
|
|
|
|
|
|
die "No image id specified" unless $id; |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
$options ||= {}; |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
my $url = $self->url . "/PublishImageWithSearchTerm?ApiKey=" . $self->apikey; |
|
220
|
|
|
|
|
|
|
$url .= '&ImageId=' . uri_escape($id); |
|
221
|
|
|
|
|
|
|
$url .= '&SearchTerm=' . uri_escape($term); |
|
222
|
|
|
|
|
|
|
$url .= '&Email=' . uri_escape($email); |
|
223
|
|
|
|
|
|
|
$url .= '&PicAboo='; |
|
224
|
|
|
|
|
|
|
$url .= '&Trigger='; |
|
225
|
|
|
|
|
|
|
$url .= '&username=&password='; |
|
226
|
|
|
|
|
|
|
my $keys = { |
|
227
|
|
|
|
|
|
|
'size' => 'Size', |
|
228
|
|
|
|
|
|
|
'image_frame' => 'ImageFrame', |
|
229
|
|
|
|
|
|
|
}; |
|
230
|
|
|
|
|
|
|
foreach my $key ( keys %$keys ) { |
|
231
|
|
|
|
|
|
|
$url .= '&' |
|
232
|
|
|
|
|
|
|
. $keys->{$key} . '=' |
|
233
|
|
|
|
|
|
|
. ( $options->{$key} ? $options->{$key} : '' ); |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
my $response; |
|
237
|
|
|
|
|
|
|
$response = Net::PicApp::Response->new; |
|
238
|
|
|
|
|
|
|
$response->url_queried($url); |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# Call PicApp |
|
241
|
|
|
|
|
|
|
my $req = HTTP::Request->new( GET => $url ); |
|
242
|
|
|
|
|
|
|
my $res = $self->{ua}->request($req); |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
# Check the outcome of the response |
|
245
|
|
|
|
|
|
|
if ( $res->is_success ) { |
|
246
|
|
|
|
|
|
|
my $content = $res->content; |
|
247
|
|
|
|
|
|
|
my $xml = eval { XMLin($content) }; |
|
248
|
|
|
|
|
|
|
if ($@) { |
|
249
|
|
|
|
|
|
|
print STDERR "ERROR: $@\n"; |
|
250
|
|
|
|
|
|
|
$response->error_message("Could not parse response: $@"); |
|
251
|
|
|
|
|
|
|
} |
|
252
|
|
|
|
|
|
|
else { |
|
253
|
|
|
|
|
|
|
$response->init($xml); |
|
254
|
|
|
|
|
|
|
} |
|
255
|
|
|
|
|
|
|
} |
|
256
|
|
|
|
|
|
|
else { |
|
257
|
|
|
|
|
|
|
$response->error_message("Could not conduct query to: $url"); |
|
258
|
|
|
|
|
|
|
} |
|
259
|
|
|
|
|
|
|
return $response; |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
} |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
1; |
|
264
|
|
|
|
|
|
|
__END__ |