Prune Ended TVShows – A Script To Move Ended TVShows

Prune Ended Shows
A Script To Move Ended TVShows

I have written a script in bash that will move all TVShows with the status “Ended” to a separate directory. I find this useful because I have over 700 TVShows in my library, and I have noticed that this amount of files increases scan time when performing library updates.

To use this script you will need to first export your library into separate files, this will create the necessary nfo files that this script needs. Please note that this script requires you to use TheTVDB as a scraper!

You will also need to create a secondary directory to store TVShows that are no longer airing. I chose to call this folder “TVShows (Old)”.

Next, use the script to move all your ended TVShows to your secondary directory. Make sure that you modify the script to work with your paths! I also suggest using your own API key for TheTVDB! Note that you can also easily customize the path of the logfile.

Code:
#!/bin/bash

# System readable timestamp
Date=`date +%Y%m%d%H%M%S`

# Human readable timestamp
Time=`date +%Y/%m/%d\ %H:%M:%S`

# Path to log file
# If file does not exist, it will be created
MyLog=~/PruneEndedShows"${Date}".log

# Path to TVShows
MyTVShowPath=~/TVShows/

# Path to Ended TVShows
MyEndedTVShowPath=~/TVShows\ \(Old\)/

# TheTVDB.com API key
MyAPIKey=1D62F2F90030C444

#----------------------------

touch "${MyLog}"
echo "${Time} - Started Processing Directory: ${MyTVShowPath}" >> "${MyLog}"

# Get list of TVShows
MyTVShowRegex="$(echo "${MyTVShowPath}" | sed -e 's/\//\\\//g')"
MyTVShowList="$(ls --format=single-column -d ${MyTVShowPath}*/ | sed -e 's/'"${MyTVShowRegex}"'//g')"

echo "The Following TVShows will be processed:" >> "${MyLog}"
echo "${MyTVShowList}" >> "${MyLog}"

IFS=$'\n' read -rd '' -a MyTVShowArray <<< "$MyTVShowList"

for ThisTVShowPath in "${MyTVShowArray[@]}"; do

    # Create temp file
    touch "${MyTVShowPath}${ThisTVShowPath}tmp.nfo"

    # Get TVShow ID from tvshow.nfo
    echo "Processing ${ThisTVShowPath}" >> "${MyLog}"
    ThisTVShowID="$(grep -hi "\<id\>" "${MyTVShowPath}${ThisTVShowPath}tvshow.nfo" | sed -e 's/\(\s\)\{0,\}<\/\?id>//g')"

    #Make URL
    ThisTVShowURL="http://www.thetvdb.com/api/${MyAPIKey}/series/${ThisTVShowID}/"

    # Download TVShow URL to temp file
    echo "Downloading MetaData from ${ThisTVShowURL}" >> "${MyLog}"
    wget -q "${ThisTVShowURL}" -O "${MyTVShowPath}${ThisTVShowPath}tmp.nfo"

    # Parse Temp NFO for status
    ThisTVShowStatus="$(grep -hi "\<status\>" "${MyTVShowPath}${ThisTVShowPath}tmp.nfo" | sed -e 's/\(\s\)\{0,\}<\/\?Status>//g')"
    echo "Status of ${ThisTVShowPath}: ${ThisTVShowStatus}" >> "${MyLog}"

    if [ "${ThisTVShowStatus}" == "Ended" ]; then

        echo "Moving files..." >> "${MyLog}"
        mv -fv "${MyTVShowPath}${ThisTVShowPath}" "${MyEndedTVShowPath}" >> "${MyLog}"

    fi

done

# Clean up temp files
echo "Cleaning up temp files" >> "${MyLog}"
rm -fv "${MyEndedTVShowPath}"*/tmp.nf >> "${MyLog}"
rm -fv "${MyTVShowPath}"*/tmp.nfo >> "${MyLog}"

# Finished
echo "Processing Complete" >> "${MyLog}"

Finally, you will need to add the secondary directory (ie. “TVShows (Old)”) back into Kodi. Make sure you add it as its own source, and also exclude the path from library updates

Now only TVShows scheduled to continue will be included in your library updates! Big Grin