Skip to content

Commit

Permalink
add parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrrk committed Nov 26, 2024
1 parent 172ffac commit 020da79
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions src/rev_ai/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,95 +337,125 @@ def get_list_of_jobs(self, limit=None, starting_after=None):

return [Job.from_json(job) for job in response.json()]

def get_transcript_text(self, id_):
def get_transcript_text(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as plain text.
:param id_: id of job to be requested
:param group_channels_by: optional, group channels by speaker or time
:param group_channels_threshold_ms: optional, group channels by time threshold in milliseconds
:returns: transcript data as text
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')

url = self._build_transcript_url(self, id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms)

response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript'.format(id_)),
url,
headers={'Accept': 'text/plain'}
)

return response.text

def get_transcript_text_as_stream(self, id_):
def get_transcript_text_as_stream(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as a plain text stream.
:param id_: id of job to be requested
:param group_channels_by: optional, group channels by speaker or time
:param group_channels_threshold_ms: optional, group channels by time threshold in milliseconds
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')

url = self._build_transcript_url(self, id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms)

response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript'.format(id_)),
url,
headers={'Accept': 'text/plain'},
stream=True
)

return response

def get_transcript_json(self, id_):
def get_transcript_json(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as json.
:param id_: id of job to be requested
:param group_channels_by: optional, group channels by speaker or time
:param group_channels_threshold_ms: optional, group channels by time threshold in milliseconds
:returns: transcript data as json
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')

url = self._build_transcript_url(self, id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms)

response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript'.format(id_)),
url,
headers={'Accept': self.rev_json_content_type}
)

return response.json()

def get_transcript_json_as_stream(self, id_):
def get_transcript_json_as_stream(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as streamed json.
:param id_: id of job to be requested
:param group_channels_by: optional, group channels by speaker or time
:param group_channels_threshold_ms: optional, group channels by time threshold in milliseconds
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')

url = self._build_transcript_url(self, id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms)

response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript'.format(id_)),
url,
headers={'Accept': self.rev_json_content_type},
stream=True
)

return response

def get_transcript_object(self, id_):
def get_transcript_object(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as a python object`.
:param id_: id of job to be requested
:param group_channels_by: optional, group channels by speaker or time
:param group_channels_threshold_ms: optional, group channels by time threshold in milliseconds
:returns: transcript data as a python object
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')

url = self._build_transcript_url(self, id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms)

response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript'.format(id_)),
url,
headers={'Accept': self.rev_json_content_type}
)

Expand Down Expand Up @@ -814,3 +844,20 @@ def _create_job_options_payload(

def _create_captions_query(self, speaker_channel):
return '' if speaker_channel is None else '?speaker_channel={}'.format(speaker_channel)

def _build_transcript_url(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Build the get transcript url.
:param id_: id of job to be requested
:param group_channels_by: optional, group channels by speaker or time
:param group_channels_threshold_ms: optional, group channels by time threshold in milliseconds
:returns: url for getting the transcript
"""
params = []
if group_channels_by is not None:
params.append('group_channels_by={}'.format(group_channels_by))
if group_channels_threshold_ms is not None:
params.append('group_channels_threshold_ms={}'.format(group_channels_threshold_ms))

query = '?{}'.format('&'.join(params))
return urljoin(self.base_url, 'jobs/{}/transcript{}'.format(id_, query))

0 comments on commit 020da79

Please sign in to comment.