1

few weeks ago I was able to make it finally work. Sometimes I got this error 400 but using try except in Python and repeating the request two or three times until it works was enough. Now it keeps repeating the request and every time it is the same error I get back. This is my code. Does anybody experience the same?

def stt_google_wav(audio_fname):


    print "Sending ", audio_fname
    #Convert to flac first
    filename = audio_fname
    del_flac = False
    if 'flac' not in filename:
        del_flac = True
        print "Converting to flac"
        print FLAC_CONV + filename
        os.system(FLAC_CONV + ' ' + filename)
        filename = filename.split('.')[0] + '.flac'

    f = open(filename, 'rb')
    flac_cont = f.read()
    f.close()


    # Headers. A common Chromium (Linux) User-Agent
    hrs = {"User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7",
       'Content-type': 'audio/x-flac; rate=16000'} 

    req = urllib2.Request(GOOGLE_SPEECH_URL, data=flac_cont, headers=hrs)
    print "Sending request to Google TTS"
    #print "response", response
    while True:    
        try:
            p = urllib2.urlopen(req)
            response = p.read()
            res = eval(response)['hypotheses']
        except:
            print "Couldn't parse service response"
            res = None
            continue
        break


    if del_flac:
        os.remove(filename) # Remove temp file

    return res  
NorthCat
  • 9,643
  • 16
  • 47
  • 50
Mehdi
  • 1,370
  • 3
  • 15
  • 26

0 Answers0