from django.http import HttpResponse, JsonResponse
import boto3
from boto3.s3.transfer import S3Transfer
import requests
from django.conf import settings
from dls.models import SurveyReport
from myproject.celery import app

@app.task
def text_to_speech(survey_report_id, original_name):
    import speech_recognition as sr
    r = sr.Recognizer()
    credentials = {
        'aws_access_key_id': settings.AWS_ACCESS_KEY,
        'aws_secret_access_key': settings.AWS_SECRET_KEY
    }
    client = boto3.client('s3', 'us-east-1', **credentials)
    client.download_file('dls2018', 'recording/'+str(original_name),  settings.MEDIA_ROOT+'recordings/'+str(original_name))
    audio = settings.MEDIA_ROOT+'recordings/'+str(original_name)
    with sr.AudioFile(audio) as source:
        audio = r.record(source)

    vessel_description = r.recognize_google(audio)
    survey_update = SurveyReport.objects.get(pk=int(survey_report_id))
    survey_update.vessel_description = str(vessel_description)
    survey_update.save()
