-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqbittorrent-postprocess
executable file
·358 lines (334 loc) · 13.3 KB
/
qbittorrent-postprocess
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
#!/bin/bash
version="0.1.0"
#
# This is an optional arguments-only example of Argbash potential
#
# ARG_OPTIONAL_SINGLE([qb_name],[N],[torrent name])
# ARG_OPTIONAL_SINGLE([qb_category],[L],[torrent category])
# ARG_OPTIONAL_SINGLE([qb_tags],[G],[separated by comma])
# ARG_OPTIONAL_SINGLE([qb_content],[F],[content path, same as root for multifile torrent])
# ARG_OPTIONAL_SINGLE([qb_root],[R],[root path, first torrent subdirectory path])
# ARG_OPTIONAL_SINGLE([qb_save],[D],[save path])
# ARG_OPTIONAL_SINGLE([qb_num],[C],[number of files])
# ARG_OPTIONAL_SINGLE([qb_size],[Z],[torrent size (bytes)])
# ARG_OPTIONAL_SINGLE([qb_tracker],[T],[current tracker])
# ARG_OPTIONAL_SINGLE([qb_hash],[I],[torrent info hash])
# ARG_USE_ENV([SCRIPTS],[/scripts],[the default mount path for scripts])
# ARG_USE_PROG([FILEBOT], [/usr/bin/filebot], [filebot executable])
# ARG_HELP([qbittorrent filebot postprocess script])
# ARG_VERSION([echo "$(basename "$0")" v$version])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
# When called, the process ends.
# Args:
# $1: The exit message (print to stderr)
# $2: The exit code (default is 1)
# if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to )
# Example:
# test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
# Function that evaluates whether a value passed to it begins by a character
# that is a short option of an argument the script knows about.
# This is required in order to support getopts-like short options grouping.
begins_with_short_option()
{
local first_option all_short_options='NLGFRDCZTIhv'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_qb_name=
_arg_qb_category=
_arg_qb_tags=
_arg_qb_content=
_arg_qb_root=
_arg_qb_save=
_arg_qb_num=
_arg_qb_size=
_arg_qb_tracker=
_arg_qb_hash=
# Function that prints general usage of the script.
# This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
# and it makes sense to remind the user how the script is supposed to be called.
print_help ()
{
printf '%s\n' "qbittorrent filebot postprocess script"
printf 'Usage: %s [-N|--qb_name <arg>] [-L|--qb_category <arg>] [-G|--qb_tags <arg>] [-F|--qb_content <arg>] [-R|--qb_root <arg>] [-D|--qb_save <arg>] [-C|--qb_num <arg>] [-Z|--qb_size <arg>] [-T|--qb_tracker <arg>] [-I|--qb_hash <arg>] [-h|--help] [-v|--version]\n' "$0"
printf '\t%s\n' "-N,--qb_name: torrent name (no default)"
printf '\t%s\n' "-L,--qb_category: torrent category (no default)"
printf '\t%s\n' "-G,--qb_tags: separated by comma (no default)"
printf '\t%s\n' "-F,--qb_content: content path, same as root for multifile torrent (no default)"
printf '\t%s\n' "-R,--qb_root: root path, first torrent subdirectory path (no default)"
printf '\t%s\n' "-D,--qb_save: save path (no default)"
printf '\t%s\n' "-C,--qb_num: number of files (no default)"
printf '\t%s\n' "-Z,--qb_size: torrent size (bytes) (no default)"
printf '\t%s\n' "-T,--qb_tracker: current tracker (no default)"
printf '\t%s\n' "-I,--qb_hash: torrent info hash (no default)"
printf '\t%s\n' "-h, --help: Prints help"
printf '\t%s\n' "-v, --version: Prints version"
printf '\nEnvironment variables that are supported:\n'
printf '\t%s\n' "SCRIPTS: the default mount path for scripts. (default: '/scripts')"
}
# The parsing of the command-line
parse_commandline ()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
# We support whitespace as a delimiter between option argument and its value.
# Therefore, we expect the --qb_name or -N value.
# so we watch for --qb_name and -N.
# Since we know that we got the long or short option,
# we just reach out for the next argument to get the value.
-N|--qb_name)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_name="$2"
shift
;;
# We support the = as a delimiter between option argument and its value.
# Therefore, we expect --qb_name=value, so we watch for --qb_name=*
# For whatever we get, we strip '--qb_name=' using the ${var##--qb_name=} notation
# to get the argument value
--qb_name=*)
_arg_qb_name="${_key##--qb_name=}"
;;
# We support getopts-style short arguments grouping,
# so as -N accepts value, we allow it to be appended to it, so we watch for -N*
# and we strip the leading -N from the argument string using the ${var##-N} notation.
-N*)
_arg_qb_name="${_key##-N}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-L|--qb_category)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_category="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_category=*)
_arg_qb_category="${_key##--qb_category=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-L*)
_arg_qb_category="${_key##-L}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-G|--qb_tags)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_tags="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_tags=*)
_arg_qb_tags="${_key##--qb_tags=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-G*)
_arg_qb_tags="${_key##-G}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-F|--qb_content)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_content="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_content=*)
_arg_qb_content="${_key##--qb_content=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-F*)
_arg_qb_content="${_key##-F}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-R|--qb_root)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_root="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_root=*)
_arg_qb_root="${_key##--qb_root=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-R*)
_arg_qb_root="${_key##-R}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-D|--qb_save)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_save="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_save=*)
_arg_qb_save="${_key##--qb_save=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-D*)
_arg_qb_save="${_key##-D}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-C|--qb_num)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_num="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_num=*)
_arg_qb_num="${_key##--qb_num=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-C*)
_arg_qb_num="${_key##-C}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-Z|--qb_size)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_size="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_size=*)
_arg_qb_size="${_key##--qb_size=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-Z*)
_arg_qb_size="${_key##-Z}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-T|--qb_tracker)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_tracker="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_tracker=*)
_arg_qb_tracker="${_key##--qb_tracker=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-T*)
_arg_qb_tracker="${_key##-T}"
;;
# See the comment of option '--qb_name' to see what's going on here - principle is the same.
-I|--qb_hash)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_qb_hash="$2"
shift
;;
# See the comment of option '--qb_name=' to see what's going on here - principle is the same.
--qb_hash=*)
_arg_qb_hash="${_key##--qb_hash=}"
;;
# See the comment of option '-N' to see what's going on here - principle is the same.
-I*)
_arg_qb_hash="${_key##-I}"
;;
# The version argurment doesn't accept a value,
# we expect the --version or -v, so we watch for them.
-v|--version)
echo "$(basename "$0")" v"$version"
exit 0
;;
# We support getopts-style short arguments clustering,
# so as -v doesn't accept value, other short options may be appended to it, so we watch for -v*.
# After stripping the leading -v from the argument, we have to make sure
# that the first character that follows coresponds to a short option.
-v*)
echo "$(basename "$0")" v"$version"
exit 0
;;
# See the comment of option '--version' to see what's going on here - principle is the same.
-h|--help)
print_help
exit 0
;;
# See the comment of option '-v' to see what's going on here - principle is the same.
-h*)
print_help
exit 0
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
# Now call all the functions defined above that are needed to get the job done
parse_commandline "$@"
# OTHER STUFF GENERATED BY Argbash
test -n "$SCRIPTS" || SCRIPTS="/scripts"
### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash
# FileBot Configuration
shopt -s nocasematch
test -n "$OUT_DIR" || OUT_DIR="."
test -n "$FILEBOT" || FILEBOT=$(which filebot) || FILEBOT="/usr/bin/filebot"
test -n "$PPR_LOG" || PPR_LOG=/config/postprocess.log
MEDIA_OUTPUT="$OUT_DIR/Media"
log() {
echo "$(date -Iseconds) [$(basename "$0")]: $*" | tee -a "$PPR_LOG"
}
log "-------------------------------------"
log "--- RUN $(date -Iseconds) ---"
log "Value of SCRIPTS: $SCRIPTS"
log "Value of OUT_DIR: $OUT_DIR"
log "Value of FILEBOT: $FILEBOT"
log "Value of --qb_name: $_arg_qb_name"
log "Value of --qb_category: $_arg_qb_category"
log "Value of --qb_tags: $_arg_qb_tags"
log "Value of --qb_content: $_arg_qb_content"
log "Value of --qb_root: $_arg_qb_root"
log "Value of --qb_save: $_arg_qb_save"
log "Value of --qb_num: $_arg_qb_num"
log "Value of --qb_size: $_arg_qb_size"
log "Value of --qb_hash: $_arg_qb_hash"
# TODO: https://www.filebot.net/forums/viewtopic.php?p=43015#p43017
# The --def ut_kind=single option instructs the amc script to use both
# ut_dir and ut_file to create a file path, and that requires both of
# these options to be set, and AFAIK is only useful for uT integration.
# [[ $_arg_qb_num -eq 1 ]] && _qb_multi="single" || _qb_multi="multi"
_qb_multi="multi"
log "Value of _qb_multi: $_qb_multi"
[[ "$_arg_qb_category" =~ (tv_shows|tv|anime|movies|movie) ]] || \
{ log 'FileBot not handling this category, skipping' ; exit 0; }
# LINKS=("$(find "$_arg_qb_root" -type l)")
# NOT reliable and returns array of length 1 even when empty
# https://stackoverflow.com/a/54561526/4078543
mapfile -d '' LINKS < <(find "$_arg_qb_root" -type l -print0)
if [[ "${#LINKS[@]}" -gt 0 ]]
then
log 'Links found in torrent directory, skipping'
exit 0
fi
# PAUSE the torrent before processing
set -x
http --pretty=format --headers --ignore-stdin ":${WEBUI_PORT}/api/v2/torrents/pause" hashes=="$_arg_qb_hash"
set -x
FB=("$FILEBOT" -script fn:amc --action keeplink --output "$MEDIA_OUTPUT" --conflict skip \
--filter \"'!readLines('\'"$SCRIPTS"'/excludes.txt'\'').contains(n)'\" \
-non-strict --log-file amc.log --def excludeList=".excludes" \
--def ut_dir="$_arg_qb_root" ut_kind="$_qb_multi" ut_title="$_arg_qb_name" ut_label="$_arg_qb_category" \
--def @"$SCRIPTS"/notify.txt \
--def minLengthMS=300000 \
--def movieDB=TheMovieDB seriesDB=TheMovieDB::TV animeDB=AniDB musicDB=ID3 \
--def movieFormat=@"$SCRIPTS"/movieFormat.groovy \
--def seriesFormat=@"$SCRIPTS"/seriesFormat.groovy \
--def animeFormat=@"$SCRIPTS"/animeFormat.groovy)
log "${FB[@]}"
# sudo -H -u devster -g devster --
"${FB[@]}"
# RESTART the torrent after processing
set -x
http --pretty=format --headers --ignore-stdin ":${WEBUI_PORT}/api/v2/torrents/resume" hashes=="$_arg_qb_hash"
set +x
# ] <-- needed because of Argbash