-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathqBittorrentHardlinksChecker.sh
executable file
·437 lines (366 loc) · 13.5 KB
/
qBittorrentHardlinksChecker.sh
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/bin/bash
########## CONFIGURATIONS ##########
# Host on which qBittorrent runs
qbt_host="http://10.0.0.100"
# Port -> the same port that is inside qBittorrent option -> Web UI -> Web User Interface
qbt_port="8081"
# Username to access to Web UI
qbt_username="admin"
# Password to access to Web UI
qbt_password="adminadmin"
# Configure here your categories, comma separated, like -> movie,tv_show
categories='Serie_Tv,Film'
# Minimum seed time before deletion, expressed in seconds, for example 864000 means 10 days
min_seeding_time=864000
# Using docker it may happen that the path is different from the real one, this allows to replace part of qBittorrent path, turning it into the real one
# In this example, if a download within qBittorren has /oldpath/film as its path, the script will interpret it as /new/path/film
# This allows volumes within qBittorrent to be mounted differently from the actual path on disk
# Leave empty if not needed
virtual_path="oldpath" # The qBittorrent path, or the part you want to change
real_path="new/path" # The new part of the path
# Check only private torrents? if not true (lowercase) will check all torrents in given categories not only the private one
only_private=true
# If true, only for private tracker, check the torrent and if is not registered will be deleted
private_torrents_check_orphan=true
# If true, only for public torrent, check the trackers and the bad one will be eliminated, not the torrent only the trackers
public_torrent_check_bad_trackers=true
# If true, if there's some torrent in error, a force recheck is actuaded, this try to start again the torrent
receck_erroring_torrent=true
########## CONFIGURATIONS ##########
jq_executable="$(command -v jq)"
curl_executable="$(command -v curl)"
if [[ -z $jq_executable ]]; then
echo -e "\n\e[0;91;1mFail on jq. Aborting.\n\e[0m"
echo "You can find it here: https://stedolan.github.io/jq/"
echo "Or you can install it with -> sudo apt install jq"
exit 1
fi
if [[ -z $curl_executable ]]; then
echo -e "\n\e[0;91;1mFail on curl. Aborting.\n\e[0m"
echo "You can install it with -> sudo apt install curl"
exit 2
fi
if [[ "${qbt_host,,}" == *"https"* ]] ;then
curl_executable="${curl_executable} --insecure"
fi
# Variable to keep track of dryrun mode
dryrun=false
if [ "$1" == "test" ]; then
dryrun=true
echo "Dryrun mode turned on."
echo ""
fi
########## FUNCTIONS ##########
url_encode() {
local string="${1}"
# Check if xxd is available
if command -v xxd >/dev/null 2>&1; then
# If xxd is available, use xxd for encoding
printf '%s' "$string" | xxd -p | sed 's/\(..\)/%\1/g' | tr -d '\n'
else
# If jq is available, use jq for encoding
jq -nr --arg s "$string" '$s|@uri'
fi
}
get_cookie () {
encoded_username=$(url_encode "$qbt_username")
encoded_password=$(url_encode "$qbt_password")
# If encoding fails, exit the function
if [ $? -ne 0 ]; then
echo "Error during URL encoding" >&2
return 1
fi
qbt_cookie=$($curl_executable --silent --fail --show-error \
--header "Referer: ${qbt_host}:${qbt_port}" \
--cookie-jar - \
--data "username=${encoded_username}&password=${encoded_password}" ${qbt_host}:${qbt_port}/api/v2/auth/login)
}
get_torrent_list () {
[[ -z "$qbt_cookie" ]] && get_cookie
torrent_list=$(echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
--cookie - \
--request GET "${qbt_host}:${qbt_port}/api/v2/torrents/info")
}
delete_torrent () {
hash="$1"
echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
-d "hashes=${hash}&deleteFiles=true" \
--cookie - \
--request POST "${qbt_host}:${qbt_port}/api/v2/torrents/delete"
echo "Deleted"
}
recheck_torrent () {
hash="$1"
echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
-d "hashes=${hash}" \
--cookie - \
--request POST "${qbt_host}:${qbt_port}/api/v2/torrents/recheck"
echo "Command executed"
}
reannounce_torrent () {
hash="$1"
echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
-d "hashes=${hash}" \
--cookie - \
--request POST "${qbt_host}:${qbt_port}/api/v2/torrents/reannounce"
}
remove_bad_tracker () {
hash="$1"
single_url="$2"
echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
-d "hash=${hash}&urls=${single_url}" \
--cookie - \
--request POST "${qbt_host}:${qbt_port}/api/v2/torrents/removeTrackers"
}
unset_array () {
array_element="$1"
unset torrent_name_array[$array_element]
unset torrent_hash_array[$array_element]
unset torrent_path_array[$array_element]
unset torrent_seeding_time_array[$array_element]
unset torrent_progress_array[$array_element]
unset private_torrent_array[$array_element]
unset torrent_trackers_array[$array_element]
unset torrent_category_array[$array_element]
}
wait() {
w=$1
echo "I'll wait ${w}s to be sure the reannunce going well..."
while [ $w -gt 0 ]; do
echo -ne "$w\033[0K\r"
sleep 1
w=$((w-1))
done
}
check_hardlinks() {
local path="$1"
local more_hard_links=false
if [ -d "$path" ]; then
# È una directory, controlla i file all'interno
while IFS= read -r -d $'\0' file; do
if [ "$(stat -c %h "$file")" -gt 1 ]; then
more_hard_links=true
break
fi
done < <(find "$path" -type f -print0)
else
# È un file
if [ "$(stat -c %h "$path")" -gt 1 ]; then
more_hard_links=true
fi
fi
echo "$more_hard_links"
}
########## FUNCTIONS ##########
get_torrent_list
if [ -z "$torrent_list" ]; then
echo "No torrents founds to check"
exit
fi
echo "Collecting data from qBittorrent, wait..."
torrent_name_array=()
torrent_hash_array=()
torrent_path_array=()
torrent_seeding_time_array=()
torrent_progress_array=()
private_torrent_array=()
torrent_trackers_array=()
torrent_category_array=()
while IFS= read -r line; do
torrent_name_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .name')
while IFS= read -r line; do
torrent_hash_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .hash')
while IFS= read -r line; do
torrent_path_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .content_path')
while IFS= read -r line; do
torrent_seeding_time_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .seeding_time')
while IFS= read -r line; do
torrent_progress_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .progress')
while IFS= read -r line; do
torrent_category_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .category')
while IFS= read -r line; do
torrent_state_array+=("$line")
done < <(echo $torrent_list | $jq_executable --raw-output '.[] | .state')
for i in "${!torrent_hash_array[@]}"; do
torrent_trackers_array[$i]=$(echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
--cookie - \
--request GET "${qbt_host}:${qbt_port}/api/v2/torrents/trackers?hash=${torrent_hash_array[$i]}")
done
for i in "${!torrent_hash_array[@]}"; do
private_torrent_array[$i]=$(echo "$qbt_cookie" | $curl_executable --silent --fail --show-error \
--cookie - \
--request GET "${qbt_host}:${qbt_port}/api/v2/torrents/properties?hash=${torrent_hash_array[$i]}" | $jq_executable --raw-output '.is_private')
done
if [ -n "$categories" ]; then
echo "Checking hardlinks:"
for j in ${categories//,/ }; do
test=$(echo "$torrent_list" | $jq_executable --raw-output --arg tosearch "$j" '.[] | select(.category == "\($tosearch)") | .name')
if [[ -z "$test" ]]; then
echo "There's no categories named ${j} or is empty"
continue
else
echo "#####################################"
echo "Checking category ${j}:"
echo "#####################################"
echo ""
for i in "${!torrent_hash_array[@]}"; do
if [[ $only_private == true ]]; then
if [[ ${torrent_category_array[$i]} == ${j} ]] && [[ ${private_torrent_array[$i]} == true ]]; then
echo "Analyzing torrent -> ${torrent_name_array[$i]}"
if awk "BEGIN {exit !(${torrent_progress_array[$i]} < 1)}rent"; then
printf "Torrent incomplete, nothing to do -> %0.3g%%\n" $(awk -v var="${torrent_progress_array[$i]}" 'BEGIN{print var * 100}')
else
if [ -z "$virtual_path" ] || [ -z "$real_path" ]; then
result="${torrent_path_array[$i]}"
else
result=$(echo "${torrent_path_array[$i]/$virtual_path/"$real_path"}")
fi
more_hard_links=$(check_hardlinks "$result")
if [ "$more_hard_links" = true ]; then
echo "More than 1 hardlinks found in $result"
else
echo "No additional hardlinks found in $result"
fi
if [[ $more_hard_links == false ]]; then
echo "Found 1 hardlinks, checking seeding time:"
if [ ${torrent_seeding_time_array[$i]} -gt $min_seeding_time ]; then
echo "I can delete this torrent, seeding time more than $min_seeding_time seconds"
if [[ $dryrun == true ]]; then
echo "Simulation (dryrun)..."
echo "reannounce torrent"
echo "wait 15 seconds..."
echo "delete torrent ${torrent_name_array[$i]}"
unset_array $i
else
reannounce_torrent ${torrent_hash_array[$i]}
wait 15
delete_torrent ${torrent_hash_array[$i]}
unset_array $i
fi
else
echo "I can't delete this torrent, seeding time not meet -> ${torrent_seeding_time_array[$i]}/${min_seeding_time}"
fi
else
echo "More than 1 hardlinks found, nothing to do"
fi
fi
echo "------------------------------"
fi
else
if [[ ${torrent_category_array[$i]} == ${j} ]]; then
echo "Analyzing torrent -> ${torrent_name_array[$i]}"
if awk "BEGIN {exit !(${torrent_progress_array[$i]} < 1)}rent"; then
printf "Torrent incomplete, nothing to do -> %0.3g%%\n" $(awk -v var="${torrent_progress_array[$i]}" 'BEGIN{print var * 100}')
else
if [ -z "$virtual_path" ] || [ -z "$real_path" ]; then
result="${torrent_path_array[$i]}"
else
result=$(echo "${torrent_path_array[$i]/$virtual_path/"$real_path"}")
fi
more_hard_links=$(check_hardlinks "$result")
if [ "$more_hard_links" = true ]; then
echo "More than 1 hardlinks found in $result"
else
echo "No additional hardlinks found in $result"
fi
if [[ $more_hard_links == false ]]; then
echo "Found 1 hardlinks, checking seeding time:"
if [ ${torrent_seeding_time_array[$i]} -gt $min_seeding_time ]; then
echo "I can delete this torrent, seeding time more than $min_seeding_time seconds"
if [[ $dryrun == true ]]; then
echo "Simulation (dryrun)..."
echo "reannounce torrent"
echo "wait 15 seconds..."
echo "delete torrent ${torrent_name_array[$i]}"
unset_array $i
else
reannounce_torrent ${torrent_hash_array[$i]}
wait 15
delete_torrent ${torrent_hash_array[$i]}
unset_array $i
fi
else
echo "I can't delete this torrent, seeding time not meet -> ${torrent_seeding_time_array[$i]}/${min_seeding_time}"
fi
else
echo "More than 1 hardlinks found, nothing to do"
fi
fi
echo "------------------------------"
fi
fi
done
fi
done
echo "Harklinks check completed"
echo "------------------------------"
else
echo "Categories list empty"
echo "------------------------------"
fi
if [[ $private_torrents_check_orphan == true ]]; then
echo "Checking for orphan torrents:"
for i in "${!torrent_hash_array[@]}"; do
orphan_torrent=$(echo ${torrent_trackers_array[$i]} | $jq_executable --raw-output '.[] | select((.status == 4) and (.num_peers < 1) and ((.msg|test("unregistered"; "i")) or (.msg|test("not registered"; "i")))) | any')
if [[ $orphan_torrent == true ]]; then
echo "Found orphan torrent -> ${torrent_name_array[$i]}, deleting"
if [[ $dryrun == true ]]; then
echo "Simulation (dryrun)..."
echo "delete torrent ${torrent_name_array[$i]}"
unset_array $i
else
delete_torrent ${torrent_hash_array[$i]}
unset_array $i
fi
echo "------------------------------"
fi
done
echo "Orphan check completed"
echo "------------------------------"
fi
if [[ $public_torrent_check_bad_trackers == true ]]; then
echo "Checking for bad trackers:"
for i in "${!torrent_hash_array[@]}"; do
if [[ ${private_torrent_array[$i]} != true ]]; then
url_list=$(echo ${torrent_trackers_array[$i]} | $jq_executable --raw-output '.[] | select((.status == 4) and (.num_peers < 1)) .url')
if [[ ! -z "$url_list" ]]; then
echo "Some problem found on -> ${torrent_name_array[$i]}"
echo "fixing..."
if [[ $dryrun == true ]]; then
echo "Simulation (dryrun)..."
echo "removing bad tracker for torrent ${torrent_name_array[$i]}"
else
remove_bad_tracker ${torrent_hash_array[$i]} $(echo $url_list | tr '\n' ' ' | tr ' ' '|' | rev | cut -c2- | rev)
fi
echo "------------------------------"
else
continue
fi
fi
done
echo "Bad trackers check completed"
echo "------------------------------"
fi
if [[ $receck_erroring_torrent == true ]]; then
echo "Checking for errored torrent:"
for i in "${!torrent_hash_array[@]}"; do
if [[ ${torrent_state_array[$i]} == "error" ]]; then
echo "Found erroring torrent -> ${torrent_name_array[$i]}, I'll recheck it"
if [[ $dryrun == true ]]; then
echo "Simulation (dryrun)..."
echo "checking torrent ${torrent_name_array[$i]}"
else
recheck_torrent ${torrent_hash_array[$i]}
fi
echo "------------------------------"
fi
done
echo "Error check completed"
echo "------------------------------"
fi