File Coverage

blib/lib/WWW/Opentracker/Stats.pm
Criterion Covered Total %
statement 37 51 72.5
branch 3 8 37.5
condition 1 3 33.3
subroutine 9 13 69.2
pod 4 7 57.1
total 54 82 65.8


line stmt bran cond sub pod time code
1             package WWW::Opentracker::Stats;
2              
3 1     1   23178 use 5.008008;
  1         5  
  1         38  
4 1     1   5 use strict;
  1         2  
  1         38  
5 1     1   4 use warnings;
  1         2  
  1         61  
6              
7              
8             our $VERSION = '1.11';
9              
10             require Class::Accessor::Fast;
11 1         6 use parent qw/
12             Class::Accessor::Fast
13             Class::Data::Inheritable
14 1     1   821 /;
  1         335  
15              
16 1     1   7739 use Params::Validate qw(:all);
  1         11287  
  1         525  
17              
18             __PACKAGE__->mk_accessors(qw/
19             _statsurl
20             _useragent
21             _debug
22             /);
23              
24             our %MODES = (
25             'tpbs' => __PACKAGE__.'::Mode::TPBS',
26             'peer' => __PACKAGE__.'::Mode::Peer',
27             'fscp' => __PACKAGE__.'::Mode::Fullscrape',
28             'top10' => __PACKAGE__.'::Mode::Top10',
29             'tcp4' => __PACKAGE__.'::Mode::TCP4',
30             'herr' => __PACKAGE__.'::Mode::HttpErrors',
31             'udp4' => __PACKAGE__.'::Mode::UDP4',
32             'scrp' => __PACKAGE__.'::Mode::Scrape',
33             'renew' => __PACKAGE__.'::Mode::Renew',
34             'torr' => __PACKAGE__.'::Mode::Torr',
35             'conn' => __PACKAGE__.'::Mode::Conn',
36             );
37              
38             =head1 NAME
39              
40             WWW::Opentracker::Stats - Perl module for retrieve statistics from Opentracker
41              
42             =head1 SYNOPSIS
43              
44             use WWW::Opentracker::Stats;
45              
46             my $ot_stats = WWW::Opentracker::Stats->new({
47             'statsurl' => 'http://localhost:6969/stats',
48             });
49              
50             my $stats_ref = $ot_stats->stats(qw/tpbs peer/);
51              
52             my $tpbs_stats = $stats_ref->{'tpbs'};
53              
54             print "Downloads:\n";
55             while (my ($torrent, $tstats) = each %{ $tpbs_stats->{'files'} }) {
56             print "$torrent: " . $tstats->{'downloaded'} . "\n";
57             }
58              
59              
60             =head1 DESCRIPTION
61              
62             Provides an easy to use interface to retrieve various statistics from
63             "opentracker", a BitTorrent tracker.
64              
65             It executes HTTP requests to opentrackers web services, parses the
66             response and returns data structures that you can easily extract data from,
67             to store it in a database or display it on the web.
68              
69              
70             =head1 METHODS
71              
72             =head2 new
73              
74             Args: $class, $args
75              
76             Constructor. Creates a new instance of the class.
77              
78             It takes a HASH/HASHREF of arguments.
79             - statsurl (mandatory)
80             - useragent (optional)
81              
82             =cut
83              
84             sub new {
85 1     1 1 13490 my $class = shift;
86              
87 1         69 my %p = validate(@_,
88             {
89             'statsurl' => {
90             'type' => SCALAR,
91             },
92             'useragent' => {
93             'isa' => 'LWP::UserAgent',
94             'default' => undef,
95             },
96             'debug' => {
97             'default' => undef,
98             },
99             },
100             );
101              
102 1 50       11 $class = ref $class if ref $class;
103              
104 1         3 my $self = bless {}, $class;
105              
106 1         8 $self->_debug($p{'debug'});
107 1         23 $self->_statsurl($p{'statsurl'});
108              
109 1   33     9 $p{'useragent'} ||= $self->default_useragent;
110 1         5 $self->_useragent($p{'useragent'});
111              
112 1         7 return $self;
113             }
114              
115              
116             =head2 default_useragent
117              
118             Args: $self
119              
120             Creates a default user agent that can be used to fetch statistics from
121             opentracker. See L for details.
122              
123             =cut
124              
125             sub default_useragent {
126 0     0 1 0 my ($self) = @_;
127              
128 0 0       0 print STDERR "Creating a new default user agent\n"
129             if $self->_debug;
130              
131 1     1   576 use WWW::Opentracker::Stats::UserAgent;
  1         4  
  1         402  
132 0         0 return WWW::Opentracker::Stats::UserAgent->default;
133             }
134              
135              
136             =head2 params
137              
138             Args: $self
139              
140             Returns a HASHREF with properties that can be passed on to the constructor of
141             the statistics mode packages.
142              
143             =cut
144              
145             sub params {
146 1     1 1 3 my ($self) = @_;
147              
148             return {
149 1         5 'statsurl' => $self->_statsurl,
150             'useragent' => $self->_useragent,
151             'debug' => $self->_debug,
152             };
153             }
154              
155              
156             sub stats {
157 0     0 0 0 my ($self, @modes) = @_;
158              
159 0         0 my %all = ();
160 0         0 for my $mode (@modes) {
161 0         0 my $stats = $self->stats_by_mode($mode);
162 0         0 $all{$mode} = $stats;
163             }
164              
165 0         0 return \%all;
166             }
167              
168             sub stats_by_mode {
169 0     0 0 0 my ($self, $mode) = @_;
170              
171 0         0 my $obj = $self->get_mode($mode);
172              
173 0         0 return $obj->stats;
174             }
175              
176             sub get_mode {
177 1     1 0 855 my ($self, $mode) = @_;
178              
179 1         4 my $package = $MODES{$mode};
180 1 50       9 die "Unavailable mode: $mode"
181             unless $package;
182              
183 1         5 my $params = $self->params;
184              
185 1         119 eval "require $package;";
186 1 50       10 if ($@) {
187 0         0 die "Failed to load $package: $@";
188             }
189              
190 1         10 my $obj = $package->new($params);
191              
192 1         6 return $obj;
193             }
194              
195              
196             =head2 available_modes
197              
198             Returns all the available modes as an array.
199              
200             =cut
201              
202             sub available_modes {
203 0     0 1   return keys %MODES;
204             }
205              
206              
207             =head1 SEE ALSO
208              
209             L
210              
211             Opentracker: L<< http://erdgeist.org/arts/software/opentracker/ >>.
212              
213              
214             =head1 AUTHOR
215              
216             Knut-Olav Hoven, Eknutolav@gmail.comE
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             Copyright (C) 2009 by Knut-Olav Hoven
221              
222             This library is free software; you can redistribute it and/or modify
223             it under the same terms as Perl itself, either Perl version 5.8.8 or,
224             at your option, any later version of Perl 5 you may have available.
225              
226             =cut
227              
228              
229             1;