-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathariactl.pl
More file actions
executable file
·215 lines (187 loc) · 6.74 KB
/
ariactl.pl
File metadata and controls
executable file
·215 lines (187 loc) · 6.74 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/local/bin/perl5
use diagnostics;
use strict;
use warnings;
use CGI qw/:standard *table *Tr *td/;
#use CGI::Pretty qw/ :standard *table *Tr *td/;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use File::Basename;
use Number::Format qw/:subs/;
use Tie::IxHash;
use Frontier::Client;
use Data::Dumper;
sub clean_die {
print map(p($_), @_),
end_html();
exit(0);
}
# There are also log and log-level, not sure how useful these are in a Web
# interface. Another option may be to call getGlobalOption to retreive
# this list. No: getGlobalOption returns way too many options (including many
# that can't be modified from the XML-RPC interface).
our %ariaopts = ("max-concurrent-downloads" => "Max simultaneous downloads",
"max-overall-download-limit" => "Overall d/l bandwidth limit",
"max-overall-upload-limit" => "Overall u/l bandwidth limit");
our %ariadlopts = ("max-download-limit" => "D/L bandwidth limit",
"max-upload-limit" => "U/L bandwidth limit",
"bt-max-peers" => "Max nb. of peers",
"bt-request-peer-speed-limit" => "Lower peer speed limit",
"cancel" => "Cancel download");
# Small subclassing of Frontier::RPC to pass the user info down for each RPC
# call
{
package AriaRPC;
use parent 'Frontier::Client';
sub new {
my $class = shift;
my $url = new URI(shift);
my $token = $url->userinfo;
$url->userinfo(undef);
my $self = Frontier::Client->new(url => $url->as_string);
$self->{token} = $token;
bless $self, $class;
}
sub call {
my $self = shift;
my $method = shift;
my @args = @_;
$self->SUPER::call($method, "token:$self->{token}", @args);
}
}
sub show_dl {
my $dls = shift;
if (not scalar @$dls) {
print p("No downloads");
return;
}
my $odd = 1;
print start_table();
print Tr(th(["Filename", "Status", "Progress"]));
foreach my $dl (@$dls) {
print start_Tr({-class => ($odd ? "oddrow" : "evenrow")});
$odd = !$odd;
my ($basedir, $basename) = ($dl->{files}[0]{path} =~ m#^(.*)/([^/]+)$#);
if (@{$dl->{files}} > 1 && $basedir =~ m#/#) {
($basedir, $basename) = ($basedir =~ m#^(.*)/([^/]+)$#);
}
# The filename being downloaded, with the details hidden by default,
# viewable by clicking on the filename
print td(dl(dt({ -onClick => "toggle(\"".$dl->{gid}."\")" },
escapeHTML($basename)),
map({ +dd({-class => $dl->{gid}}, escapeHTML($_->{path})) }
@{$dl->{files}})));
my $progress;
if ($dl->{totalLength}) {
$progress = sprintf(
"%.2f%% (%s/%s)",
100*$dl->{completedLength} / $dl->{totalLength},
format_bytes($dl->{completedLength}),
format_bytes($dl->{totalLength}));
} else {
$progress = "n/a";
}
print td(p($dl->{status}),
start_form(),
popup_menu(-name => "dloptname", -values => [keys %ariadlopts],
-labels => \%ariadlopts),
textfield(-name => "dloptval"),
hidden(-name => "dlid",
-default => $dl->{gid}),
end_form()),
td p $progress;
print end_Tr();
}
print end_table();
}
binmode STDOUT, ':utf8';
print header(),
start_html(-title => "Aria Control",
-style => { -src => "/style.css" },
-script => { -src => "/behaviour.js" },
-encoding => "UTF-8");
my $urlfilename = dirname($ENV{"SCRIPT_FILENAME"}) . "/ariaurl.txt";
open my $urlfile, "<", $urlfilename or clean_die("Could not open the URL file at $urlfilename: $!.");
my $ariaurl = new URI(<$urlfile>);
close $urlfile;
my $ariactl = new AriaRPC($ariaurl);
my $v;
eval {
$v = $ariactl->call("aria2.getVersion");
};
if ($@) {
clean_die("aria2 doesn't seem to be available...", $@),
}
print h1("aria2 Control"),
start_form(),
fieldset(legend("Add downloads"),
"URL to add to the queue of aria2: ",
textfield(-name => "url", -default => "", -override => 1), br(),
"Output directory: ",
textfield(-name => "dir", -value => "/storage"), br(),
submit()),
end_form();
print
start_form(),
fieldset(legend("aria2 options"),
"Option: ",
popup_menu(-name => "optname", -values => [keys %ariaopts],
-labels => \%ariaopts), br(),
"Value: ",
textfield(-name => "optval"), br(),
submit()),
end_form();
if (my $url = param("url") and
my $dir = param("dir")) {
if ($dir !~ m#^/[a-zA-Z0-9/ -_]+$#) {
die(p(escapeHTML($dir), " is not a valid path."));
}
print p("Adding ", escapeHTML($url), " to the download list,",
"ouputting to $dir...");
my $gid;
$gid = $ariactl->call("aria2.addUri", [$url], {dir => $dir});
print p("Added to the queue as $gid");
}
if (my $optname = param("optname") and
my $optval = param("optval")) {
print p("Setting ", escapeHTML($optname), " to ",
escapeHTML($optval), "...");
my $resp = $ariactl->call("aria2.changeGlobalOption",
{$optname => $optval});
print p("aria2 said $resp.");
}
if (my $dlid = param("dlid") and
my $optname = param("dloptname") and
my $optval = param("dloptval")) {
my $resp;
if ($optname eq "cancel") {
print p("Putting download ".escapeHTML($dlid)." back to the waiting
queue...");
$resp = $ariactl->call("aria2.pause", $ariactl->string($dlid));
$resp .= " and ".$ariactl->call("aria2.unpause", $ariactl->string($dlid));
} else {
print p("Setting ", escapeHTML($optname), " to ", escapeHTML($optval),
" for download ", escapeHTML($dlid), "...");
$resp = $ariactl->call("aria2.changeOption",
$ariactl->string($dlid), {$optname => $optval});
}
print p("aria2 said $resp.");
}
my @tellargs = qw/files gid totalLength completedLength status/;
tie my %methods => 'Tie::IxHash',
"Current Downloads" => ["aria2.tellActive", \@tellargs],
"Finished Downloads" => ["aria2.tellStopped", 0, 50, \@tellargs],
"Waiting Downloads" => ["aria2.tellWaiting", 0, 50, \@tellargs],
;
foreach my $title (keys %methods) {
my $dls = $ariactl->call(@{$methods{$title}});
print h1($title);
show_dl $dls;
}
print hr(),
p(sprintf "Version: %s, features: %s\n",
$v->{version},
join(", ", @{$v->{enabledFeatures}}));
my $options = $ariactl->call("aria2.getGlobalOption");
print p("Options: "),
table(map { Tr(td[$_, $options->{$_}]) if !/passwd/; } keys %$options);
print end_html();