# -*- coding: utf-8 -*-
import wave
import json
import requests
import pyaudio
from settings import HTTP_PROXY, HTTPS_PROXY

ASR_URL = "https://try-api.recaius.jp/asr/v1/"

class RecaiusASR(object):
    """Speech Recognizer by RECAIUS-dev API"""

    def __init__(self, recaius_id, recaius_password, lang):
        # TODO: Use proper id and password automatically according to lang
        self.recaius_id = recaius_id
        self.recaius_password = recaius_password
        self.lang = lang

        self._values = dict()
        self._values['id'] = self.recaius_id
        self._values['password'] = self.recaius_password

        model = dict()
        if self.lang == 'ja_JP':
            model['model_id'] = 1
        elif self.lang == 'en_US':
            model['model_id'] = 5
        elif self.lang == 'zh_CN':
            model['model_id'] = 7

        self._values['model'] = model

        # set proxies
        self.proxies = {
            'http': HTTP_PROXY,
            'https': HTTPS_PROXY,
        }

    def recognize(self, wave_file):
        uuid = self._login()

        w = wave.open(wave_file)

        p = pyaudio.PyAudio()
        stream = p.open(
            format=p.get_format_from_width(w.getsampwidth()),
            channels=w.getnchannels(),
            rate=w.getframerate(),
            output=True)
        chunk = 999999999
        data = w.readframes(chunk)
        voiceid = 1
        self._voice(uuid, voiceid, data, wave_file)
        response = self._voice(uuid, voiceid, b'', wave_file)
        print("==>", response.status_code, response.text)
        self._logout(uuid)

    def _login(self):
        # check necessary parameters
        if 'id' not in self._values:
            raise RecaiusASRException('Missing parameter: id')
        if 'password' not in self._values:
            raise RecaiusASRException('Missing parameter: password')
        if 'model' not in self._values:
            raise RecaiusASRException('Missing parameter: model')

        headers = {'Content-Type': 'application/json'}
        data = json.dumps(self._values)
        data = data.encode('utf-8')
        response = requests.post(ASR_URL + 'login', data=data, headers=headers, proxies=self.proxies)
        uuid = response.text

        return uuid

    def _logout(self, uuid):
        values = dict()
        values['uuid'] = uuid

        headers = {'Content-Type': 'application/json'}
        data = json.dumps(values)
        data = data.encode('utf-8')
        response = requests.post(ASR_URL + uuid + '/logout', data=data, headers=headers, proxies=self.proxies)
        uuid = response.text

        return

    def _voice(self, uuid, voiceid, pcm_data, wave_file):
        voice = open(wave_file, 'rb')
        files = {'voice': ('data.wav', voice, 'application/octet-stream')}
        data = {'voiceid': voiceid}
        response = requests.put(ASR_URL + uuid + '/voice', files=files, data=data, proxies=self.proxies)
        print(response.status_code)
        return response

    def _result(self, uuid):
        pass

class RecaiusASRException(Exception):
    pass

if __name__ == '__main__':
    from settings import ASR_ID, ASR_PASSWORD
    rec = RecaiusASR(ASR_ID, ASR_PASSWORD, 'ja_JP')
    rec.recognize('../recaius_test.wav')

@@ -1076,16 +1076,45 @@
 _login()
+%0A        print(%22uuid:%22, uuid)
 %0A%0A      
@@ -1116,16 +1116,17 @@
        w
+f
  = wave.
@@ -1241,16 +1241,17 @@
 _width(w
+f
 .getsamp
@@ -1282,16 +1282,17 @@
 annels=w
+f
 .getncha
@@ -1318,16 +1318,17 @@
   rate=w
+f
 .getfram
@@ -1381,17 +1381,13 @@
 k = 
-999999999
+31250
 %0A   
@@ -1399,16 +1399,17 @@
 data = w
+f
 .readfra
@@ -1432,16 +1432,17 @@
    voice
+_
 id = 1%0A 
@@ -1439,32 +1439,74 @@
 e_id = 1%0A       
+ while data != b'':%0A            response =
  self._voice(uui
@@ -1517,16 +1517,17 @@
 oice
+_
 id, data
 , wa
@@ -1522,28 +1522,140 @@
 id, data
-, wave_file)
+)%0A            print(%22%25d:%22 %25 voice_id, len(data), response)%0A            data = wf.readframes(chunk)%0A            voice_id += 1
 %0A       
@@ -1693,26 +1693,17 @@
 oice
+_
 id, 
-b'', wave_file
+data
 )%0A  
@@ -1719,34 +1719,103 @@
 nt(%22
-==%3E%22, response.status_code
+%25d:%22 %25 voice_id, len(data), response)%0A        response = self._result(uuid)%0A        print(%22==%3E%22
 , re
@@ -1820,21 +1820,16 @@
 response
-.text
 )%0A      
@@ -2910,16 +2910,17 @@
 d, voice
+_
 id, pcm_
@@ -2927,59 +2927,10 @@
 data
-, wave_file):%0A        voice = open(wave_file, 'rb'
 )
+:
 %0A   
@@ -2959,23 +2959,27 @@
  ('d
-ata
+ummy
 .wav', 
-voice
+pcm_data
 , 'a
@@ -3037,16 +3037,17 @@
 ': voice
+_
 id%7D%0A    
@@ -3155,22 +3155,23 @@
         
-print(
+return 
 response
@@ -3182,32 +3182,17 @@
 tus_code
-)%0A        return
+,
  respons
@@ -3192,16 +3192,21 @@
 response
+.text
 %0A%0A    de
@@ -3236,20 +3236,140 @@
         
-pass
+response = requests.get(ASR_URL + uuid + '/result', proxies=self.proxies)%0A        return response.status_code, response.text
 %0A%0Aclass 
@@ -3561,20 +3561,14 @@
 '../
-recaius_test
+sample
 .wav

Send voice requests by the chunk