def identify_file(self,
                      file_path,
                      test_profile_ids,
                      force_short_audio=False):
        """Enrolls a profile using an audio file and returns a
        dictionary of the enrollment response.

        Arguments:
        file_path -- the file path of the audio file to test
        test_profile_ids -- an array of test profile IDs strings
        force_short_audio -- instruct the service to waive the recommended minimum audio limit
                             needed for enrollment
        """
        try:
            # Prepare the request
            if len(test_profile_ids) < 1:
                raise Exception(
                    'Error identifying file: no test profile IDs are provided.'
                )
            test_profile_ids_str = ','.join(test_profile_ids)
            request_url = '{0}?identificationProfileIds={1}&{2}={3}'.format(
                self._IDENTIFICATION_URI,
                urllib.parse.quote(test_profile_ids_str),
                self._SHORT_AUDIO_PARAMETER_NAME, force_short_audio)

            # Prepare the body of the message
            if type(file_path) == type('str'):
                with open(file_path, 'rb') as body:
                    # Send the request
                    res, message = self._send_request(
                        'POST', self._BASE_URI, request_url,
                        self._STREAM_CONTENT_HEADER_VALUE, body)
            else:
                body = file_path
                res, message = self._send_request(
                    'POST', self._BASE_URI, request_url,
                    self._STREAM_CONTENT_HEADER_VALUE, body)

            if res.status == self._STATUS_OK:
                # Parse the response body
                return IdentificationResponse.IdentificationResponse(
                    json.loads(message))
            elif res.status == self._STATUS_ACCEPTED:
                operation_url = res.getheader(self._OPERATION_LOCATION_HEADER)

                return IdentificationResponse.IdentificationResponse(
                    self._poll_operation(operation_url))
            else:
                reason = res.reason if not message else message
                raise Exception('Error identifying file: ' + reason)
        except:
            logging.error('Error identifying file.')
            raise
Exemple #2
0
    def identify_file(self, file_path, test_profile_ids):
        """Enrolls a profile using an audio file and returns a
        dictionary of the enrollment response.

        Arguments:
        file_path -- the file path of the audio file to test
        test_profile_ids -- an array of test profile IDs strings
        """
        try:
            # Prepare the request
            if len(test_profile_ids) < 1:
                raise Exception(
                    'Error identifying file: no test profile IDs are provided.'
                )
            test_profile_ids_str = ','.join(test_profile_ids)
            request_url = '{0}?identificationProfileIds={1}'.format(
                self._IDENTIFICATION_URI,
                urllib.parse.quote(test_profile_ids_str))

            # Prepare the body of the message
            with open(file_path, 'rb') as body:
                # Send the request
                res, message = self._send_request(
                    'POST', self._BASE_URI, request_url,
                    self._STREAM_CONTENT_HEADER_VALUE, body)

            if res.status == self._STATUS_OK:
                # Parse the response body
                return IdentificationResponse.IdentificationResponse(
                    json.loads(message))
            elif res.status == self._STATUS_ACCEPTED:
                operation_url = res.getheader(self._OPERATION_LOCATION_HEADER)

                return IdentificationResponse.IdentificationResponse(
                    self._poll_operation(operation_url))
            else:
                reason = res.reason if not message else message
                raise Exception('Error identifying file: ' + reason)
        except:
            logging.error('Error identifying file.')
            raise