move getting trasncripts logic to helpers

This commit is contained in:
2019-02-20 11:39:16 -05:00
parent 2a3b85a8b5
commit 797f5a3bfe
4 changed files with 46 additions and 32 deletions

View File

@@ -6,7 +6,7 @@ with open('README.md') as file:
setup(
name="tatt",
version="0.942",
version="0.943",
py_modules=['tatt'],
url='https://github.com/zevaverbach/tatt',
install_requires=[

View File

@@ -6,3 +6,10 @@ class ConfigError(Exception):
class AlreadyExistsError(Exception):
pass
class DoesntExistError(Exception):
pass
class NotAvailable(Exception):
pass

View File

@@ -1,5 +1,4 @@
from tatt import config
from tatt import vendors
from tatt import config, exceptions, vendors
LB = '\n'
TAB = '\t'
@@ -25,6 +24,20 @@ def make_string_all_services(free_only=False):
return all_services_string + '\n'
def get_job(job_name):
job = helpers.get_transcription_jobs_dict().get(name)
if not job:
raise exceptions.DoesntExistError
if job['status'].lower() != 'completed':
raise exceptions.NotAvailable(f'transcript status is {job["status"]}')
def get_transcript(job_name):
job = get_job(job_name)
service = get_service(job['service_name'])
return service.retrieve_transcript(name)
def get_service(service_name):
module = vendors.SERVICES[service_name]
return getattr(module, config.SERVICE_CLASS_NAME)

View File

@@ -14,22 +14,23 @@ def cli():
@cli.command()
@click.option('-f', '--file', is_flag=True, help='save to file')
@click.option('-s', '--save', is_flag=True, help='save to file')
@click.argument('name')
def get(name, file):
def get(name, save):
"""Downloads and/or saves completed transcript."""
job = helpers.get_transcription_jobs_dict().get(name)
if not job:
try:
transcript = get_transcript(name)
except exceptions.DoesntExistError:
raise click.ClickException(f'no such transcript {name}')
if job['status'].lower() != 'completed':
raise click.ClickException(f'transcript status is {job["status"]}')
service = helpers.get_service(job['service_name'])
if not file:
pprint(service.retrieve_transcript(name))
except exceptions.NotAvailable as e:
raise click.ClickException(str(e))
if not save:
click.echo(transcript)
else:
with open(f'{name}.json', 'w') as fout:
fout.write(json.dumps(service.retrieve_transcript(name)))
print(f'Okay, downloaded {name}.json')
fout.write(json.dumps(transcript))
click.echo(f'Okay, downloaded {name}.json')
@@ -61,34 +62,27 @@ def services(free_only):
@cli.command()
@click.option('-d', '--dry-run', is_flag=True, help=(
'Do a dry run without actually submitting the media file for transcription'))
@click.argument('media_filepath', type=str)
@click.argument('service_name', type=str)
def this(dry_run, media_filepath, service_name):
"""Sends a media file to be transcribed."""
if service_name not in vendors.SERVICES:
print()
try:
service = helpers.get_service(service_name)
except KeyError as e:
raise click.ClickException(
f'No such service! {print_all_services(print_=False)}')
service = helpers.get_service(service_name)
try:
s = service(media_filepath)
except exceptions.ConfigError as e:
raise click.ClickException(str(e))
if dry_run:
print('If this weren\'t a dry run, I would transcribe '
f'{media_filepath} using {service_name}')
pprint(vars(s))
else:
print(
f'Okay, transcribing {media_filepath} using {service_name}...')
click.echo(
f'Okay, transcribing {media_filepath} using {service_name}...')
try:
job_num = s.transcribe()
except exceptions.AlreadyExistsError as e:
raise click.ClickException(str(e))
print(f'Okay, job {job_num} is being transcribed. Use "get" '
'command to download it.')
try:
job_num = s.transcribe()
except exceptions.AlreadyExistsError as e:
raise click.ClickException(str(e))
click.echo(f'Okay, job {job_num} is being transcribed. Use "get" '
'command to download it.')