added add torrent command
[cloudstore.git] / torrent / transmission-client.pl
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 transmission-client.pl - Alternative to transmission-remote
6
7 =head1 SYNOPSIS
8
9  transmission-client.pl list;
10  transmission-client.pl session;
11  transmission-client.pl session $key $value;
12  transmission-client.pl stats;
13
14 =head1 DESCRIPTION
15
16 This is an example application for L<Transmission::Client>
17
18 =cut
19
20 use strict;
21 use warnings;
22 use lib qw(lib);
23 use Transmission::Client;
24
25 my $action = shift @ARGV or _help();
26
27 my ( $username, $password ) = split(/:/, $ENV{TR_AUTH} );
28
29 my $tc = Transmission::Client->new(
30         autodie => 1,
31         username => $username,
32         password => $password,
33 );
34
35 if($action eq 'list') {
36     printf "%3s %-34s %4s %4s %5s %5s\n", 'id', 'name', 'lcrs', 'sdrs', 'rate', 'eta';
37     print "-" x 79, "\n";
38     for my $torrent ($tc->read_torrents) {
39         printf "%3i %-34s %4s %4s %5s %5s\n",
40             $torrent->id,
41             substr($torrent->name, 0, 34),
42             _peers($torrent->leechers),
43             _peers($torrent->seeders),
44             _rate($torrent->rate_download),
45             _eta($torrent->eta),
46             ;
47     }
48 }
49 elsif($action eq 'session') {
50     if(my $set = shift @ARGV) {
51         $tc->session->$set(shift @ARGV);
52         $tc->session->${ \"clear_$set" };
53         printf "%s: %s\n", $set, $tc->session->$set;
54         print $tc->error;
55     }
56     else {
57         my $res = $tc->session->read_all;
58         for my $key (sort keys %$res) {
59             printf "%-30s %s\n", $key, $res->{$key};
60         }
61     }
62 }
63 elsif($action eq 'stats') {
64     my $res = $tc->session->stats->read_all;
65     for my $key (sort keys %$res) {
66         printf "%-30s %s\n", $key, $res->{$key};
67     }
68 }
69
70 elsif($action eq 'add') {
71         $tc->add(
72                 filename => $ARGV[0],
73                 download_dir => '/rsync1/s1/torrent/download',
74         );
75 }
76
77 else {
78     _help();
79 }
80
81 print "\n";
82
83 #==============================================================================
84 sub _peers {
85     my $n = shift;
86
87     if($n < 0) {
88         return 'na';
89     }
90     elsif($n < 9999) {
91         return $n;
92     }
93     else {
94         return '++';
95     }
96 }
97 sub _rate {
98     my $kbps = shift;
99
100     if($kbps < 0) {
101         return '0';
102     }
103     elsif($kbps < 1000) {
104         return $kbps;
105     }
106     elsif($kbps < 1e6) {
107         return int($kbps / 1e3) . 'k';
108     }
109     elsif($kbps < 1e6) {
110         return int($kbps / 1e6) . 'M';
111     }
112     else {
113         return '++';
114     }
115 }
116
117 sub _eta {
118     my $sec = shift;
119
120     if($sec < 0) {
121         return 'inf';
122     }
123     elsif($sec < 60) {
124         return $sec . "s";
125     }
126     elsif($sec < 3600) {
127         return int($sec / 6) / 10 . "m";
128     }
129     elsif($sec < 86400) {
130         return int($sec / 360) / 10 . "h";
131     }
132     else {
133         return '>1d';
134     }
135 }
136
137 sub _help {
138     exec perldoc => -tT => $0;
139 }
140
141 =head1 LICENSE
142
143 This library is free software; you can redistribute it and/or modify it under
144 the same terms as Perl itself.
145
146 =head1 AUTHOR
147
148 Jan Henning Thorsen
149
150 =cut
151
152 exit;