| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # $Id: API.pm,v 1.35 2009/08/02 17:16:12 asc Exp $ | ||||||||||||||||||||||||||||
| 4 | # -*-perl-*- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Net::Flickr::API; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | $Net::Flickr::API::VERSION = '1.8'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Net::Flickr::API - base API class for Net::Flickr::* libraries | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package Net::Flickr::RDF; | ||||||||||||||||||||||||||||
| 17 | use base qw (Net::Flickr::API); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Base API class for Net::Flickr::* libraries | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Net::Flickr::API is a wrapper for Flickr::API that provides support for | ||||||||||||||||||||||||||||
| 24 | throttling API calls (per hour), retries if the API is disabled or | ||||||||||||||||||||||||||||
| 25 | rate-limited, and marshalling of API responses into XML::LibXML or XML::XPath | ||||||||||||||||||||||||||||
| 26 | objects. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Options are passed to Net::Flickr::Backup using a Config::Simple object or | ||||||||||||||||||||||||||||
| 31 | a valid Config::Simple config file. Options are grouped by "block". | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head2 flick | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =over 4 | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =item * B<api_key> | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | String. I<required> | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | A valid Flickr API key. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =item * B<api_secret> | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | String. I<required> | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | A valid Flickr Auth API secret key. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item * B<auth_token> | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | String. I<required> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | A valid Flickr Auth API token. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item * B<api_handler> | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | String. I<required> | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The B<api_handler> defines which XML/XPath handler to use to process API responses. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =over 4 | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item * B<LibXML> | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Use XML::LibXML. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item * B<XPath> | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Use XML::XPath. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =back | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item * B<api_calls_per_hour> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Integer. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | The most API calls to make in any hour, counting retries. Short bursts are | ||||||||||||||||||||||||||||
| 78 | allowed after a lull, but never enough to go over this in any rolling hour. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | The default, and the maximum, is 3600, which is Flickr's documented limit per | ||||||||||||||||||||||||||||
| 81 | API key. The minimum is 60. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =back | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 reporting | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =over | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item * B<enabled> | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Boolean. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Default is false. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item * B<handler> | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | String. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | The default handler is B<Screen>, as in C<Log::Dispatch::Screen> | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =item * B<handler_args> | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | For example, the following : | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | reporting_handler_args=name:foobar;min_level=info | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Would be converted as : | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | (name => "foobar", | ||||||||||||||||||||||||||||
| 110 | min_level => "info"); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | The default B<name> argument is "__report". The default B<min_level> argument | ||||||||||||||||||||||||||||
| 113 | is "info". | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =back | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | use Config::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | use Flickr::API; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 3 | ▶ | use Flickr::Upload; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | ▶ | use HTTP::Date qw(str2time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 3 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | 3 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | 3 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | ▶ | use Log::Dispatch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | use Log::Dispatch::Screen; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | Readonly::Scalar my $CALLS_PER_HOUR_MAX => 3600; | |||||||||||||||||||||||||||
| 134 | 3 | Readonly::Scalar my $CALLS_PER_HOUR_MIN => 60; | |||||||||||||||||||||||||||
| 135 | 3 | Readonly::Scalar my $PAUSE_SECONDS_UNAVAILABLE => 4; | |||||||||||||||||||||||||||
| 136 | 3 | Readonly::Scalar my $PAUSE_MAXTRIES => 10; | |||||||||||||||||||||||||||
| 137 | 3 | Readonly::Hash my %PAUSE_ONSTATUS => (429 => 1, 503 => 1); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | Readonly::Scalar my $RETRY_MAXTRIES => 10; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 PACKAGE METHODS | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head2 __PACKAGE__->new($cfg) | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Where B<$cfg> is either a valid I<Config::Simple> object or the path | ||||||||||||||||||||||||||||
| 148 | to a file that can be parsed by I<Config::Simple>. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Returns a I<Net::Flickr::API> object. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 16 | my $pkg = shift; | |||||||||||||||||||||||||||
| 156 | 16 | my $cfg = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 16 | my $self = {'__retries' => 0,}; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 16 | bless $self,$pkg; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 16 | ▶ | if (! $self->init($cfg)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | undef $self; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 16 | return $self; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 16 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=11 SCAR=33.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | 16 | my $cfg = shift; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 16 | ▶ | $self->{cfg} = (UNIVERSAL::isa($cfg, "Config::Simple")) ? $cfg : Config::Simple->new($cfg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 16 | ▶ | if ($self->{cfg}->param("flickr.api_handler") !~ /^(?:XPath|LibXML)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | warn "Invalid API handler"; | |||||||||||||||||||||||||||
| 177 | 0 | return 0; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 16 | $self->_init_rate_limit(); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 4 | ▶ | my $log_fmt = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 185 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 4 | my $msg = $args{'message'}; | |||||||||||||||||||||||||||
| 188 | 4 | chomp $msg; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 4 | ▶ | if ($args{'level'} eq "error") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | my ($ln, $sub) = (caller(4))[2,3]; | |||||||||||||||||||||||||||
| 193 | 4 | $sub =~ s/.*:://; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | return sprintf("[%s][%s, ln%d] %s\n", | ||||||||||||||||||||||||||||
| 196 | 4 | $args{'level'}, $sub, $ln, $msg); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | return sprintf("[%s] %s\n", $args{'level'}, $msg); | |||||||||||||||||||||||||||
| 200 | 16 | }; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 16 | my $logger = Log::Dispatch->new(callbacks=>$log_fmt); | |||||||||||||||||||||||||||
| 203 | 16 | my $error = Log::Dispatch::Screen->new(name => '__error', | |||||||||||||||||||||||||||
| 204 | min_level => 'error', | ||||||||||||||||||||||||||||
| 205 | stderr => 1); | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 16 | $logger->add($error); | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||||||
| 210 | # Custom report logging | ||||||||||||||||||||||||||||
| 211 | # | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 16 | ▶ | if ($self->{cfg}->param("reporting.enable")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my $report_handler = $self->{cfg}->param("reporting.handler") || "Screen"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'cfg'}->param('reporting.handler') || 'Screen'
MC/DC0%0/1
$self->{'cfg'}->param('reporting.handler') || 'Screen'
$self->{'cfg'}->param('reporting.handler')
Truth table0%0/2
A
A= $self->{'cfg'}->param('reporting.handler')
| |||||||||||||||||||||||||||||
| 216 | 0 | $report_handler =~ s/:://g; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | my $report_pkg = "Log::Dispatch::$report_handler"; | |||||||||||||||||||||||||||
| 219 | 0 | eval "require $report_pkg"; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | warn "Failed to load $report_pkg, $@"; | |||||||||||||||||||||||||||
| 223 | 0 | return 0; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | my %report_args = (); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | if (my $args = $self->{cfg}->param("reporting.handler_args")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | foreach my $part (split(",", $args)) { | |||||||||||||||||||||||||||
| 231 | 0 | my ($key, $value) = split(":", $part); | |||||||||||||||||||||||||||
| 232 | 0 | $report_args{$key} = $value; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | $report_args{'name'} ||= "__report"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$report_args{"name"} ||= "__report"
MC/DC0%0/1
$report_args{"name"} ||= "__report"
$report_args{"name"}
Truth table0%0/2
A
A= $report_args{"name"}
| |||||||||||||||||||||||||||||
| 237 | 0 | ▶ | $report_args{'min_level'} ||= "info"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$report_args{"min_level"} ||= "info"
MC/DC0%0/1
$report_args{"min_level"} ||= "info"
$report_args{"min_level"}
Truth table0%0/2
A
A= $report_args{"min_level"}
| |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | my $reporter = $report_pkg->new(%report_args); | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if (! $reporter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 0 | warn "Failed to instantiate $report_pkg, $!"; | |||||||||||||||||||||||||||
| 243 | 0 | return 0; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 0 | $logger->add($reporter); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 16 | $self->{'__logger'} = $logger; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | # | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | $self->{api} = Flickr::API->new({key => $self->{cfg}->param("flickr.api_key"), | ||||||||||||||||||||||||||||
| 254 | secret => $self->{cfg}->param("flickr.api_secret"), | ||||||||||||||||||||||||||||
| 255 | 16 | handler => $self->{cfg}->param("flickr.api_handler")}); | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 16 | my $pkg = ref($self); | |||||||||||||||||||||||||||
| 258 | 16 | my $version = undef; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 16 | do { | |||||||||||||||||||||||||||
| 261 | 16 | my $ref = join("::", $pkg, "VERSION"); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 3 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 264 | 16 | $version = ${$ref}; | |||||||||||||||||||||||||||
| 265 | }; | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 16 | my $agent_string = sprintf("%s/%s", $pkg, $version); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 16 | $self->{api}->agent($agent_string); | |||||||||||||||||||||||||||
| 270 | 16 | return 1; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head1 OBJECT METHODS | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head2 $obj->api_call(\%args) | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Valid args are : | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =over 4 | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =item * B<method> | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | A string containing the name of the Flickr API method you are | ||||||||||||||||||||||||||||
| 286 | calling. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =item * B<args> | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | A hash ref containing the key value pairs you are passing to | ||||||||||||||||||||||||||||
| 291 | I<method> | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =back | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | If the method encounters any errors calling the API, receives an API error | ||||||||||||||||||||||||||||
| 296 | or can not parse the response it will log an error event, via the B<log> method, | ||||||||||||||||||||||||||||
| 297 | and return undef. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Otherwise it will return a I<XML::LibXML::Document> object (if XML::LibXML is | ||||||||||||||||||||||||||||
| 300 | installed) or a I<XML::XPath> object. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =cut | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 21240 | ▶ | sub api_call { | ||||||||||||||||||||||||||
|
Subroutine
api_call: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 305 | 21240 | my $self = shift; | |||||||||||||||||||||||||||
| 306 | 21240 | my $args = shift; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # A 429 (rate limited) or 503 (unavailable) reply is retried in this | ||||||||||||||||||||||||||||
| 309 | # loop, up to $PAUSE_MAXTRIES times. This used to be done by having | ||||||||||||||||||||||||||||
| 310 | # retry_api_call call api_call recursively, which reset the retry | ||||||||||||||||||||||||||||
| 311 | # count as each nested call returned. -- claude, 2026-09-26 | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 21240 | my $res = undef; | |||||||||||||||||||||||||||
| 314 | 21240 | my $tries = 0; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 21240 | while (1) { | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # check to see if we need to take | ||||||||||||||||||||||||||||
| 319 | # breather (are we pounding or are | ||||||||||||||||||||||||||||
| 320 | # we not?) | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 21255 | $self->_await_rate_limit(); | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | # send request | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 21255 | ▶ | if (exists($args->{'args'}->{'api_sig'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | delete $args->{'args'}->{'api_sig'}; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 21255 | $args->{'args'}->{'auth_token'} = $self->{cfg}->param("flickr.auth_token"); | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | # | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 21255 | my $req = Flickr::API::Request->new($args); | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 21255 | $self->log()->debug("calling $args->{method} : " . Dumper($args->{args})); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 21255 | eval { | |||||||||||||||||||||||||||
| 339 | 21255 | $res = $self->{'api'}->execute_request($req); | |||||||||||||||||||||||||||
| 340 | }; | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 21255 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | $self->log()->error("Fatal error calling the Flickr API, $@"); | |||||||||||||||||||||||||||
| 344 | 0 | return undef; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # | ||||||||||||||||||||||||||||
| 348 | # check for 429 or 503 status | ||||||||||||||||||||||||||||
| 349 | # | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 21255 | ▶ | last unless $PAUSE_ONSTATUS{ $res->code() }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | # you are in a dark and twisty corridor | ||||||||||||||||||||||||||||
| 354 | # where all the errors look the same - | ||||||||||||||||||||||||||||
| 355 | # just give up if we hit this ceiling | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 16 | $tries ++; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 16 | ▶ | if ($tries > $PAUSE_MAXTRIES) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | my $errmsg = sprintf("service returned status %d %d times calling %s; giving up", | ||||||||||||||||||||||||||||
| 361 | 1 | $res->code(), $PAUSE_MAXTRIES, $args->{method}); | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 1 | $self->log()->error($errmsg); | |||||||||||||||||||||||||||
| 364 | 1 | return undef; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 15 | my $pause = $self->_retry_pause($res, $tries); | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 15 | $self->log()->debug(sprintf("service returned status %d, pause for %.2f seconds", | |||||||||||||||||||||||||||
| 370 | $res->code(), $pause)); | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 15 | $self->_sleep($pause); | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 21239 | return $self->parse_api_call($args, $res); | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # How long to wait before the $tries-th retry of a request that got $res. | ||||||||||||||||||||||||||||
| 379 | # Retry-After may be either a number of seconds or an HTTP date; without it, | ||||||||||||||||||||||||||||
| 380 | # we back off a little more on each try. | ||||||||||||||||||||||||||||
| 381 | 15 | ▶ | sub _retry_pause { | ||||||||||||||||||||||||||
|
Subroutine
_retry_pause: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 382 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 383 | 15 | my $res = shift; | |||||||||||||||||||||||||||
| 384 | 15 | my $tries = shift; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 15 | my $retry_after = $res->header("Retry-After"); | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 15 | ▶ | if (defined($retry_after)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 12 | ▶ | if ($retry_after =~ /\A\s*([0-9]+)\s*\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 11 | return $1; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 1 | ▶ | if (my $when = str2time($retry_after)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 1 | my $pause = $when - $self->_now; | |||||||||||||||||||||||||||
| 395 | 1 | ▶ | return ($pause > 0) ? $pause : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 3 | return $PAUSE_SECONDS_UNAVAILABLE * $tries; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | # Pacing is a token bucket: it holds up to "size" tokens, refills at "rate" | ||||||||||||||||||||||||||||
| 403 | # tokens per second, and each request sent (retries included) spends one. In | ||||||||||||||||||||||||||||
| 404 | # any window of T seconds, then, at most size + rate * T requests go out. The | ||||||||||||||||||||||||||||
| 405 | # rate is set so that this is no more than calls_per_hour for T = 3600, so a | ||||||||||||||||||||||||||||
| 406 | # burst after a lull never pushes an hour over the limit. The bucket is | ||||||||||||||||||||||||||||
| 407 | # charged when a request is sent, so request latency overlaps the wait for the | ||||||||||||||||||||||||||||
| 408 | # next token instead of adding to it. -- claude, 2026-09-26 | ||||||||||||||||||||||||||||
| 409 | 16 | ▶ | sub _init_rate_limit { | ||||||||||||||||||||||||||
|
Subroutine
_init_rate_limit: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 410 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 16 | my $per_hour = $self->{cfg}->param("flickr.api_calls_per_hour"); | |||||||||||||||||||||||||||
| 413 | 16 | ▶ | $per_hour = $CALLS_PER_HOUR_MAX unless defined($per_hour) && length($per_hour); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $per_hour and length $per_hour
MC/DC50%1/2
defined $per_hour and length $per_hour
defined $per_hour
length $per_hour
Truth table66%2/3
A and B
A= defined $per_hourB= length $per_hour
| |||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 16 | ▶ | if ($per_hour !~ /\A[0-9]+\z/ || $per_hour > $CALLS_PER_HOUR_MAX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $per_hour =~ /\A[0-9]+\z/ or $per_hour > $CALLS_PER_HOUR_MAX
MC/DC50%1/2
not $per_hour =~ /\A[0-9]+\z/ or $per_hour > $CALLS_PER_HOUR_MAX
not $per_hour =~ /\A[0-9]+\z/
$per_hour > $CALLS_PER_HOUR_MAX
Truth table66%2/3
not(A) or B
A= not $per_hour =~ /\A[0-9]+\z/B= $per_hour > $CALLS_PER_HOUR_MAX
| |||||||||||||||||||||||||||||
| 416 | 1 | warn "api_calls_per_hour must be at most $CALLS_PER_HOUR_MAX; using $CALLS_PER_HOUR_MAX\n"; | |||||||||||||||||||||||||||
| 417 | 1 | $per_hour = $CALLS_PER_HOUR_MAX; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 16 | ▶ | if ($per_hour < $CALLS_PER_HOUR_MIN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | 1 | warn "api_calls_per_hour must be at least $CALLS_PER_HOUR_MIN; using $CALLS_PER_HOUR_MIN\n"; | |||||||||||||||||||||||||||
| 422 | 1 | $per_hour = $CALLS_PER_HOUR_MIN; | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 16 | ▶ | my $size = int($per_hour / 360) || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
int $per_hour / 360 || 1
MC/DC100%1/1
int $per_hour / 360 || 1
int $per_hour / 360
Truth table100%2/2
A
A= int $per_hour / 360
| |||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 16 | $self->{'__bucket'} = {size => $size, | |||||||||||||||||||||||||||
| 428 | rate => ($per_hour - $size) / 3600, | ||||||||||||||||||||||||||||
| 429 | tokens => $size, | ||||||||||||||||||||||||||||
| 430 | at => $self->_now}; | ||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 21255 | ▶ | sub _await_rate_limit { | ||||||||||||||||||||||||||
|
Subroutine
_await_rate_limit: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 434 | 21255 | my $self = shift; | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 21255 | my $bucket = $self->{'__bucket'}; | |||||||||||||||||||||||||||
| 437 | 21255 | my $now = $self->_now; | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # The wall clock can be set backward, which must not drain the bucket. | ||||||||||||||||||||||||||||
| 440 | 21255 | my $elapsed = $now - $bucket->{at}; | |||||||||||||||||||||||||||
| 441 | 21255 | ▶ | $elapsed = 0 if $elapsed < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | $bucket->{tokens} = min($bucket->{size}, | ||||||||||||||||||||||||||||
| 444 | 21255 | $bucket->{tokens} + $elapsed * $bucket->{rate}); | |||||||||||||||||||||||||||
| 445 | 21255 | $bucket->{at} = $now; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 21255 | ▶ | if ($bucket->{tokens} < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 20972 | my $pause = (1 - $bucket->{tokens}) / $bucket->{rate}; | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 20972 | my $debug_msg = sprintf("trying not to beat up the Flickr servers, pause for %.2f seconds", | |||||||||||||||||||||||||||
| 451 | $pause); | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 20972 | $self->log()->debug($debug_msg); | |||||||||||||||||||||||||||
| 454 | 20972 | $self->_sleep($pause); | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 20972 | $bucket->{tokens} = 1; | |||||||||||||||||||||||||||
| 457 | 20972 | $bucket->{at} = $now + $pause; | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 21255 | $bucket->{tokens} --; | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | # These exist so that tests can supply a fake clock. | ||||||||||||||||||||||||||||
| 464 | 0 | ▶ | sub _now { | ||||||||||||||||||||||||||
|
Subroutine
_now: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 465 | 0 | return Time::HiRes::time(); | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 0 | ▶ | sub _sleep { | ||||||||||||||||||||||||||
|
Subroutine
_sleep: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 469 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 470 | 0 | my $seconds = shift; | |||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | 0 | Time::HiRes::sleep($seconds); | |||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =head2 $obj->get_auth() | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | Return an XML I<node> element containing the Flickr auth token information for | ||||||||||||||||||||||||||||
| 478 | the current object. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | Returns undef if no token information is present. | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =cut | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | 0 | ▶ | sub get_auth { | ||||||||||||||||||||||||||
|
Subroutine
get_auth: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 485 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | ▶ | if (! $self->{'__auth'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | 0 | my $auth = $self->api_call({"method" => "flickr.auth.checkToken"}); | |||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 0 | ▶ | if (! $auth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 0 | return undef; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 0 | my $nsid = $auth->find("/rsp/auth/user/\@nsid")->string_value(); | |||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 0 | ▶ | if (! $nsid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | 0 | $self->log()->error("unabled to determine ID for token"); | |||||||||||||||||||||||||||
| 498 | 0 | return undef; | |||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 0 | $self->{'__auth'} = $auth; | |||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 0 | return $self->{'__auth'}; | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | =head2 $obj->get_auth_nsid() | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | Return the Flickr NSID of the user associated with the Flickr auth token information | ||||||||||||||||||||||||||||
| 510 | for the current object. | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Returns undef if no token information is present. | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =cut | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 0 | ▶ | sub get_auth_nsid { | ||||||||||||||||||||||||||
|
Subroutine
get_auth_nsid: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 517 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 0 | ▶ | if (my $auth = $self->get_auth()){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 0 | return $auth->find("/rsp/auth/user/\@nsid")->string_value(); | |||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | 0 | return undef; | |||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | 21239 | ▶ | sub parse_api_call { | ||||||||||||||||||||||||||
|
Subroutine
parse_api_call: called CC=5 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 527 | 21239 | my $self = shift; | |||||||||||||||||||||||||||
| 528 | 21239 | my $args = shift; | |||||||||||||||||||||||||||
| 529 | 21239 | my $res = shift; | |||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 21239 | $self->log()->debug($res->decoded_content()); | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 21239 | my $xml = $self->_parse_results_xml($res); | |||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | 21239 | ▶ | if (! $xml) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 536 | 1 | $self->log()->error("failed to parse API response, calling $args->{method}"); | |||||||||||||||||||||||||||
| 537 | 1 | $self->log()->error($res->decoded_content()); | |||||||||||||||||||||||||||
| 538 | 1 | return undef; | |||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | 21238 | my $stat = $xml->find("/rsp/\@stat")->string_value(); | |||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | 21238 | ▶ | if ($stat eq "fail") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 0 | my $code = $xml->findvalue("/rsp/err/\@code"); | |||||||||||||||||||||||||||
| 545 | 0 | my $msg = $xml->findvalue("/rsp/err/\@msg"); | |||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 0 | $self->log()->error(sprintf("[%s] %s (calling $args->{method})\n", | |||||||||||||||||||||||||||
| 548 | $code, | ||||||||||||||||||||||||||||
| 549 | $msg)); | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 0 | ▶ | if ($code==0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 552 | 0 | $self->log()->info(sprintf("api disabled attempting %s/%s tries to see if it's come back up", $self->{'__retries'}, $RETRY_MAXTRIES)); | |||||||||||||||||||||||||||
| 553 | 0 | return $self->api_disabled($args, $res); | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 21238 | $self->{'__retries'} = 0; | |||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | 21238 | ▶ | return ($@) ? undef : $xml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | 21239 | ▶ | sub _parse_results_xml { | ||||||||||||||||||||||||||
|
Subroutine
_parse_results_xml: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||
| 563 | 21239 | my $self = shift; | |||||||||||||||||||||||||||
| 564 | 21239 | my $res = shift; | |||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | 21239 | my $xml = undef; | |||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | # | ||||||||||||||||||||||||||||
| 569 | # Please for Cal to someday accept the patch to add | ||||||||||||||||||||||||||||
| 570 | # response handlers to Flickr::API... | ||||||||||||||||||||||||||||
| 571 | # | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | 21239 | ▶ | if ($self->{cfg}->param("flickr.api_handler") eq "XPath") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | 0 | eval "require XML::XPath"; | |||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | 0 | ▶ | if (! $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 577 | 0 | eval { | |||||||||||||||||||||||||||
| 578 | 0 | $xml = XML::XPath->new(xml=>$res->decoded_content()); | |||||||||||||||||||||||||||
| 579 | }; | ||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | else { | ||||||||||||||||||||||||||||
| 584 | 21239 | eval "require XML::LibXML"; | |||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | 21239 | ▶ | if (! $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 587 | 21239 | eval { | |||||||||||||||||||||||||||
| 588 | 21239 | my $parser = XML::LibXML->new(); | |||||||||||||||||||||||||||
| 589 | 21239 | $xml = $parser->parse_string($res->decoded_content()); | |||||||||||||||||||||||||||
| 590 | }; | ||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | # | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 21239 | ▶ | if (! $xml) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 597 | 1 | $self->log()->error("XML parse error : $@"); | |||||||||||||||||||||||||||
| 598 | 1 | return undef; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | # | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | 21238 | return $xml; | |||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | 0 | ▶ | sub api_disabled { | ||||||||||||||||||||||||||
|
Subroutine
api_disabled: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 607 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 608 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 609 | 0 | my $res = shift; | |||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | 0 | $self->{'__retries'} ++; | |||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | 0 | ▶ | if ($self->{'__retries'} > $RETRY_MAXTRIES) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 614 | 0 | $self->log()->critical(sprintf("API still down after %s tries - exiting", $RETRY_MAXTRIES)); | |||||||||||||||||||||||||||
| 615 | 0 | exit; | |||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 0 | my $pause = $PAUSE_SECONDS_UNAVAILABLE * $self->{'__retries'}; | |||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | 0 | $self->log()->debug(sprintf("api disabled, pause for %.2f seconds", $pause)); | |||||||||||||||||||||||||||
| 621 | 0 | $self->_sleep($pause); | |||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | # try, try again | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 0 | $res = $self->api_call($args); | |||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 0 | ▶ | if (! $res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 628 | 0 | $self->log()->critical("Returned false during 'api disabled' retry. That can only be bad - exiting"); | |||||||||||||||||||||||||||
| 629 | 0 | exit; | |||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | 0 | return $res; | |||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | =head2 $obj->upload(\%args) | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | This is a helper method that simply wraps calls to the I<Flickr::Upload> upload | ||||||||||||||||||||||||||||
| 638 | method. All the arguments are the same. For complete documentation please consult: | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | L<http://search.cpan.org/dist/Flickr-Upload/Upload.pm#upload> | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | (Note: There's no need to pass an auth_token argument as the wrapper will take care | ||||||||||||||||||||||||||||
| 643 | of for you.) | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | Returns a photo ID (or a ticket ID if the call is asynchronous) on success or false | ||||||||||||||||||||||||||||
| 646 | if there was a problem. | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | =cut | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | 0 | ▶ | sub upload { | ||||||||||||||||||||||||||
|
Subroutine
upload: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 651 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 652 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | 0 | $args->{'auth_token'} = $self->{cfg}->param("flickr.auth_token"); | |||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | my $ua = Flickr::Upload->new({'key' => $self->{cfg}->param("flickr.api_key"), | ||||||||||||||||||||||||||||
| 657 | 0 | 'secret' => $self->{cfg}->param("flickr.api_secret")}); | |||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | 0 | my $id = undef; | |||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | 0 | eval { | |||||||||||||||||||||||||||
| 662 | 0 | $id = $ua->upload(%$args); | |||||||||||||||||||||||||||
| 663 | }; | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | 0 | ▶ | if ($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 666 | 0 | $self->log()->error("upload failed: $@"); | |||||||||||||||||||||||||||
| 667 | 0 | return 0; | |||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | 0 | return $id; | |||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | =head2 $obj->log() | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | Returns a I<Log::Dispatch> object. | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | =cut | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | 63517 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 680 | 63517 | my $self = shift; | |||||||||||||||||||||||||||
| 681 | 63517 | return $self->{'__logger'}; | |||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | =head1 VERSION | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | 1.8 | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | =head1 DATE | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | $Date: 2009/08/02 17:16:12 $ | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | Aaron Straup Cope E<lt>ascope@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | L<Config::Simple> | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | L<Flickr::API> | ||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | L<XML::XPath> | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | L<XML::LibXML> | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | =head1 BUGS | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | Please report all bugs via http://rt.cpan.org/ | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | Copyright (c) 2005-2008 Aaron Straup Cope. All Rights Reserved. | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | This is free software. You may redistribute it and/or | ||||||||||||||||||||||||||||
| 715 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | =cut | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | 3 | return 1; | |||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | __END__ | ||||||||||||||||||||||||||||