#!/usr/bin/env python

import argparse
import io

def transcribe_file_with_auto_punctuation(path,pathtxt):
    """Transcribe the given audio file with auto punctuation enabled."""
    # [START speech_transcribe_auto_punctuation]
    from google.cloud import speech
    client = speech.SpeechClient()

    # path = 'resources/commercial_mono.wav'
    with io.open(path, 'rb') as audio_file:
        content = audio_file.read()

    audio = speech.RecognitionAudio(content=content)
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=8000,
        language_code='en-US',
        # Enable automatic punctuation
        enable_automatic_punctuation=True)

    response = client.recognize(config=config, audio=audio)

    f = open(pathtxt,"a")
    for i, result in enumerate(response.results):
        alternative = result.alternatives[0]
        #print('{}'.format(alternative.transcript))
        f.write(alternative.transcript+"\r\n")
    # [END speech_transcribe_auto_punctuation]
    f.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('path', help='File to stream to the API')

    args = parser.parse_args()
    pathtxt = args.path + ".txt"
    transcribe_file_with_auto_punctuation(args.path,pathtxt)
