forked from chupovsky/Transmission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrentclear
142 lines (131 loc) · 4.9 KB
/
torrentclear
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
#!/bin/bash
# Torrent Clear Script v1.0.1
# Script for Cron
# Clear torrents in Transmission-daemon, ratio >= 2
# And who have reached the limit of distribution activity: By default 7 days
# Default startup schedule: Every hour
#
# Tested on:
# Debian GNU/Linux 9.4 (stretch)
# transmission-daemon 2.92 (14714)
#
# Cron setup:
# cd /etc/cron.hourly/
# put a torrentclear file in this folder and give execution rights
# chmod +x torrentclear
#
# Create logfile dir:
# mkdir /var/log/transmission
#
# INIT
SETTINGFILE="/config/settings.json"
LOGFILE="/config/log/torrentclear.log"
TRANSIP="127.0.0.1"
TRANSPORT="9091"
TR_LOGIN="user"
TR_PASSWORD="123456789"
TR_CONNECT="transmission-remote $TRANSIP:$TRANSPORT -n $TR_LOGIN:$TR_PASSWORD"
NOWDATE=$(date +%s) # Current date in seconds | It is advisable that NTP synchronization be configured on the OS
LIMITTIME="604800" # After how many days (7 days = 604800 seconds) should the torrent be deleted, even if it has not reached the distribution coefficient = 2
regex_file="\.(mkv|avi|mp4|ts|mov)$"
# FUNCTIONS
function logging
{
# $1 - Text
if [ -e $LOGFILE ]; then
echo "`date '+%d.%m.%Y %H:%M:%S'` $1" >> $LOGFILE
else
touch $LOGFILE
echo "`date '+%d.%m.%Y %H:%M:%S'` $1" >> $LOGFILE
fi
}
function torrentinfo
{
# $1 - TORRENTID
logging "ID: $1"
local TR_NAME=$($TR_CONNECT -t$1 -i | grep 'Name:' | grep -Eo '\:\s(.*+)' | sed -r 's/^\:\s//')
logging "TR_NAME: $TR_NAME"
local TR_DATEDONE=$($TR_CONNECT -t$1 -i | grep 'Date finished:' | grep -Eo '\:\s{4}.*+' | sed -r 's/^\:\s{4}//')
local TR_DATEDONE=$(date -d "$TR_DATEDONE" +%s)
logging "TR_DATEDONE: $TR_DATEDONE"
local PERCENT=$($TR_CONNECT -t$1 -i | grep 'Percent Done:' | sed -r 's/(\s|[a-zA-Z]|\:)//g' | sed -r 's/\%//g')
logging "PERCENT: $PERCENT%"
local RATIO=$($TR_CONNECT -t$1 -i | grep 'Ratio:' | sed -r 's/(\s|[a-zA-Z]|\:)//g')
logging "RATIO: $RATIO"
local STATE=$($TR_CONNECT -t$1 -i | grep 'State:' | sed -r 's/\s(.*)\:\s//g')
logging "STATE: $STATE"
local DATEDIFF=$(($NOWDATE - $TR_DATEDONE))
logging "DATEDIFF: $DATEDIFF"
}
function torrentremove
{
# $1 - TORRENTID
# $2 - (1 - RATIO | 2 - DATETIME)
# Stop and remove torrent from Transmission
local TR_NAME=$($TR_CONNECT -t$1 -i | grep 'Name:' | grep -Eo '\:\s(.*+)' | sed -r 's/^\:\s//')
$TR_CONNECT -t$1 -S
logging "Remove torrent and delete"
local REMOVE=$($TR_CONNECT -t$1 --remove-and-delete | grep -Eo '\"([a-z]+)\"' | sed -r 's/\"//g')
fi
# Delete check
if [[ $2 == 1 ]]; then
# RATIO
if [[ $REMOVE != "success" ]]; then
# Delete is failed
logging "ERR: Failed to stop and remove torrent $TR_NAME by ratio limit"
else
# Delete is successful
logging "Stopping and deleting a torrent $TR_NAME by ratio limit completed successfully"
fi
fi
if [[ $2 == 2 ]]; then
# DATETIME
if [[ $REMOVE != "success" ]]; then
# Delete is failed
logging "ERR: Failed to stop and remove torrent $TR_NAME by datetime limit"
else
# Delete is successful
logging "Stopping and deleting a torrent $TR_NAME by datetime limit completed successfully"
fi
fi
}
# If the service is running, we continue.
# Select all torrents with 100% completion and ratio = 2
# Get all torrent IDs.
# We process them in a cycle
for TORRENTID in $($TR_CONNECT -l | grep -Eo '^ *([0-9]+)' | sed -r 's/[^0-9]//g'); do
# GET TORRENT INFO
# We get the name of the processed torrent for logs
TR_NAME=$($TR_CONNECT -t$TORRENTID -i | grep 'Name:' | grep -Eo '\:\s(.*+)' | sed -r 's/^\:\s//')
# Torrent download end date in format: Wed Apr 18 13:00:35 2018
TR_DATEDONE=$($TR_CONNECT -t$TORRENTID -i | grep 'Date finished:' | grep -Eo '\:\s{4}.*+' | sed -r 's/^\:\s{4}//')
TR_DATEDONE=$(date -d "$TR_DATEDONE" +%s)
# We get the percentage of download torrent (36.6 | 100 | etc)
PERCENT=$($TR_CONNECT -t$TORRENTID -i | grep 'Percent Done:' | sed -r 's/(\s|[a-zA-Z]|\:)//g' | sed -r 's/\%//g')
# We get the torrent distribution coefficient (0.3 | 1.0 | etc)
RATIO=$($TR_CONNECT -t$TORRENTID -i | grep 'Ratio:' | sed -r 's/(\s|[a-zA-Z]|\:)//g')
STATE=$($TR_CONNECT -t$TORRENTID -i | grep 'State:' | sed -r 's/\s(.*)\:\s//g')
DATEDIFF=$(($NOWDATE - $TR_DATEDONE))
# Verification of conditions
# Torrent complete?
logging "#=====================================================================================#"
logging "Check torrent $TR_NAME:: PERCENT: $PERCENT; RATIO: $RATIO; STATE: $STATE; DATEDIFF: $DATEDIFF"
if python3 -c "exit(0 if $PERCENT == 100 else 1)"; then
# Check ratio-limit-enabled
if [[ $RATIOENABLE == 1 ]]; then
LIMRATIO=$(cat $SETTINGFILE | grep '\"ratio-limit\":' | grep -Eo '([0-9\.])')
# Is the ratio greater than or equal to 2?
if python -c "exit(0 if $RATIO >= $LIMRATIO else 1)"; then
torrentinfo $TORRENTID
torrentremove $TORRENTID 1
fi
fi
# Date difference greater than or equal to LIMITTIME
if [ $DATEDIFF -ge $LIMITTIME ]; then
torrentinfo $TORRENTID
torrentremove $TORRENTID 2
fi
fi
done
logging "//END"
exit 0;