|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Client used or interacting with our language identification api""" |
| 3 | + |
| 4 | +import json |
| 5 | +from .generic_api_client import GenericApiClient |
| 6 | +from .models import LanguageIdentificationJob, LanguageIdentificationResult |
| 7 | + |
| 8 | +try: |
| 9 | + from urllib.parse import urljoin |
| 10 | +except ImportError: |
| 11 | + from urlparse import urljoin |
| 12 | + |
| 13 | + |
| 14 | +class LanguageIdentificationClient(GenericApiClient): |
| 15 | + """Client for interacting with the Rev AI language identification api""" |
| 16 | + |
| 17 | + # Default version of Rev AI language identification api |
| 18 | + api_version = 'v1' |
| 19 | + |
| 20 | + # Default api name of Rev AI language identification api |
| 21 | + api_name = 'languageid' |
| 22 | + |
| 23 | + def __init__(self, access_token): |
| 24 | + """Constructor |
| 25 | +
|
| 26 | + :param access_token: access token which authorizes all requests and links them to your |
| 27 | + account. Generated on the settings page of your account dashboard |
| 28 | + on Rev AI. |
| 29 | + """ |
| 30 | + |
| 31 | + GenericApiClient.__init__(self, access_token, self.api_name, self.api_version, |
| 32 | + LanguageIdentificationJob.from_json, |
| 33 | + LanguageIdentificationResult.from_json) |
| 34 | + |
| 35 | + def submit_job_url( |
| 36 | + self, |
| 37 | + media_url, |
| 38 | + metadata=None, |
| 39 | + callback_url=None, |
| 40 | + delete_after_seconds=None): |
| 41 | + """Submit media as a URL for language identification. |
| 42 | + The audio data is downloaded from the URL. |
| 43 | +
|
| 44 | + :param media_url: web location of the media file |
| 45 | + :param metadata: info to associate with the language identification job |
| 46 | + :param callback_url: callback url to invoke on job completion as a webhook |
| 47 | + :param delete_after_seconds: number of seconds after job completion when job is auto-deleted |
| 48 | + :returns: raw response data |
| 49 | + :raises: HTTPError |
| 50 | + """ |
| 51 | + if not media_url: |
| 52 | + raise ValueError('media_url must be provided') |
| 53 | + |
| 54 | + payload = self._enhance_payload({'media_url': media_url}, |
| 55 | + metadata, callback_url, delete_after_seconds) |
| 56 | + |
| 57 | + return self._submit_job(payload) |
| 58 | + |
| 59 | + def submit_job_local_file( |
| 60 | + self, |
| 61 | + filename, |
| 62 | + metadata=None, |
| 63 | + callback_url=None, |
| 64 | + delete_after_seconds=None): |
| 65 | + """Submit a local file for language identification. |
| 66 | + Note that the content type is inferred if not provided. |
| 67 | +
|
| 68 | + :param filename: path to a local file on disk |
| 69 | + :param metadata: info to associate with the language identification job |
| 70 | + :param callback_url: callback url to invoke on job completion as a webhook |
| 71 | + :param delete_after_seconds: number of seconds after job completion when job is auto-deleted |
| 72 | + :returns: raw response data |
| 73 | + :raises: HTTPError |
| 74 | + """ |
| 75 | + if not filename: |
| 76 | + raise ValueError('filename must be provided') |
| 77 | + |
| 78 | + payload = self._enhance_payload({}, metadata, callback_url, delete_after_seconds) |
| 79 | + |
| 80 | + with open(filename, 'rb') as f: |
| 81 | + files = { |
| 82 | + 'media': (filename, f), |
| 83 | + 'options': (None, json.dumps(payload, sort_keys=True)) |
| 84 | + } |
| 85 | + |
| 86 | + response = self._make_http_request( |
| 87 | + "POST", |
| 88 | + urljoin(self.base_url, 'jobs'), |
| 89 | + files=files |
| 90 | + ) |
| 91 | + |
| 92 | + return LanguageIdentificationJob.from_json(response.json()) |
| 93 | + |
| 94 | + def get_result_json(self, id_): |
| 95 | + """Get result of a language identification job as json. |
| 96 | +
|
| 97 | + :param id_: id of job to be requested |
| 98 | + :returns: job result data as raw json |
| 99 | + :raises: HTTPError |
| 100 | + """ |
| 101 | + return self._get_result_json(id_, {}) |
| 102 | + |
| 103 | + def get_result_object(self, id_): |
| 104 | + """Get result of a language identification job as LanguageIdentificationResult object. |
| 105 | +
|
| 106 | + :param id_: id of job to be requested |
| 107 | + :returns: job result data as LanguageIdentificationResult object |
| 108 | + :raises: HTTPError |
| 109 | + """ |
| 110 | + return self._get_result_object(id_, {}) |
0 commit comments