-
Notifications
You must be signed in to change notification settings - Fork 49
/
qb_remove-completed-downloads.sh
executable file
·81 lines (57 loc) · 3.57 KB
/
qb_remove-completed-downloads.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
#!/bin/bash
#
# qBitTorrent script to automatically remove any completed downloads from matching category (tv|movies) after X hours.
# (The idea behind this is to allow Radarr/Sonar to copy & rename files, then this script will come and remove them after X hours so they still have time to seed.)
#
# Author: DN
# https://github.com/ultimate-pms/ultimate-plex-setup
#
#########################################################################################################
QBITTORRENT_HOSTNAME="localhost:8080"
QBITTORRENT_USERNAME=""
QBITTORRENT_PASSWORD=""
MOVIE_CATAGORY="movies"
TV_CATAGORY="tv"
REMOVE_AFTER_X_HOURS=720 # Suggest at least 24 hours, so that you maintain a reasonable seed ratio... Defaults to 1 month.
DELETE_FILES_FROM_QBTORRENT="true" # Set to true if you want it to delete any remaining files in the qbittorrent downloads directory...
# qBitTorrent 4.5.x and newer now needs to use a cookie to access the API...
COOKIE_TOKEN=`curl \
--header "Referer: http://$QBITTORRENT_HOSTNAME" \
--data "username=$QBITTORRENT_USERNAME&password=$QBITTORRENT_PASSWORD" \
-S -c - "$QBITTORRENT_HOSTNAME/api/v2/auth/login" | grep HttpOnly | awk '{print $6 "=" $7}'`
#########################################################################################################
#
## MOVIE TORRENTS
# ------------------------------------------------------------------------------------------------------------
COMPLETED_MOVIES=`curl --cookie "$COOKIE_TOKEN" -s "http://$QBITTORRENT_HOSTNAME/api/v2/torrents/info?filter=completed&category=$MOVIE_CATAGORY"`
# Radarr client copies the movies, but does not remove the torrent file or the movie file, instead it creates a hardlink so the file can continue to seed...
# Find any torrents older than X hours (after they have been copied over to the NAS volumes), go ahead and remove the torrents and files (in this case symlinks, your copied files will not be removed)...
echo $COMPLETED_MOVIES | jq -r '.[] | [.save_path + .name, .hash, .completion_on|tostring] | @tsv' |
while IFS=$'\t' read -r item hash completion_on; do
COMPLETED_DATE=`date -d @$completion_on "+%Y-%m-%d %H:%M:%S"`
CURRENT_DATE=`date "+%Y-%m-%d %H:%M:%S"`
HOURS_OLD=`echo $(( ( $(date -ud "$CURRENT_DATE" +'%s') - $(date -ud "$COMPLETED_DATE" +'%s') )/60/60 ))`
if (( $HOURS_OLD > $REMOVE_AFTER_X_HOURS )); then
echo "Removing: [$item]"
# Remove from torrent client...
curl --cookie "$COOKIE_TOKEN" -s "http://$QBITTORRENT_HOSTNAME/api/v2/torrents/delete?hashes=$hash&deleteFiles=$DELETE_FILES_FROM_QBTORRENT"
# Remove seed files...
rm -rf $item
fi
done
## TV TORRENTS
# ------------------------------------------------------------------------------------------------------------
COMPLETED_TV_SERIES=`curl --cookie "$COOKIE_TOKEN" -s "http://$QBITTORRENT_HOSTNAME/api/v2/torrents/info?filter=completed&category=$TV_CATAGORY"`
echo $COMPLETED_TV_SERIES | jq -r '.[] | [.save_path + .name, .hash, .completion_on|tostring] | @tsv' |
while IFS=$'\t' read -r item hash completion_on; do
COMPLETED_DATE=`date -d @$completion_on "+%Y-%m-%d %H:%M:%S"`
CURRENT_DATE=`date "+%Y-%m-%d %H:%M:%S"`
HOURS_OLD=`echo $(( ( $(date -ud "$CURRENT_DATE" +'%s') - $(date -ud "$COMPLETED_DATE" +'%s') )/60/60 ))`
if (( $HOURS_OLD > $REMOVE_AFTER_X_HOURS )); then
echo "Removing: [$item]"
# Remove from torrent client...
curl --cookie "$COOKIE_TOKEN" -s "http://$QBITTORRENT_HOSTNAME/api/v2/torrents/delete?hashes=$hash&deleteFiles=$DELETE_FILES_FROM_QBTORRENT"
# Remove seed files...
rm -rf $item
fi
done