handleMedia.sh: update

I made some changes to make my handleMedia.sh script more generic, removing some duplicate code, etc. Here is the final result. I’m still trying to think of a good way to pare it down even more, I don’t like how ugly all the matching is to figure out if the files are located in the Downloads folder or one or two folders deep.

#!/bin/bash

#/usr/local/bin/handleMedia.sh
#
# Jay Holler 09-24-2011
# Find files in the Downloads directory and move them to
# the appropriate folder on the NFS share or put them in the Dropbox directory for syncing to iTunes.

# Set up some reusable variables
notify=$(/usr/local/bin/prowl.pl -apikey=”/dev/null” -application=”`basename $0`” -event=”Done” -notification=”`cat /tmp/emailmessage.txt`”)
umask 0022

#Function for matching TV shows, assumes SxxExx format and “.” separators
function MOVE_TV_SHOWS() {

storageDir=/home/jholler/TV
cd /home/jholler/Downloads

for myFile in *.$ext; do

shopt -s nocasematch
if [[ “$myFile” =~ ([A-Za-z0-9\.]*)\.(S..E..).*.$ext ]] ; then
File=${BASH_REMATCH[2]}.$ext
PreShowName=${BASH_REMATCH[1]}
ShowName=$(echo $PreShowName | sed ‘s/\./ /g’)
echo “[*] A new episode of $ShowName is ready for your enjoyment: $File” > /tmp/emailmessage.txt && sleep 2
[ -d “$storageDir/$ShowName” ] || mkdir -v “$storageDir/$ShowName”
mv -v $myFile “$storageDir/$ShowName/$ShowName.$File”
$notify
else
echo “[+] $myFile does not appear to be a TV show, pushing it to /home/jholler/Movies on lunchbox” > /tmp/emailmessage.txt
mv -v “$myFile” “/home/jholler/Movies/$myFile”
$notify
fi
done

}

#Function for matching mp3s, typically stored in a folder for the album.
function MOVE_MP3() {

storageDir=/home/jholler/Music/new
cd /home/jholler/Downloads

myFile=$(ls */*.mp3 | head -1)

shopt -s nocasematch
if [[ “$myFile” =~ ([A-Za-z0-9].*)\/([A-Za-z0-9].*).mp3 ]] ; then
AlbumName=${BASH_REMATCH[1]}
[ -d “$storageDir/$AlbumName” ] || mkdir -v “$storageDir/$AlbumName”
mv -v “$AlbumName” “$storageDir/”
echo “[*] The album $AlbumName is now ready for your enjoyment” > /tmp/emailmessage.txt && sleep 2
$notify
else
echo “[+] $myFile does not appear to consist of mp3 files, please investigate” > /tmp/emailmessage.txt
$notify
fi

shopt -u nocasematch

}

###############################################
## Start the avi matching portion of the script
## If for some reason the file is located in a folder or several folders deep, find it and put it in the Downloads directory for further manipulation.
###############################################
cd /home/jholler/Downloads
ls *.avi 2> /dev/null | wc -l && ext=avi; MOVE_TV_SHOWS
ls */*.avi 2> /dev/null | wc -l && mv */*.avi /home/jholler/Downloads; ext=avi; MOVE_TV_SHOWS
ls Movies/*/*.avi 2> /dev/null | wc -l && mv Movies/*/*.avi /home/jholler/Downloads; ext=avi; MOVE_TV_SHOWS
ls TV/*.avi 2> /dev/null | wc -l && mv TV/*.avi /home/jholler/Downloads; ext=avi; MOVE_TV_SHOWS
ls TV/*/*.avi 2> /dev/null | wc -l && mv TV/*/*.avi /home/jholler/Downloads; ext=avi; MOVE_TV_SHOWS
#########################################
## End avi matching portion of the script
#########################################
###############################################
## Start the mkv matching portion of the script
## If for some reason the file is located in a folder or several folders deep, find it and put it in the Downloads directory for further manipulation.
###############################################
ls *.mkv i 2> /dev/null | wc -l && ext=mkv; MOVE_TV_SHOWS
ls */*.mkv 2> /dev/null | wc -l && mv */*.mkv /home/jholler/Downloads; ext=mkv; MOVE_TV_SHOWS
ls */*/*.mkv 2> /dev/null | wc -l && mv */*/*.mkv /home/jholler/Downloads; ext=mkv; MOVE_TV_SHOWS
ls Movies/*/*.mkv 2> /dev/null | wc -l && mv Movies/*/*.mkv /home/jholler/Downloads; ext=mkv; MOVE_TV_SHOWS
ls TV/*.mkv 2> /dev/null | wc -l && mv TV/*.mkv /home/jholler/Downloads; ext=mkv; MOVE_TV_SHOWS
ls TV/*/*.mkv 2> /dev/null | wc -l && mv TV/*/*.mkv /home/jholler/Downloads; ext=mkv; MOVE_TV_SHOWS
#########################################
## End mkv matching portion of the script
#########################################
###########################################
## Begin mp3 matching portion of the script
###########################################
MOVE_MP3
#########################################
## End mp3 matching portion of the script
#########################################
exit 0

Advertisements

Author: jayholler

A technology lover living in California with my wife and two children.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s