# -*- coding: utf-8 -*-

"""
unmollom.voice_recognition


"""

import requests
import json
import wave
from exceptions import RecognitionException, CommunicationException, AudioFormatException
from tempfile import NamedTemporaryFile
from pydub import AudioSegment


class GoogleSpeechRecognition(object):
    def __init__(self):
        self.samplerate = '48000'
        self.url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
        self.headers={'Content-Type': 'audio/x-flac; rate=%s' % self.samplerate, 'User-Agent':'Mozilla/5.0'}

    def recognize(self, audio_file_name, format='mp3'):
        flac = self.convert_to_flac(audio_file_name, format)
        result = self.send_request(self.url, flac, self.headers)
        if result.ok:
            try:
                response = json.loads(result.text)
                if response['status'] == 0 and len(response['hypotheses']) > 0:
                    hypo = response['hypotheses'][0]
                    return {'confidence' : hypo['confidence'], 'text' : hypo['utterance']}
                else:
                    raise RecognitionException("Voice recognition failed")
            except ValueError:
                raise RecognitionException("Speech Recognition Server did not return a json")
            except KeyError: 
                raise RecognitionException("Server returned the wrong json structure")
        else:
            raise CommunicationException("Google does not like you!: " + result.text)

    def convert_to_flac(self, audio_file_name, format):
        if format == 'flac':
            return open(audio_file_name,'rb').read()
        converted = NamedTemporaryFile('rb')
        try:
            audio = AudioSegment.from_file(audio_file_name, format=format)
            audio.export(converted.name, format='flac', parameters=['-ar', self.samplerate])
            flac = converted.read()
            converted.close()
            return flac
        except (EOFError,wave.Error): #unfortunately pydub uses a lot of different exceptions
            raise AudioFormatException("file not in given format or unsupported format")

    def send_request(self, url, data, headers):
        """
        returns a result object with:
            result.ok   = True/False 
            result.text = server response text

        either a requests respnose object or a hardcoded stub. 
        """
        return  requests.post(url, data=data, headers=headers)

def recognize_file(audio_file_name, format='mp3'):
    return GoogleSpeechRecognition().recognize(audio_file_name, format)

def recognize(audio_data, format='mp3'):
    input_file = NamedTemporaryFile('wb')
    input_file.write(audio_data)
    input_file.flush()
    ret = recognize_file(input_file.name, format)
    input_file.close()
    return ret

@@ -1181,16 +1181,17 @@
  except 
+(
 ValueErr
@@ -1192,16 +1192,27 @@
 lueError
+, KeyError)
 :%0A      
@@ -1283,121 +1283,22 @@
 did 
-not 
 return 
-a json%22)%0A            except KeyError: %0A                raise RecognitionException(%22Server returned the
+no or a
  wro
@@ -1309,17 +1309,14 @@
 son 
-structure
+format
 %22)%0A 

simplify the recognition exceptions