2023-07-19 13:22:02 +02:00
|
|
|
#!/usr/bin/with-contenv bash
|
2023-07-20 01:04:25 +02:00
|
|
|
ScriptVersion="1.5"
|
2023-07-19 13:22:02 +02:00
|
|
|
scriptName="Audiobook"
|
|
|
|
|
|
|
|
#### Import Settings
|
|
|
|
source /config/extended.conf
|
|
|
|
|
|
|
|
log () {
|
|
|
|
m_time=`date "+%F %T"`
|
2023-07-19 14:19:56 +02:00
|
|
|
echo $m_time" :: $scriptName :: $ScriptVersion :: "$1
|
2023-07-19 13:22:02 +02:00
|
|
|
}
|
|
|
|
|
2023-07-19 23:45:41 +02:00
|
|
|
if [ -z $allowM4b ]; then
|
|
|
|
allowM4b=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z $allowMp3 ]; then
|
|
|
|
allowMp3=false
|
|
|
|
fi
|
2023-07-19 13:22:02 +02:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
touch "/config/scripts/audiobook.txt"
|
|
|
|
exec &> >(tee -a "/config/scripts/audiobook.txt")
|
|
|
|
|
2023-07-19 14:19:56 +02:00
|
|
|
|
|
|
|
SECONDS=0
|
2023-07-19 17:59:14 +02:00
|
|
|
log "Processing $1"
|
2023-07-19 16:22:48 +02:00
|
|
|
log "Searching for audiobook (m4b) files in completed download..."
|
2023-07-19 17:36:25 +02:00
|
|
|
m4bCount=$(find "$1" -type f -iname "*.m4b" | wc -l)
|
|
|
|
if [ $m4bCount -gt 0 ]; then
|
|
|
|
log "$m4bCount M4B files found, removing non m4b files..."
|
2023-07-19 14:19:56 +02:00
|
|
|
find "$1" -type f -not -iname "*.m4b" -delete
|
|
|
|
find "$1" -mindepth 2 -type f -exec mv "{}" "$1"/ \;
|
|
|
|
find "$1" -mindepth 1 -type d -delete
|
2023-07-19 16:22:48 +02:00
|
|
|
else
|
|
|
|
log "None found..."
|
2023-07-19 14:19:56 +02:00
|
|
|
fi
|
|
|
|
|
2023-07-19 16:22:48 +02:00
|
|
|
log "Searching for audiobook (m4b.mp4) files in completed download..."
|
2023-07-19 17:36:25 +02:00
|
|
|
mp4Count=$(find "$1" -type f -iname "*.m4b.mp4" | wc -l)
|
|
|
|
if [ $mp4Count -gt 0 ]; then
|
|
|
|
log "$mp4Count M4B (m4b.mp4) files found, removing non m4b files..."
|
2023-07-19 14:19:56 +02:00
|
|
|
find "$1" -type f -not -iname "*.m4b.mp4" -delete
|
|
|
|
find "$1" -mindepth 2 -type f -exec mv "{}" "$1"/ \;
|
|
|
|
find "$1" -mindepth 1 -type d -delete
|
|
|
|
log "Renaming m4b.mp4 files to m4b..."
|
|
|
|
count=0
|
|
|
|
fileCount=$(find "$1" -type f -iname "*.m4b.mp4"| wc -l)
|
|
|
|
find "$1" -type f -iname "*.m4b.mp4" -print0 | while IFS= read -r -d '' file; do
|
|
|
|
count=$(($count+1))
|
|
|
|
baseFileName="${file%.*}"
|
|
|
|
fileName="$(basename "$file")"
|
|
|
|
extension="${fileName##*.}"
|
|
|
|
log "$count of $fileCount :: Processing $fileName"
|
|
|
|
if [ -f "$file" ]; then
|
|
|
|
mv "$file" "$1/${fileName%.*}"
|
2023-07-19 13:22:02 +02:00
|
|
|
fi
|
2023-07-19 14:19:56 +02:00
|
|
|
done
|
2023-07-19 16:22:48 +02:00
|
|
|
log "All files renamed"
|
|
|
|
else
|
|
|
|
log "None found..."
|
2023-07-19 14:19:56 +02:00
|
|
|
fi
|
|
|
|
|
2023-07-19 16:22:48 +02:00
|
|
|
log "Searching for audiobook (mp3) files in completed download..."
|
|
|
|
mp3Count=$(find "$1" -type f -iname "*.mp3" | wc -l)
|
|
|
|
if [ $mp3Count -gt 0 ]; then
|
2023-07-19 17:36:25 +02:00
|
|
|
log "$mp3Count MP3 files found, removing non mp3 files..."
|
2023-07-19 16:22:48 +02:00
|
|
|
find "$1" -type f -not -iname "*.mp3" -delete
|
|
|
|
find "$1" -mindepth 2 -type f -exec mv "{}" "$1"/ \;
|
|
|
|
find "$1" -mindepth 1 -type d -delete
|
2023-07-19 17:36:25 +02:00
|
|
|
if [ $mp3Count -ne 1 ]; then
|
2023-07-19 16:22:48 +02:00
|
|
|
log "ERROR: More than 1 MP3 file found, performing cleanup..."
|
|
|
|
find "$1" -type f -iname "*.mp3" -delete
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
log "None found..."
|
|
|
|
fi
|
|
|
|
|
|
|
|
error="false"
|
|
|
|
bookfound="false"
|
|
|
|
m4bCount=$(find "$1" -type f -iname "*.m4b" | wc -l)
|
2023-07-19 17:59:14 +02:00
|
|
|
mp3Count=$(find "$1" -type f -iname "*.mp3" | wc -l)
|
|
|
|
#log "$m4bCount m4bs found :: $mp3Count mp3s found"
|
2023-07-19 16:22:48 +02:00
|
|
|
if [ "$bookfound" == "false" ]; then
|
|
|
|
if [ $m4bCount -eq 0 ]; then
|
|
|
|
error="true"
|
|
|
|
else
|
|
|
|
bookfound="true"
|
2023-07-19 17:59:14 +02:00
|
|
|
error="false"
|
2023-07-19 16:22:48 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$bookfound" == "false" ]; then
|
|
|
|
if [ $mp3Count -eq 0 ]; then
|
|
|
|
error="true"
|
|
|
|
else
|
|
|
|
bookfound="true"
|
2023-07-19 17:59:14 +02:00
|
|
|
error="false"
|
2023-07-19 16:22:48 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-07-20 01:04:25 +02:00
|
|
|
if [ "$allowM4b" != "true" ]; then
|
2023-07-19 23:45:41 +02:00
|
|
|
if [ $allowM4b -gt 0 ]; then
|
|
|
|
log "M4B's disabled via config file, performing cleanup..."
|
|
|
|
rm "$1"/*
|
|
|
|
error="true"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-07-20 01:04:25 +02:00
|
|
|
if [ "$allowMp3" != "true" ]; then
|
2023-07-19 23:45:41 +02:00
|
|
|
if [ $mp3Count -gt 0 ]; then
|
|
|
|
log "MP3's disabled via config file, performing cleanup..."
|
|
|
|
rm "$1"/*
|
|
|
|
error="true"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-07-19 16:22:48 +02:00
|
|
|
if [ "$error" == "true" ]; then
|
|
|
|
echo "ERROR: No audiobook files found" && exit 1
|
2023-07-19 14:19:56 +02:00
|
|
|
fi
|
2023-07-19 13:22:02 +02:00
|
|
|
|
2023-07-19 14:19:56 +02:00
|
|
|
chmod 777 "$1"
|
|
|
|
chmod 666 "$1"/*
|
|
|
|
duration=$SECONDS
|
|
|
|
echo "Post Processing Completed in $(($duration / 60 )) minutes and $(($duration % 60 )) seconds!"
|
2023-07-19 13:22:02 +02:00
|
|
|
exit
|