Page 1 of 1

BASH: Copy and rename thumbnail script after sabtosickbeard

Posted: January 5th, 2012, 7:32 pm
by supremicus
Hey guys,

A little bit of background, i wanted a script that would create thumbnails using tvshow thumbnails from fanart.tv (not a fan of thumbnails ripped from the video at random sections) that would be renamed to the same filename of the processed show that i have automatically copied to a seperate downloads folder so it's easier for me to keep track of new downloads on the ps3. Downside is i have to make my own cache of thumbnails, but it's working beautifully so far so can't complain. Am posting it up just incase it can be of some use to other people and can possibly get help to improve it in the future (and maybe clean up my code lol, been 15years since i've done any sort of scripting).

List of what it currently does after the duplicate script is run to put the file into the downloads folder :

Looks for thumbnail by tvdb showid in the thumbnail folder
If showid.extension exists, copys to download folder as show filename.jpg or .png & reports success to a log
If showid.extension dosn't exist copys the blank thumbnail to download folder as show filename.png & reports warning to a log

What i'd like to add in the future is :

If a thumbnail dosn't exist is to use the fanart.tv api to grab one, and have that copied to the thumbnail folder for future use. The api works via tvdb showid and can be sorted by most popular download. So maybe sort by popularity and grab the most popular one. Have alot more reading to do before i can get that far.

Possibly other things aswell but so far it's doing what i originally set out to do, and will be of use to others.

Code: Select all

#!/bin/bash
# Log File Location
LOGFILE="/mnt/tank/logs/thumbnailer.log"

# Set permissions for log file
chmod 666 "$LOGFILE"

# COPYDIR - Directory where your video is double copied to after sabToSickBeard.py is run
COPYDIR="/mnt/storage/Downloads/"

# thumbdir - Directory with your thumbnails saved as (showid).(jpg/png)
thumbdir="/mnt/tank/thumbnails/"

