-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunrar_and_move.sh
executable file
·161 lines (141 loc) · 3.84 KB
/
unrar_and_move.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
#/bin/bash
# This script will scan the $TORRENTDIR for newly completed torrents.
# Upon completion, all non-sample media files, (mkv, avi, mp4, etc.)
# which have not already been copied, will be copied to the $VIDEODIR.
# If no media files are found, then a rar file will be located and if
# the existing media file has not already been, it will be extracted
# into the $VIDEODIR.
#
# If the torrent which is downloaded is not a media file, it will be
# ignored (for now).
#TIME=$(/bin/date +%Y%m%d_%H%M%S)
MV=/usr/bin/mv
LS=/usr/bin/ls
RM=/usr/bin/rm
FIND=/usr/bin/find
GREP=/usr/bin/grep
RSYNC="/usr/bin/rsync"
UNRAR=/usr/bin/unrar
TORRENTDIR=/home/herlo/Torrents
VIDEODIR=/home/herlo/Videos
AUDIODIR=/home/herlo/Music
EXCLUDEFILE=${TORRENTDIR}/.unrar_excludes
FORCE=0
MEDIATYPES=(avi mp4 mkv)
AUDIOTYPES=(mp3 ogg aac)
copy_video_files () {
for mt in ${MEDIATYPES[*]}; do
# echo "MT: ${mt}"
# echo "$1" | ${GREP} ${mt} #&> /dev/null
if [ $? -eq 0 ]; then
if [ ! -f "${VIDEODIR}/$1" ] || [ ${FORCE} -eq 1 ]; then
echo "COPYING VIDEO FILE: $1"
/usr/bin/rsync -a "${1}" ${VIDEODIR}
break
# else
# ${RM} -f $1
# break
fi
fi
done
}
copy_audio_files () {
for mt in ${AUDIOTYPES[*]}; do
if [ $? -eq 0 ]; then
if [ ! -f "${AUDIODIR}/$1" ] || [ ${FORCE} -eq 1 ]; then
/usr/bin/rsync -a "${1}" ${AUDIODIR}
break
# else
# ${RM} -f $1
# break
fi
fi
done
}
copy_video_dirs () {
pushd "${1}" &> /dev/null
SAVEIFS=${IFS}
IFS=$(echo -en "\n\b")
for mt in ${MEDIATYPES[*]}; do
# FILES="$(${LS} -1 *.${mt} 2> /dev/null)"
# echo "FILES: ${FILES}"
FILES="$(${FIND} . -mindepth 1 -maxdepth 1 -not -iname '*sample*' -iname "*.${mt}" 2> /dev/null)"
if [ -n "${FILES}" ]; then
SAVEIFS=${IFS}
IFS=$'\n'
for f in *.${mt}; do
EXCLUDED=$(${GREP} -i "${f}" "${EXCLUDEFILE}")
if [ -n "$EXCLUDED" ]; then
continue
fi
IFS=${SAVEIFS}
if [ ! -e "${VIDEODIR}/${f}" ] || [ ${FORCE} -eq 1 ]; then
echo /usr/bin/rsync -a "${f}" ${VIDEODIR} &> /dev/null
fi
done
IFS=${SAVEIFS}
fi
done
popd &> /dev/null
IFS=${SAVEIFS}
}
copy_audio_dirs () {
pushd "${1}" &> /dev/null
for mt in ${AUDIOTYPES[*]}; do
# FILES="$(${LS} -1 *.${mt} 2> /dev/null)"
FILES="$(${FIND} . -mindepth 1 -maxdepth 1 -not -iname '*sample*' -iname "*.${mt}" 2> /dev/null)"
if [ -n "${FILES}" ]; then
if [ ! -d "${AUDIODIR}/${1}" ]; then
mkdir -p "${AUDIODIR}/${1}"
fi
SAVEIFS=${IFS}
IFS=$'\n'
for f in *.${mt}; do
IFS=${SAVEIFS}
if [ ! -e "${AUDIODIR}/${1}/${f}" ] || [ ${FORCE} -eq 1 ]; then
/usr/bin/rsync -a "${f}" "${AUDIODIR}/${1}/" # &> /dev/null
fi
done
IFS=${SAVEIFS}
fi
done
popd &> /dev/null
}
extract_media_dirs () {
pushd "$1" &> /dev/null
for f in *.rar; do
MEDIAFILE=$(${UNRAR} lb "${f}")
EXCLUDED=$(${GREP} -i "${MEDIAFILE}" "${EXCLUDEFILE}")
if [ -n "$EXCLUDED" ]; then
continue
fi
if [ ! -e "${VIDEODIR}/${MEDIAFILE}" ] || [ ${FORCE} -eq 1 ]; then
echo "EXTRACTING: ${MEDIAFILE}"
${UNRAR} -idq -y e "${f}"
/usr/bin/rsync -a "${MEDIAFILE}" ${VIDEODIR} &> /dev/null
fi
done
popd &> /dev/null
}
pushd ${TORRENTDIR} &> /dev/null
for file in *; do
EXCLUDED=$(${GREP} -i "${file}" "${EXCLUDEFILE}")
if [ -n "$EXCLUDED" ]; then
continue
fi
#echo "${file}: ${EXCLUDED}"
if [ "${EXCLUDED}" == "0" ]; then
echo "FILE: ${1} being excluded"
continue
fi
#echo "${file}" #&> /dev/null
if [ -d "${file}" ]; then
copy_video_dirs "${file}"
copy_audio_dirs "${file}"
extract_media_dirs "${file}"
elif [ -f "${file}" ]; then
copy_video_files "${file}"
copy_audio_files "${file}"
fi
done
popd &> /dev/null