File Coverage

blib/lib/P2P/Transmission/Remote.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package P2P::Transmission::Remote;
2              
3 1     1   4 use strict;
  1         2  
  1         31  
4 1     1   17 use 5.8.1;
  1         3  
  1         57  
5             our $VERSION = '0.02';
6              
7 1     1   6 use Carp;
  1         1  
  1         74  
8 1     1   1166 use JSON::XS;
  1         13069  
  1         92  
9 1     1   5771 use LWP::UserAgent;
  1         119179  
  1         45  
10 1     1   13 use URI;
  1         2  
  1         27  
11              
12 1     1   510 use Moose;
  0            
  0            
13             use Moose::Util::TypeConstraints;
14              
15             subtype 'Uri'
16             => as 'Object'
17             => where { $_->isa('URI') };
18              
19             coerce 'Uri'
20             => from 'Object'
21             => via { $_->isa('URI')
22             ? $_ : Params::Coerce::coerce( 'URI', $_ ) }
23             => from 'Str'
24             => via { URI->new( $_, 'http' ) };
25              
26             has url => (
27             is => 'rw',
28             isa => 'Uri',
29             default => sub { URI->new("http://localhost:9091/") },
30             lazy => 1,
31             coerce => 1,
32             );
33              
34             has user_agent => (
35             is => 'rw',
36             isa => 'LWP::UserAgent',
37             default => sub { LWP::UserAgent->new },
38             lazy => 1,
39             );
40              
41             has username => (
42             is => 'rw',
43             isa => 'Str',
44             );
45              
46             has password => (
47             is => 'rw',
48             isa => 'Str',
49             );
50              
51             sub _prepare_auth {
52             my $self = shift;
53             if ( $self->username && $self->password ) {
54             # set Digest auth credentials
55             $self->user_agent->credentials( $self->url->host_port, "Transmission RPC Server", $self->username, $self->password );
56             }
57             }
58              
59             sub _request {
60             my($self, $method, $args) = @_;
61              
62             my $url = $self->url . "transmission/rpc";
63              
64             my $req = HTTP::Request->new( POST => $url );
65             $req->header( Accept => "application/json, text/javascript, */*" );
66             $req->header( "Content-Type" => "application/json" );
67              
68             $self->_prepare_auth;
69              
70             my $body = JSON::XS::encode_json({
71             method => $method,
72             arguments => $args,
73             });
74              
75             $req->header( "Content-Length" => length($body) );
76             $req->content($body);
77              
78             my $ua = $self->user_agent;
79             my $res = $ua->request( $req );
80              
81             my $result = JSON::XS::decode_json( $res->content );
82              
83             if ($result->{result} ne 'success') {
84             croak $result->{result};
85             }
86              
87             return $result->{arguments};
88             }
89              
90             sub _cmd_torrents {
91             my($self, $methods, @torrents) = @_;
92             $self->_request($methods, { ids => [ map $_->{id}, @torrents ] });
93             }
94              
95             sub torrents {
96             my $self = shift;
97              
98             my $res = $self->_request("torrent-get", {
99             fields => [ "addedDate","announceURL","comment","creator","dateCreated",
100             "downloadedEver","error","errorString","eta","hashString","haveUnchecked","haveValid",
101             "id","isPrivate","leechers","leftUntilDone","name","peersGettingFromUs","peersKnown",
102             "peersSendingToUs","rateDownload","rateUpload","seeders","sizeWhenDone","status","swarmSpeed",
103             "totalSize","uploadedEver" ],
104             });
105              
106             return @{ $res->{torrents} };
107             }
108              
109             sub start {
110             my $self = shift;
111             $self->_cmd_torrents("torrent-start", @_);
112             }
113              
114             sub stop {
115             my $self = shift;
116             $self->_cmd_torrents("torrent-stop", @_);
117             }
118              
119             sub remove {
120             my $self = shift;
121             $self->_cmd_torrents("torrent-remove", @_);
122             }
123              
124             1;
125             __END__
126              
127             =encoding utf-8
128              
129             =for stopwords API url
130              
131             =head1 NAME
132              
133             P2P::Transmission::Remote - Control Transmission using its Remote API
134              
135             =head1 SYNOPSIS
136              
137             use P2P::Transmission::Remote;
138              
139             my $client = P2P::Transmission::Remote->new;
140             for my $torrent ($client->torrents) {
141             print $torrent->{name};
142             $client->stop($torrent);
143             }
144              
145             =head1 DESCRIPTION
146              
147             P2P::Transmission::Remote is a client module to control torrent
148             software Transmission using its Remote API. You need to enable its
149             Remote and allows access from your client machine (usually localhost).
150              
151             =head1 METHODS
152              
153             =over 4
154              
155             =item url
156              
157             Gets and sets the URL of Transmission Remote API. Defaults to I<http://localhost:9091/>.
158              
159             =item user_agent
160              
161             Gets and sets the User Agent object to make API calls.
162              
163             =item torrents
164              
165             my @torrents = $client->torrents;
166              
167             Gets the list of Torrent data.
168              
169             =item start, stop, remove
170              
171             $client->start(@torrents);
172             $client->stop(@torrents);
173             $client->remove(@torrents);
174              
175             Starts, stops and removes the torrent transfer.
176              
177             =item upload
178              
179             $client->upload($torrent_path);
180              
181             Adds a new torrent by uploading the file.
182              
183             =back
184              
185             =head1 AUTHOR
186              
187             Tatsuhiko Miyagawa E<lt>miyagawa@cpan.orgE<gt>
188              
189             =head1 LICENSE
190              
191             This library is free software; you can redistribute it and/or modify
192             it under the same terms as Perl itself.
193              
194             =head1 SEE ALSO
195              
196             L<P2P::Transmission>
197              
198             =cut