line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Opentracker::Stats::Mode; |
2
|
|
|
|
|
|
|
|
3
|
13
|
|
|
13
|
|
38625
|
use strict; |
|
13
|
|
|
|
|
176
|
|
|
13
|
|
|
|
|
460
|
|
4
|
13
|
|
|
13
|
|
71
|
use warnings; |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
647
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Class::Accessor::Fast; |
7
|
13
|
|
|
|
|
99
|
use parent qw/ |
8
|
|
|
|
|
|
|
Class::Accessor::Fast |
9
|
|
|
|
|
|
|
Class::Data::Inheritable |
10
|
13
|
|
|
13
|
|
1106
|
/; |
|
13
|
|
|
|
|
353
|
|
11
|
|
|
|
|
|
|
|
12
|
13
|
|
|
13
|
|
96836
|
use Carp; |
|
13
|
|
|
|
|
35
|
|
|
13
|
|
|
|
|
1422
|
|
13
|
13
|
|
|
13
|
|
16627
|
use Params::Validate qw(:all); |
|
13
|
|
|
|
|
196123
|
|
|
13
|
|
|
|
|
12131
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata('_format'); |
17
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata('_mode'); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/ |
20
|
|
|
|
|
|
|
_statsurl |
21
|
|
|
|
|
|
|
_useragent |
22
|
|
|
|
|
|
|
_debug |
23
|
|
|
|
|
|
|
/); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 NAME |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
WWW::Opentracker::Stats::Mode - Base module for the different modes |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 SYNOPSIS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
use WWW::Opentracker::Stats::Mode::TPBS; |
33
|
|
|
|
|
|
|
my $tpbs = WWW::Opentracker::Stats::Mode::TPBS->new( |
34
|
|
|
|
|
|
|
{ |
35
|
|
|
|
|
|
|
'statsurl' => 'http://localhost:6969/stats', |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $stats = $tpbs->stats(); |
40
|
|
|
|
|
|
|
printf "%d torrents served", scalar @{$stats->{'files'}}; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Provides accessability for fetching and parsing the statistics from Opentracker. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 new |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Args: $class, $args |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Constructor. Creates a new instance of the class. |
54
|
|
|
|
|
|
|
This constructor is also used by all sub statistics packages. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
It takes a HASH/HASHREF of arguments. |
57
|
|
|
|
|
|
|
- statsurl (mandatory) |
58
|
|
|
|
|
|
|
- useragent (mandatory) |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub new { |
63
|
12
|
|
|
12
|
1
|
37
|
my $class = shift; |
64
|
|
|
|
|
|
|
|
65
|
12
|
|
|
|
|
876
|
my %p = validate(@_, |
66
|
|
|
|
|
|
|
{ |
67
|
|
|
|
|
|
|
'statsurl' => { |
68
|
|
|
|
|
|
|
'type' => SCALAR, |
69
|
|
|
|
|
|
|
}, |
70
|
|
|
|
|
|
|
'useragent' => { |
71
|
|
|
|
|
|
|
'isa' => 'LWP::UserAgent', |
72
|
|
|
|
|
|
|
}, |
73
|
|
|
|
|
|
|
'debug' => { |
74
|
|
|
|
|
|
|
'default' => undef, |
75
|
|
|
|
|
|
|
}, |
76
|
|
|
|
|
|
|
}, |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
12
|
50
|
|
|
|
463
|
$class = ref $class if ref $class; |
80
|
|
|
|
|
|
|
|
81
|
12
|
|
|
|
|
55
|
my $self = bless {}, $class; |
82
|
|
|
|
|
|
|
|
83
|
12
|
|
|
|
|
136
|
$self->_statsurl($p{'statsurl'}); |
84
|
12
|
|
|
|
|
340
|
$self->_useragent($p{'useragent'}); |
85
|
12
|
|
|
|
|
285
|
$self->_debug($p{'debug'}); |
86
|
|
|
|
|
|
|
|
87
|
12
|
|
|
|
|
135
|
$self->_require_impl; |
88
|
|
|
|
|
|
|
|
89
|
12
|
|
|
|
|
259
|
return $self; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 stats |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Args: $self |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Fetches statistics from the opentracker server over a HTTP channel, |
98
|
|
|
|
|
|
|
decodes the content in the HTTP response and returns the statistics data |
99
|
|
|
|
|
|
|
structure. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
It caches the statistics for the entire lifetime of the object. |
102
|
|
|
|
|
|
|
If something is found in the cache, |
103
|
|
|
|
|
|
|
it is returned instead of contacting the server. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub stats { |
108
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
0
|
return $self->_stats if defined $self->_stats; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
0
|
my $payload = $self->fetch; |
113
|
0
|
|
|
|
|
0
|
my $stats = $self->parse_stats($payload); |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
0
|
$self->_stats($stats); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
0
|
return $stats; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 parse_stats |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Args: $self, $payload |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Returns the payload unchanged. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
WARNING This method should really, really be implemented by a subclass. |
128
|
|
|
|
|
|
|
It should return a HASHREF with a sane structure of the statistics data. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub parse_stats { |
133
|
0
|
|
|
0
|
1
|
0
|
my ($self, $payload) = @_; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
0
|
warn "You should override this method in the subclass. This method should return a HASHREF"; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
0
|
return $payload; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 fetch |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Args: $self |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Makes a HTTP request to the opentracker statistics service |
146
|
|
|
|
|
|
|
using the implementation (sub) class' mode and format settings. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Returns the content of the response unless there was an error. |
149
|
|
|
|
|
|
|
Dies on errors. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub fetch { |
154
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
0
|
my $ua = $self->_useragent; |
157
|
0
|
|
|
|
|
0
|
my $url = $self->url; |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
0
|
print STDERR "Retrieving stats from url: $url\n" |
160
|
|
|
|
|
|
|
if $self->_debug; |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
|
|
0
|
my $response = $ua->get($url); |
163
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
0
|
if ($response->is_success) { |
165
|
0
|
|
|
|
|
0
|
return $response->decoded_content(charset => 'none'); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
else { |
168
|
0
|
|
|
|
|
0
|
die $response->status_line; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 url |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Args: $self |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Assembles the URL to the opentracker statistics based on the statsurl, |
178
|
|
|
|
|
|
|
format and mode. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Returns the URL as a string. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub url { |
185
|
11
|
|
|
11
|
1
|
16083
|
my ($self) = @_; |
186
|
|
|
|
|
|
|
|
187
|
11
|
|
|
|
|
55
|
my $url = sprintf( |
188
|
|
|
|
|
|
|
'%s?format=%s&mode=%s', |
189
|
|
|
|
|
|
|
$self->_statsurl, |
190
|
|
|
|
|
|
|
$self->_format, |
191
|
|
|
|
|
|
|
$self->_mode |
192
|
|
|
|
|
|
|
); |
193
|
|
|
|
|
|
|
|
194
|
11
|
|
|
|
|
315
|
return $url; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 parse_thousands |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Args: $self, $number |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Parses a string that represents a number with a thousands delimiter. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=cut |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub parse_thousands { |
207
|
8
|
|
|
8
|
1
|
13
|
my ($self, $number) = @_; |
208
|
|
|
|
|
|
|
|
209
|
8
|
|
|
|
|
16
|
$number =~ s{[\'\.]}{}g; |
210
|
|
|
|
|
|
|
|
211
|
8
|
|
|
|
|
32
|
return $number; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 _require_impl |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Private method |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Args: $self |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Croaks from the perspect of the caller two steps up the call stack if the |
222
|
|
|
|
|
|
|
method is not called from a subclass implementation. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=cut |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub _require_impl { |
227
|
12
|
|
|
12
|
|
30
|
my ($self) = @_; |
228
|
|
|
|
|
|
|
|
229
|
12
|
50
|
|
|
|
88
|
return unless ref $self eq __PACKAGE__; |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
local $Carp::CarpLevel = 2; |
232
|
0
|
|
|
|
|
|
croak "You can not use this package directly. Use a subclass."; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head1 AUTHOR |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Knut-Olav Hoven, Eknutolav@gmail.comE |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Copyright (C) 2009 by Knut-Olav Hoven |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
245
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
246
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=cut |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
1; |