Saturday, 7 September 2013

Automatic log manager for Game of Thrones series.

I watch a lot of anime. When I started watching One Piece which had 500+ episodes, it was very hard for me to keep track of how many episodes I have seen already. So I used to keep a log file which always had the next episode's number and I used to manually update it whenever I watch an episode.

That was a good idea but not good enough :P. At least for awesome people like me doing work manually is too mainstream xD.
If you do all the work what is your computer there for???
So I wrote a script which automatically manages the log. And next time when I run the script it will continue from the place I stopped last time \m/. Now I am planning to start Game of Thrones TV series. People tell I'll like it because I am a big fan of Age of Empires and I love wars :D.

So now what I am planning to do is to automate viewing it just like I did for One piece. In the version of Game of Thrones which I have, each episode name has SxxExx in it. For example S02E09 is there in the file name of episode 9 in season 2. So using this I can iterate over series and inside that i can iterate over episodes.

Setup

Download the gist from here or copy it from here:
#!/bin/bash
# log is 1:2
echo -n "how many episodes u wanna see: "
read count
while [ $count -ne 0 ]
do
log=`cat log`
expr "$log" : "\(.*\):.*"
series=`expr "$log" : "\(.*\):.*"`
episode=`expr "$log" : ".*:\(.*\)"`
if [ `echo "$series<10"|bc` -ne 0 ]
then
series=0$series
fi
if [ `echo "$episode<10"|bc` -ne 0 ]
then
episode=0$episode
fi
file=S"$series"E"$episode"
file=`find . -name "*$file*"`
#if file exists then play else go to next series
if [ "$file" ]
then
# vlc is run in full screen. If u r not using vlc change this line
vlc -f "$file"
count=`expr $count - 1`
series=`expr "$log" : "\(.*\):.*"`
episode=`expr "$log" : ".*:\(.*\)"`
episode=`expr $episode + 1`
echo "$series:$episode">log
echo "none">error.log
else
error_log=`cat error.log`
if [ "$error_log" == "error" ]
then
# this will just print a fancy message :P change it if u want
figlet end of episodes
exit
fi
echo "error">error.log
series=`expr "$log" : "\(.*\):.*"`
series=`expr $series + 1`
echo "$series:1">log
fi
done
view raw script.sh hosted with ❤ by GitHub

Step1

Place the script in the base folder which has folders of all the series. Make it executable:
$ chmod +x script.sh

Step2

create a file called "log" in the same folder. The log file contains current series number and episode number. For example if you are watching third episode of season 1, put 1:3 in that file and save it.

Running:

Just go that folder in terminal and do this:
$ ./script.sh

Now just enter the  number of episodes you want to watch in a stretch and enjoy!