#!/bin/bash

if [ x`which festival` == "x" ]
then
    echo "You need to install festival"
    exit
fi

python -c "import json" 1>/dev/null 2>1 

if [ $? == 1 ]
then
    echo "You need to install python-json"
    exit
fi

TWITTERURL="http://twitter.com/statuses/friends_timeline.json"
JSON="/tmp/twittline.json"
SPEECH="/tmp/twt.message"
PYCODE="/tmp/twt2speech.py"

read -p "Username: " TUSER && \
read -sp "Password: " TPASS && \
curl -s -u $TUSER:$TPASS $TWITTERURL > $JSON

cat > $PYCODE << "EOF"
import json
import sys
import re
urlp="(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"
twits = json.load(open(sys.argv[1]))
for twit in twits:
    text = twit['user']['name']+' says: '+twit['text']
    text = re.sub(urlp, '', text)
    print text
EOF

python $PYCODE $JSON > $SPEECH

while read line
do
notify-send -t 15000 "$line"
echo $line | festival --tts
sleep 1
done < $SPEECH

echo "THE END" | festival --tts

# cleanup
rm -f $JSON
rm -f $PYCODE
rm -f $SPEECH

