import os
import subprocess
from urllib import quote_plus

from text_manipulators import split_text
from sounds import SoundPlayer

class GoogleTextToSpeech(object):

    def __init__(self, url_string=None):

        self.url_string = url_string
        if not url_string:
            self.url_string = u"http://translate.google.com/translate_tts?tl=en_gb&ie=UTF-8&q=%s"

    def say(self, text):

        if len(text) > 100:
            phrases = split_text(text, 100)
            for phrase in phrases:
                self.say(phrase)

            return

        text = quote_plus(text.encode("utf-8"))

        filename = "speech/%s.mp3" % text

        if not os.path.isfile(filename):
            f = open(filename, "w")
            subprocess.call(
                    ['curl','-A Mozilla', self.url_string % (text)], 
                    stdout=f)

        s = SoundPlayer()
        s.play_sound(filename)

class EspeakTextToSpeech(object):

    def __init__(self, speak_path="espeak", wpm=150):

        self.speak_path = speak_path
        self.wpm_string = "-s %s" % wpm 

    def say(self, text): 

        subprocess.call([self.speak_path, text, self.wpm_string])
@@ -644,16 +644,184 @@
 %25 text%0A%0A
+        self.get_file(filename, self.url_string %25 (text))%0A%0A        s = SoundPlayer()%0A        s.play_sound(filename)%0A%0A    def get_file(self, filename, url, retries=3):%0A%0A
         
@@ -964,32 +964,11 @@
 a', 
-self.url_string %25 (text)
+url
 %5D, %0A
@@ -997,25 +997,24 @@
 dout=f)%0A
-%0A
         
 s = Soun
@@ -1009,55 +1009,112 @@
     
-s = SoundPlayer()%0A        s.play_sound(filename
+if os.path.getsize(filename) and retries %3E 0:%0A            self.get_file(filename, url, retries=retries-1
 )%0A%0Ac

Fix for bad google