-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull-from-seedbox
executable file
·94 lines (85 loc) · 2.87 KB
/
pull-from-seedbox
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
#!/bin/bash
# shellcheck disable=SC2088,SC2029
#
# This script can be added to crontab (see cron-wrapper) to automatically pull
# completed downloads off a seedbox as soon as they're finished. Unlike various
# sync solutions, it's a simple one-way move, so you can have it drop files in
# your regular Downloads instead of needing a dedicated sync folder.
#
# In ruTorrent, make sure AutoMove is enabled and set to "Hard link" and the
# REMOTE_DIR below. Torrents will continue seeding until removed either manually
# or by a ratio group (e.g. 200%/0/0/720h/Remove data). AutoWatch can be set to
# the same path (you could add a second rclone to send torrent files up to the
# box; I use WinSCP's right click > Send To > Seedbox instead). Note: you may
# wish to reduce the watch interval in rutorrent/plugins/autotools/conf.php.
#
# Install rclone (https://rclone.org/downloads/) and run `rclone config` to set
# the remote's login credentials.
#
set -e
REMOTE_NAME='seedbox'
REMOTE_DIR='downloads/sync'
LOCAL_DIR='/mnt/c/Users/max/Downloads'
run_rclone() {
# Run rclone, but hold off on logging anything unless it failed
if ! output=$(
rclone move \
--no-traverse \
--log-format '' \
--log-level DEBUG \
--ignore-existing \
--exclude '*.torrent' \
--delete-empty-src-dirs \
--low-level-retries 3 \
"$REMOTE_NAME:$REMOTE_DIR" "$LOCAL_DIR" 2>&1
); then
echo "$output"
return 1
fi
}
# Exponential backoff in case of intermittent connection errors
backoff=2
backoff_factor=2
attempts_left=4
while (( attempts_left-- > 0 )); do
if run_rclone; then
break
elif (( attempts_left == 0 )) || [[ $output == *unable\ to\ authenticate* ]]; then
# cron-wrapper will disable the cronjob and trigger a notification
exit 1
fi
printf 'rclone failed, retrying in %s min (%s attempts left)\n' "$backoff" "$attempts_left"
sleep "${backoff}m"
(( backoff *= backoff_factor ))
done
# Exit silently if nothing was copied to avoid filling up logs
if grep -q 'There was nothing to transfer' <<<"$output"; then
exit
fi
echo "$output"
# Grep out the names of the top-level items that were copied
list=$(grep -oP '(?<=INFO : )[^/]*(?=.*: (Multi-thread )?Copied)' <<<"$output" | sort -u)
readarray -t items <<<"$list"
count=${#items[@]}
if (( count == 0 )); then
exit
fi
# Show a notification
# Note: --appId is specific to wsl-notify-send
title="$count item"
if (( count > 1 )); then
title+='s'
fi
title+=' transferred'
description=$(basename -a "${items[@]}")
box() {
local width=$1
printf "┌%$((width - 2))s┐\n" | sed 's/ /─/g'
fold -w $((width - 4)) | xargs -d $'\n' printf "│ %-$((width - 4))s │\n"
printf "└%$((width - 2))s┘\n" | sed 's/ /─/g'
}
printf '%s\n\n%s\n' "$title" "$description" | box 75 | sed 's/^/ /'
notify-send \
--appId "$(basename "${BASH_SOURCE[0]}" | title-case)" \
--category "$title" \
"$description"