line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
598224
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
2
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
70
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package WebService::TVDB; |
5
|
|
|
|
|
|
|
{ |
6
|
|
|
|
|
|
|
$WebService::TVDB::VERSION = '1.133200'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ABSTRACT: Interface to http://thetvdb.com/ |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
531
|
use WebService::TVDB::Languages qw($languages); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
138
|
|
12
|
1
|
|
|
1
|
|
949
|
use WebService::TVDB::Series; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use WebService::TVDB::Util qw(get_api_key_from_file); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Carp qw(carp); |
16
|
|
|
|
|
|
|
use LWP::Simple (); |
17
|
|
|
|
|
|
|
use URI::Escape qw(uri_escape); |
18
|
|
|
|
|
|
|
use XML::Simple qw(:strict); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use constant SEARCH_URL => |
21
|
|
|
|
|
|
|
'http://thetvdb.com/api/GetSeries.php?seriesname=%s&language=%s'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use constant API_KEY_FILE => '/.tvdb'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Object::Tiny qw( |
26
|
|
|
|
|
|
|
api_key |
27
|
|
|
|
|
|
|
language |
28
|
|
|
|
|
|
|
max_retries |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
|
|
|
|
|
|
my $class = shift; |
33
|
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
unless ( $self->api_key ) { |
36
|
|
|
|
|
|
|
require File::HomeDir; |
37
|
|
|
|
|
|
|
$self->{api_key} = |
38
|
|
|
|
|
|
|
get_api_key_from_file( File::HomeDir->my_home . API_KEY_FILE ); |
39
|
|
|
|
|
|
|
die 'Can\'t find API key' unless $self->api_key; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
unless ( $self->language ) { |
43
|
|
|
|
|
|
|
$self->{language} = 'English'; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
unless ( $self->max_retries ) { |
47
|
|
|
|
|
|
|
$self->{max_retries} = 10; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub search { |
54
|
|
|
|
|
|
|
my ( $self, $term ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
unless ($term) { |
57
|
|
|
|
|
|
|
die 'search term is required'; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $url = sprintf( SEARCH_URL, |
61
|
|
|
|
|
|
|
uri_escape($term), $languages->{ $self->language }->{abbreviation} ); |
62
|
|
|
|
|
|
|
my $agent = $LWP::Simple::ua->agent; |
63
|
|
|
|
|
|
|
$LWP::Simple::ua->agent("WebService::TVDB/$WebService::TVDB::VERSION"); |
64
|
|
|
|
|
|
|
my $xml = LWP::Simple::get($url); |
65
|
|
|
|
|
|
|
my $retries = 0; |
66
|
|
|
|
|
|
|
until ( defined $xml || $retries == $self->max_retries ) { |
67
|
|
|
|
|
|
|
carp "failed to get URL $url - retrying"; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# TODO configurable wait time |
70
|
|
|
|
|
|
|
sleep 1; |
71
|
|
|
|
|
|
|
$xml = LWP::Simple::get($url); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$retries++; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
$LWP::Simple::ua->agent($agent); |
76
|
|
|
|
|
|
|
unless ($xml) { |
77
|
|
|
|
|
|
|
die "failed to get URL $url after $retries retries. Aborting."; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
$self->{series} = _parse_series( |
80
|
|
|
|
|
|
|
XML::Simple::XMLin( |
81
|
|
|
|
|
|
|
$xml, |
82
|
|
|
|
|
|
|
ForceArray => ['Series'], |
83
|
|
|
|
|
|
|
KeyAttr => 'Series', |
84
|
|
|
|
|
|
|
SuppressEmpty => 1 |
85
|
|
|
|
|
|
|
), |
86
|
|
|
|
|
|
|
$self->api_key, |
87
|
|
|
|
|
|
|
$languages->{ $self->language }, |
88
|
|
|
|
|
|
|
$self->max_retries |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return $self->{series}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub get { |
95
|
|
|
|
|
|
|
my ( $self, $id ) = @_; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
die 'id is required' unless $id; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$self->{series} = _parse_series( |
100
|
|
|
|
|
|
|
{ |
101
|
|
|
|
|
|
|
Series => [ |
102
|
|
|
|
|
|
|
{ |
103
|
|
|
|
|
|
|
seriesid => $id, |
104
|
|
|
|
|
|
|
language => $languages->{ $self->language }->{abbreviation}, |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
] |
107
|
|
|
|
|
|
|
}, |
108
|
|
|
|
|
|
|
$self->api_key, |
109
|
|
|
|
|
|
|
$languages->{ $self->language }, |
110
|
|
|
|
|
|
|
$self->max_retries |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
$self->{series}->[0]->fetch(); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
return $self->{series}->[0]; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# parse the series xml and return an array of WebService::TVDB::Series |
119
|
|
|
|
|
|
|
sub _parse_series { |
120
|
|
|
|
|
|
|
my ( $xml, $api_key, $api_language, $max_retries ) = @_; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# loop over results and create new series objects |
123
|
|
|
|
|
|
|
my @series; |
124
|
|
|
|
|
|
|
for ( @{ $xml->{Series} } ) { |
125
|
|
|
|
|
|
|
push @series, |
126
|
|
|
|
|
|
|
WebService::TVDB::Series->new( |
127
|
|
|
|
|
|
|
%$_, |
128
|
|
|
|
|
|
|
_api_key => $api_key, |
129
|
|
|
|
|
|
|
_api_language => $api_language, |
130
|
|
|
|
|
|
|
_max_retries => $max_retries |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
return \@series; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
__END__ |