###TeCoed###
import sys, subprocess, urllib, time, tweepy

# == OAuth Authentication ==
#
# This mode of authentication is the new preferred way
# of authenticating with Twitter.

# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located 
# under "Your access token")
access_token= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

def getSpeech(phrase):
    googleAPIurl = "http://translate.google.com/translate_tts?tl=en&"
    param = {'q': phrase}
    data = urllib.urlencode(param)
    googleAPIurl += data # Combine the data from the Google API
    return googleAPIurl
 
def raspberryTalk(text): # This will call mplayer and will play the sound
    subprocess.call(["mplayer",getSpeech(text)], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

public_tweets = api.mentions_timeline()
for tweet in public_tweets:
    print tweet.text
    raspberryTalk(tweet.text)
    time.sleep(30) # delay of 30 seconds to allow google time to process
