/
completion-path.sh
executable file
·179 lines (145 loc) · 6.02 KB
/
completion-path.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
#! /usr/bin/env bash
#
# Determine a dynamic completion path and print it on stdout for capturing
#
# Call with "-h" for installation instructions!
# List of attributes passed by the 'completion_path' method
arglist=( default session hash name directory base_path tied_to_file is_multi_file label display_name )
# Helper to get the filesystem path for any path
fs4path() {
local path="${1:?You MUST provide a path}"
df -P "$path" | tail -n1 | awk '{print $1;}'
}
# Determine target path (adapt this to your needs)
set_target_path() {
local month=$(date +'%Y-%m')
# Only move data downloaded into a "work" directory
if egrep >/dev/null "/work/" <<<"${base_path}/"; then
# Make sure the target directory is on the same drive as "work", else leave it alone
work_dir=$(sed -re 's~(^.+/work/).*~\1~' <<<"${base_path}/")
test $(fs4path "$work_dir") == $(fs4path "$(dirname ${base_path})") || return
else
return # no "work" component in data path (pre-determined path)
fi
# "target_base" is used to complete a non-empty but relative "target" path
target_base=$(sed -re 's~^(.*)/work/.*~\1/done~' <<<"${base_path}")
target_tail=$(sed -re 's~^.*/work/(.*)~\1~' <<<"${base_path}")
test "$is_multi_file" -eq 1 || target_tail="$(dirname "$target_tail")"
test "$target_tail" != '.' || target_tail=""
# Move by label
test -n "$target" || case $(tr A-Z' ' a-z_ <<<"${label:-NOT_SET}") in
tv|hdtv) target="TV" ;;
movie*) target="Movies/$month" ;;
esac
# Move by name patterns (check both displayname and info.name)
for i in "$display_name" "$name"; do
test -n "$target" -o -z "$i" || case $(tr A-Z' ' a-z. <<<"${i}") in
*hdtv*|*pdtv*) target="TV" ;;
*.s[0-9][0-9].*) target="TV" ;;
*.s[0-9][0-9]e[0-9][0-9].*) target="TV" ;;
*pdf|*epub|*ebook*) target="eBooks/$month" ;;
esac
done
test -z "$target" && is_movie "$name" && target="Movies/$month" || :
test -z "$target" -a -n "$display_name" && is_movie "$display_name" && target="Movies/$month" || :
# Prevent duplication at end of path
if test -n "$target" -a "$is_multi_file" -eq 1 -a "$name" = "$target_tail"; then
target_tail=""
fi
# Append tail path if non-empty
test -z "$target" -o -z "$target_tail" || target="$target/$target_tail"
} # set_target_path
is_movie() {
python - "$@" <<'EOF'
import re
import sys
pattern = re.compile(
r"^(?P<title>.+?)[. ](?P<year>\d{4})"
r"(?:[._ ](?P<release>UNRATED|REPACK|INTERNAL|PROPER|LIMITED|RERiP))*"
r"(?:[._ ](?P<format>480p|576p|720p|1080p|1080i|2160p))?"
r"(?:[._ ](?P<srctag>[a-z]{1,9}))?"
r"(?:[._ ](?P<source>BDRip|BRRip|HDRip|DVDRip|DVD[59]?|PAL|NTSC|Web|WebRip|WEB-DL|Blu-ray|BluRay|BD25|BD50))"
r"(?:[._ ](?P<sound1>MP3|DD.?[25]\.[01]|AC3|AAC(?:2.0)?|FLAC(?:2.0)?|DTS(?:-HD)?))?"
r"(?:[._ ](?P<codec>xvid|divx|avc|x264|h\.?264|hevc|h\.?265))"
r"(?:[._ ](?P<sound2>MP3|DD.?[25]\.[01]|AC3|AAC(?:2.0)?|FLAC(?:2.0)?|DTS(?:-HD)?))?"
r"(?:[-.](?P<group>.+?))"
r"(?P<extension>\.avi|\.mkv|\.mp4|\.m4v)?$", re.I
)
title = ' '.join(sys.argv[1:])
sys.exit(not pattern.match(title))
EOF
} # is_movie
# Print installation instructions when called with "-h"
if test "$1" = "-h" -o "$1" = "--help"; then
echo "$(basename $0) – Determine a dynamic rTorrent completion path"
echo
echo "To install, call these commands:"
cat <<EOF
# Change "~/rtorrent" to your instance's directory
# (and do the same in the "completion_path" method below)
mkdir -p ~/rtorrent/scripts
cp -np $(readlink -f $0) \$_
chmod a+x \$_/$(basename $0)
EOF
echo
echo "Also add this to your rtorrent.rc:"
cat <<'EOF'
# Completion moving
# vim: ft=dosini
method.insert = completion_path, simple|private, "execute.capture = \
~/rtorrent/scripts/completion-path.sh, \
(directory.default), (session.path), \
(d.hash), (d.name), (d.directory), (d.base_path), (d.tied_to_file), \
(d.is_multi_file), (d.custom1), (d.custom, displayname)"
method.insert = completion_dirname, simple|private, \
"execute.capture = bash, -c, \"dirname \\\"$1\\\" | tr -d $'\\\\n'\", \
completion_dirname, (argument.0)"
method.insert = completion_move_print, simple|private, \
"print = \"MOVED »\", (argument.0), \"« to »\", (argument.1), «"
method.insert = completion_move_single, simple|private, \
"d.directory.set = (argument.1); \
execute.throw = mkdir, -p, (argument.1); \
execute.throw = mv, -u, (argument.0), (argument.1)"
method.insert = completion_move_multi, simple|private, \
"d.directory_base.set = (argument.1); \
execute.throw = mkdir, -p, (completion_dirname, (argument.1)); \
execute.throw = mv, -uT, (argument.0), (argument.1)"
method.insert = completion_move, simple|private, \
"branch=d.is_multi_file=, \
\"completion_move_multi = (argument.0), (argument.1)\", \
\"completion_move_single = (argument.0), (argument.1)\" ; \
d.save_full_session="
method.insert = completion_move_verbose, simple|private, \
"completion_move = (argument.0), (argument.1); \
completion_move_print = (argument.0), (argument.1)"
method.insert = completion_move_handler, simple|private, \
"branch=\"not=(equal, argument.0=, cat=)\", \
\"completion_move_verbose = (d.base_path), (argument.0)\""
method.set_key = event.download.finished, move_on_completion, \
"completion_move_handler = (completion_path)"
# END move_on_completion
EOF
exit 1
fi
fail() {
echo ERROR: "$@"
exit 1
}
# Take arguments
for argname in "${arglist[@]}"; do
test $# -gt 0 || fail "'$argname' is missing!"
eval "$argname"'="$1"'
shift
done
#set | egrep '^[a-z_]+=' >&2
# Determine target path
target=""
set_target_path
# Return result (an empty target prevents moving)
if test -n "$target"; then
if test "${target:0:1}" = '/'; then
echo -n "$target"
else
echo -n "${target_base%/}/$target"
fi
fi