#!/bin/bash

#This script works with a standard Tivo HD with dual tuners. It's meant to be put in cron. Mine is run every minute on Ubuntu 9.04.
#Requires wget, curl, and html2text.

#Replace the following with your own Twitter and Tivo info. 
TW_USER=30West
TW_PASS=password
TIVO_IP=192.168.1.29
TIVO_MAC=5555555555

FILE=/tmp/nowplaying.html

wget --no-check-certificate --http-user=tivo --http-password=$TIVO_MAC -O $FILE "https://$TIVO_IP/nowplaying/index.html?Recurse=Yes" > /dev/null
HEADTMP=`html2text -nobs -width 200 $FILE | grep progress | grep -v HD\ Recordings | grep -v transfer | head -n1 | cut -c 70-160 | sed 's/^[ \t]*//;s/[ \t]*$//'`
TAILTMP=`html2text -nobs -width 200 $FILE | grep progress | grep -v HD\ Recordings | grep -v transfer | tail -n1 | cut -c 70-160 | sed 's/^[ \t]*//;s/[ \t]*$//'`

echo $HEADTMP > /tmp/tivotmp1
echo $TAILTMP > /tmp/tivotmp2
HEAD=`</tmp/tivotmp1`
TAIL=`</tmp/tivotmp2`
TIVO1=`</tmp/tivo1`
TIVO2=`</tmp/tivo2`

if [ -n "$HEAD" ]; then
  if [ "$HEAD" = "$TAIL" ]; then
    if [[ "$HEAD" != "$TIVO1" && "$HEAD" != "$TIVO2" ]]; then
      curl --basic --user $TW_USER:$TW_PASS --data status="Recording: $HEAD" http://twitter.com/statuses/update.xml > /dev/null
      echo $HEAD > /tmp/tivo1
    fi
  else
    if [[ "$HEAD" != "$TIVO1"  && "$HEAD" != "$TIVO2" ]]; then
      curl --basic --user $TW_USER:$TW_PASS --data status="Recording: $HEAD" http://twitter.com/statuses/update.xml > /dev/null
      echo $HEAD > /tmp/tivo1
    fi
    if [[ "$TAIL" != "$TIVO1" && "$TAIL" != "$TIVO2" ]]; then 
      curl --basic --user $TW_USER:$TW_PASS --data status="Recording: $TAIL" http://twitter.com/statuses/update.xml > /dev/null
      echo $TAIL > /tmp/tivo2
    fi
  fi
fi

