TV, XML and me

I have recently discovered that the program I use on my AppleTV to browse our media share, ATVFiles, has the facility to parse XML about the file and display that in a very nice way. So I whipped up a script to pull down data from thetvdb.com, parse out the data I need into a new XML file that ATVFiles understands, and clean up after itself. In addition, since I name all my files SXXEXX.avi I grab the official screen shots of the show so now all the metadata is really nice, clean, and accurate. Plus, it is way easier to see whether I have seen a show or not already. Here’s the script, please ignore the underscores in the section where it actually writes the XML file, blogger gave me a hard time, and I’m too lazy to look into it that much.:

#!/bin/bash

# Setup the storage directories
HD=/storage/HDTV
TV=/storage/TV

# Tell us what we’ve provided
echo -e “\n[ $(date +%T) ] Show is: $1”
sleep 1
echo -e “\n[ $(date +%T) ] Season is: $2”
sleep 1
echo -e “\n[ $(date +%T) ] Episode is: $3”
sleep 1
echo -e “\n[ $(date +%T) ] TV type is: $4\n”

show=”$1″

#Get the series ID, so we can dig deeper
wget -O /tmp/”$show”.xml “http://www.thetvdb.com/api/GetSeries.php?seriesname=$show”

if [ -z $5 ]; then
series=$(grep seriesid /tmp/”$show”.xml | head -1 | sed -e ‘s/\(\)\([0-9]*\)\(\)/\2/’)
else
series=$5
fi

echo -e “/n[ $(date +%T) ] Series number is: $series\n”

# Take the series ID and get the episode ID, so we can dig ever more deeply
wget -O /tmp/S0$2E$3.xml http://www.thetvdb.com/api/30A6C059BE4318B9/series/$series/default/$2/$3/en.xml
episodeid=$(grep id /tmp/S0$2E$3.xml | head -1 | sed -e ‘s:\([0-9]*\):\1:’ | awk -F ” ” ‘{print $1}’)

# Set some variables we’ll need for writing the XML file
description=$(grep \ /tmp/S0$2E$3.xml | sed -e ‘s:\(\)\(.*\)\(\):\2:’ | sed ‘s/^ *//’)
title=$(grep \ /tmp/S0$2E$3.xml | sed -e ‘s:\(\)\(.*\)\(\):\2:’ | sed ‘s/^ *//’)

# Write the XML data to a file
cat > /tmp/file.xml << EOF

$title
$description
$show
$3
$2

EOF

# Move the XML data to the appropriate location
if [ $4 = HDTV ]; then
if [ $3 -lt 10 ]; then
mv -v /tmp/file.xml $HD/”$show”/S0$2E0$3.xml
wget -O $HD/”$show”/S0$2E0$3.jpg http://www.thetvdb.com/banners/episodes/$series-$episodeid.jpg
else
mv -v /tmp/file.xml $HD/”$show”/S0$2E$3.xml
wget -O $HD/”$show”/S0$2E$3.jpg http://www.thetvdb.com/banners/episodes/$series-$episodeid.jpg
fi
else
if [ $3 -lt 10 ]; then
mv -v /tmp/file.xml $TV/”$show”/S0$2E0$3.xml
wget -O $TV/”$show”/S0$2E0$3.jpg http://www.thetvdb.com/banners/episodes/$series-$episodeid.jpg
else
mv -v /tmp/file.xml $TV/”$show”/S0$2E$3.xml
wget -O $TV/”$show”/S0$2E$3.jpg http://www.thetvdb.com/banners/episodes/$series-$episodeid.jpg
fi
fi

# Cleanup unecessary files
rm /tmp/”$show”.xml
rm /tmp/S0$2E$3.xml

echo -e “\n[ $(date +%T) ] Done!”
exit 0

There are a few things to note here. First of all, you run it like this:

tvxml.sh Lost 5 1 HDTV

This tells the script that you want Lost, Season 5, Episode 1, and it’s in the HDTV folder, TV is the other option. If the show name has more than one word, you’ll have to put the name in quotes, like this:

tvxml.sh “Battlestar Galactica” 4 15 HDTV

In addition, Battlestar pulled up three different versions of the show, so I had to go find out the show id that http://www.thetvdb.com uses, and manually include it like this:

tvxml.sh “Battlestar Galactica” 4 15 HDTV 73545

Also, if you want to grab an entire directory of shows at once, it’s pretty simple with a little for loop:

for i in $(ls | sed -e ‘s/S04E\(..\).mp4/\1/’); do /usr/local/bin/tvxml.sh “Battlestar Galactica” 4 $i HDTV 73545; done

This loop just says, for every episode number in the /storage/HDTV/Battlestar\ Galactica/
directory run the script with the approriate episode number. Easy Peasy. This was run right IN the /storage/HDTV/Battlestar\ Galactica directory so I didn’t need to explicity specify that in the for loop.

In the future I will make this interactive by pulling out all the names and show id’s out of the first xml file grabbed, and let the user decide which one they’re looking for. This happens so rarely for me that it’s a version 2 feature of this script.

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