-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcleaner.pl
115 lines (103 loc) · 3.22 KB
/
cleaner.pl
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
# github.com/cxmplex
# Deluge Slow Torrent Remover
my %local_collection;
local *get_deluge_info = sub {
my $info = `deluge-console "connect 10.0.0.1:52757 user pass; info"`;
my @collection;
while ($info =~ /(?:Name:\s)(.+)\n(?:ID:\s)([a-z0-9]+)\n(?:State:\s)(.+)\n(?:Seeds:\s)(.+)\n(?:Size:\s)(.+)\n(?:Seed time:\s)(.+)\n(?:Tracker status:\s)(.+)(?:\n(?:Progress:\s)(\d+))?/ig) {
# this could be auto-gen by removing the non capture
my %deluge_obj = (
'name' => $1,
'id' => $2,
'state' => $3,
'seeds' => $4,
'size' => $5,
'seed_time' => $6,
'tracker_status' => $7,
);
my $c = $8 || '';
# ignore incomplete
next if ($deluge_obj{'state'} !~ /seeding/i && $c !~ /100/i);
# set seed_time
$deluge_obj{'seed_time'} =~ /^(\d+)\s([a-z]+)\s(?:([\d]+):(\d+):(\d+))/i;
my %date = (
'days' => $1,
'hours' => $3,
'minutes' => $4,
'seconds' => $5 ,
);
$deluge_obj{'seed_time'} = \%date;
# set Speed
$deluge_obj{'state'} =~ /(?:Up Speed:\s)([\d\.]+)\s([a-z]+)/i;
my %speed = (
'speed' => 0,
'unit' => '',
);
$speed{'speed'} = $1;
$speed{'unit'} = $2;
$deluge_obj{'speed'} = \%speed;
push @collection, \%deluge_obj;
}
return \@collection;
};
# normalize to a base unit of MiB
local *normalize = sub {
my $obj = shift;
if ($obj->{'unit'} =~ /KiB/i) {
return $obj->{'speed'} /= 1000;
}
return $obj->{'speed'};
};
local *update_local_info = sub {
my $collection = shift;
foreach(@$collection) {
$obj = $_;
next unless (length $obj->{'id'} > 0);
if (!$local_collection{$obj->{'id'}}) {
my %speeds = (
'speeds' => [normalize($obj->{'speed'})],
'seed_time' => $obj->{'seed_time'}
);
$local_collection{$obj->{'id'}} = \%speeds;
next;
}
push @{$local_collection{$obj->{'id'}}->{'speeds'}}, normalize($obj->{'speed'});
$local_collection{$obj->{'id'}}->{'seed_time'} = $obj->{'seed_time'};
}
};
local *get_average = sub {
my $objs = shift;
my $total;
my $n = 0;
foreach(@$objs) {
$total += $_;
$n++;
}
return ($total/$n, $n);
};
local *get_slow_torrents = sub {
my @slow;
foreach(keys %local_collection) {
my ($average, $n) = get_average($local_collection{$_}->{'speeds'});
my $time = $local_collection{$_}->{'seed_time'}->{'minutes'};
if ($n >= 5 && $average <= 10 && $time >= 5) {
push @slow, $_;
delete $local_collection{$_};
}
}
return \@slow;
};
while (true) {
my $collection = get_deluge_info();
if ($collection) {
update_local_info($collection);
}
my $delete_list = get_slow_torrents();
foreach(@$delete_list) {
print "Deleting $_ \n";
my $output = `deluge-console "connect 10.0.0.1:52757 user pass; pause $_"`;
sleep 5;
my $output = `deluge-console "connect 10.0.0.1:52757 user pass; rm $_ --remove_data"`;
}
sleep 20;
}