# Pull filename from SickBeard Variable and strip directory name
FULLPATH="$1"
FILENAME=${FULLPATH##*/}
FILENOEXT=${FILENAME%.*}

# Get Date & Time for Log File
date=$(date "+%d/%m/%y")
time=$(date "+%k:%M")

# Find out extension of thumbnail from thumbnail directory
ID="$3"
filename=$(basename $thumbdir$ID.*)
extension=${filename##*.}
filename=${filename%.*}


if [ -e ${thumbdir}${ID}.${extension} ];
then
    # thumbnail exists, so copy and report to log file.
    exists=yes
    cp "${thumbdir}${ID}.${extension}" "${COPYDIR}${FILENOEXT}.${extension}"
    # set thumbnail permissions (gets copied with none, don't know why) 
    chmod 666 "${COPYDIR}${FILENOEXT}.${extension}"
    echo -e "$date - $time" \\r >> $LOGFILE
    echo -e "Show ID: $ID - Thumbnail exists: $exists - Thumbnail created: ${FILENOEXT}.${extension}" \\r >> $LOGFILE
    echo -e Copying "${thumbdir}${ID}.${extension}" to "${COPYDIR}${FILENOEXT}.${extension}" \\r >> $LOGFILE
    echo -e " " \\r >> $LOGFILE
else
    # thumbnail does NOT exist, so we copy the blank thumbnail and report to log file.
    exists=no
    cp "${thumbdir}blank.png" "${COPYDIR}${FILENOEXT}.png"
    # set thumbnail permissions (gets copied with none, don't know why)
    chmod 666 "${COPYDIR}${FILENOEXT}.png"
    echo -e "$date - $time" \\r >> $LOGFILE
    echo -e "WARNING: Thumbnail does not exist, using blank thumbnail." \\r >> $LOGFILE
    echo -e "Show ID: $ID - Thumbnail exists: $exists - Thumbnail created: ${FILENOEXT}.png" \\r >> $LOGFILE
    echo -e Copying "${thumbdir}blank.png" to "${COPYDIR}${FILENOEXT}.png" \\r >> $LOGFILE
    echo -e " " \\r >> $LOGFILE
    exit
fi

Re: BASH: Copy and rename thumbnail script after sabtosickbe

Posted: January 7th, 2012, 5:56 pm
by supremicus
Didn't take aslong as i thought to get the thumbnai automatcially if it was missing. It's set to grab the most downloaded thumbnail, if there is none it will grab the clearart instead.
Can someone tell me why i have to define paths and change permissions ? It wasn't necessary when i was testing via ssh, but when it was transfered to sickbeard files would be copied with no permissions and it couldn't find system commands.

Code: Select all

#!/bin/bash
# wget & fetch dir
wgetdir="/usr/local/bin/"
fetchdir="/usr/bin/"
grepdir="/usr/bin/"

# Log File Location
LOGFILE="/mnt/tank/logs/thumbnailer.log"

# Set permissions for log file
chmod 666 "$LOGFILE"

# COPYDIR - Directory where your video is double copied to after sabToSickBeard.py is run
COPYDIR="/mnt/storage/Downloads/"

# thumbdir - Directory with your thumbnails saved as (showid).(jpg/png)
thumbdir="/mnt/tank/thumbnails/"

# Pull filename from SickBeard Variable and strip directory name
FULLPATH="$1"
FILENAME=${FULLPATH##*/}
FILENOEXT=${FILENAME%.*}

# Get Date & Time for Log File
date=$(date "+%d/%m/%y")
time=$(date "+%k:%M")

# Find out extension of thumbnail from thumbnail directory
ID="$3"
filename=$(basename $thumbdir$ID.*)
extension=${filename##*.}
filename=${filename%.*}


if [ -e ${thumbdir}${ID}.${extension} ];
then
    # thumbnail exists, so copy and report to log file.
    cp "${thumbdir}${ID}.${extension}" "${COPYDIR}${FILENOEXT}.${extension}"
    # set thumbnail permissions (gets copied with none, don't know why) 
    chmod 666 "${COPYDIR}${FILENOEXT}.${extension}"
    echo -e "$date - $time" \\r >> $LOGFILE
    echo -e "Show ID: $ID - Thumbnail created: ${FILENOEXT}.${extension}" \\r >> $LOGFILE
    echo -e " " \\r >> $LOGFILE
else
    urlthumb=$(${wgetdir}wget -q -O- "http://fanart.tv/api/fanart.php?id=$ID&sortby=favdesc" | ${grepdir}grep -m 1 -o '<tvthumb url="[^"]*' | ${grepdir}grep -m 1 -o '[^"]*$')
    urlclearart=$(${wgetdir}wget -q -O- "http://fanart.tv/api/fanart.php?id=$ID&sortby=favdesc" | ${grepdir}grep -m 1 -o '<clearart url="[^"]*' | ${grepdir}grep -m 1 -o '[^"]*$')

    if [ -n "$urlthumb" ]
    then
        # If thumbnail found on fanart.tv download and save to thumbdir then copy to download dir like normal
        ${fetchdir}fetch -o "${thumbdir}${ID}.jpg" "$urlthumb" 
        chmod 666 "${thumbdir}${ID}.jpg"
        cp "${thumbdir}${ID}.jpg" "${COPYDIR}${FILENOEXT}.jpg"
        chmod 666 "${COPYDIR}${FILENOEXT}.jpg"
        echo -e "$date - $time" \\r >> $LOGFILE
        echo -e "Downloaded $urlthumb" \\r >> $LOGFILE
        echo -e "Show ID: $ID - Thumbnail created: ${FILENOEXT}.jpg" \\r >> $LOGFILE
        echo -e " " \\r >> $LOGFILE
    elif [ -n "$urlclearart" ]
    then
        # If no thumbnail found try clearart
        ${fetchdir}fetch -o "${thumbdir}${ID}.png" "$urlclearart"
        chmod 666 "${thumbdir}${ID}.png"
        cp "${thumbdir}${ID}.png" "${COPYDIR}${FILENOEXT}.png"
        chmod 666 "${COPYDIR}${FILENOEXT}.png"
        echo -e "$date - $time" \\r >> $LOGFILE
        echo -e "Downloaded $urlclearart" \\r >> $LOGFILE
        echo -e "Show ID: $ID - Thumbnail created: ${FILENOEXT}.png" \\r >> $LOGFILE
        echo -e " " \\r >> $LOGFILE
    else
    # if it can't get thumbnail or clearart then copy the blank as a last resort.
    cp "${thumbdir}blank.png" "${COPYDIR}${FILENOEXT}.png"
    # set thumbnail permissions (gets copied with none, don't know why)
    chmod 666 "${COPYDIR}${FILENOEXT}.png"
    echo -e "$date - $time" \\r >> $LOGFILE
    echo -e "WARNING: Thumbnail does not exist, using blank thumbnail." \\r >> $LOGFILE
    echo -e "Show ID: $ID - Thumbnail created: ${FILENOEXT}.png" \\r >> $LOGFILE
    echo -e " " \\r >> $LOGFILE
    fi
fi