Exemple #1
0
def upload_progress_callback(encoder):
    encoder_len = total_len(encoder)
    bar = ProgressBar(expected_size=encoder_len, filled_char='=')

    def callback(monitor):
        bar.show(monitor.bytes_read)

    return callback
    def __init__(self, file, cipher, taglen=16):
        self.file = file
        self.size = total_len(file) + taglen
        file.seek(0)
        self.fpos = 0

        self.cipher = cipher
        self.taglen = taglen
        self.tagio = None
Exemple #3
0
    def _generate_recognize_payload(self, audio):
        """
        prepare event payload for speech Recognize event. if the audio file-like does not specify a total length via
        __len__, len, or similar, it is assumed that audio is a continuous stream. in this case the NEAR_FIELD profile
        is used and a custom file-like wrapper is constructed to deliver the multi-part body. however, if a total
        length of the audio can be determined, the CLOSE_TALK profile will be used.

        :param audio: file-like containing audio for request.
        :return: file-like containing the payload for the http request
        """
        event = self._generate_recognize_speech_event(self.speech_profile)
        if total_len(audio) is None:
            boundary_term = str(uuid.uuid4())
            boundary_separator = b'--' + boundary_term.encode('utf8') + b'\r\n'
            body = b''.join([
                boundary_separator, _RECOGNIZE_METADATA_PART_HEADER,
                json.dumps(event).encode('utf8'), b'\r\n\r\n',
                boundary_separator, _RECOGNIZE_AUDIO_PART_HEADER
            ])
            epilogue = b'\r\n--' + boundary_term.encode('utf8') + b'--\r\n'

            class MultiPartAudioFileLike:
                def __init__(self):
                    self._audio_closed = False
                    self.content_type = 'multipart/form-data; boundary={}'.format(
                        boundary_term)
                    self._audio_buffer = b''

                def read(self, size=-1):
                    nonlocal body
                    nonlocal epilogue
                    ret = b''
                    if len(body):
                        ret = body[:size]
                        body = body[size:]
                    remaining = size - len(ret)
                    if remaining and len(self._audio_buffer):
                        ret += self._audio_buffer[:remaining]
                        self._audio_buffer = self._audio_buffer[remaining:]
                    remaining = size - len(ret)
                    if remaining and not self._audio_closed:
                        audio_data = audio.read(remaining)
                        if len(audio_data) < remaining:
                            self._audio_closed = True
                        self._audio_buffer += audio_data
                        ret += self._audio_buffer[:remaining]
                        self._audio_buffer = self._audio_buffer[remaining:]
                    remaining = size - len(ret)
                    if remaining:
                        if len(epilogue):
                            ret += epilogue[:remaining]
                            epilogue = epilogue[remaining:]
                    return ret

            return MultiPartAudioFileLike()
        else:
            payload = MultipartEncoder({
                'metadata': (None, io.BytesIO(json.dumps(event).encode()),
                             'application/json'),
                'audio': (None, audio, 'application/octet-stream')
            })
        return payload