-
Notifications
You must be signed in to change notification settings - Fork 5
/
torrent-add
executable file
·57 lines (45 loc) · 1.28 KB
/
torrent-add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
use strict;
use warnings;
my $host=`twipc --fetch`;
chomp $host;
my $port="9091";
my $user="pi";
my $pw="raspberry";
my $rpcUrl = "http://$host:$port/transmission/rpc";
my $paused = 'false';
my $usage = "$0 magnet [magnet magnet ..]\n";
sub main(@){
die $usage if @_ == 0 or $_[0] =~ /^(-h|--help)$/;
my @sessIdCmd = curlCmd();
my $sessIdResponse = `@sessIdCmd`;
my $sessIdHeader = "";
if($sessIdResponse =~ /X-Transmission-Session-Id: [a-zA-Z0-9\+\-\\\/]+/){
my $header = $&;
for my $magnet(@_){
$magnet = "magnet:?xt=urn:btih:$magnet" if $magnet =~ /^[0-9a-f]+$/i;
my $data = ''
. "{"
. "\"method\":\"torrent-add\","
. "\"arguments\":{"
. "\"paused\":$paused,"
. "\"filename\":\"$magnet\""
. "}"
. "}";
my @addTorrentCmd = curlCmd($header, $data);
system @addTorrentCmd;
}
}else{
die "No sess id\n";
}
}
sub curlCmd(;$$){
my ($header, $data) = @_;
$header = '' if not defined $header;
$data = '' if not defined $data;
my @cmd = ("curl", "--silent", "--anyauth", "--user", "$user:$pw", $rpcUrl);
@cmd = (@cmd, "--header", $header) if length $header > 0;
@cmd = (@cmd, "--data", $data) if length $data > 0;
return @cmd;
}
&main(@ARGV);