use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionService method registerCallback.
/**
* Register the callback url with the Speech-to-text service. From:
* https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#register_callback
*
* curl -X POST -u "{username}":"{password}" --data "{}"
* "https://stream.watsonplatform.net/speech-to-text/api/v1/register_callback?callback_url=http://{user_callback_path}/results&user_secret=ThisIsMySecret"
* Response looks like: { "status": "created", "url": "http://{user_callback_path}/results" }
*/
void registerCallback() throws TranscriptionServiceException {
if (callbackAlreadyRegistered)
return;
Organization org = securityService.getOrganization();
String adminUrl = StringUtils.trimToNull(org.getProperties().get(ADMIN_URL_PROPERTY));
if (adminUrl != null)
callbackUrl = adminUrl + CALLBACK_PATH;
else
callbackUrl = serverUrl + CALLBACK_PATH;
logger.info("Callback url is {}", callbackUrl);
CloseableHttpClient httpClient = makeHttpClient();
HttpPost httpPost = new HttpPost(IBM_WATSON_SERVICE_URL + REGISTER_CALLBACK + String.format("?callback_url=%s", callbackUrl));
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
switch(code) {
case // 200
HttpStatus.SC_OK:
logger.info("Callback url: {} had already already been registered", callbackUrl);
callbackAlreadyRegistered = true;
EntityUtils.consume(response.getEntity());
break;
case // 201
HttpStatus.SC_CREATED:
logger.info("Callback url: {} has been successfully registered", callbackUrl);
callbackAlreadyRegistered = true;
EntityUtils.consume(response.getEntity());
break;
case // 400
HttpStatus.SC_BAD_REQUEST:
logger.warn("Callback url {} could not be verified, status: {}", callbackUrl, code);
break;
case // 503
HttpStatus.SC_SERVICE_UNAVAILABLE:
logger.warn("Service unavailable when registering callback url {} status: {}", callbackUrl, code);
break;
default:
logger.warn("Unknown status when registering callback url {}, status: {}", callbackUrl, code);
break;
}
} catch (Exception e) {
logger.warn("Exception when calling the the register callback endpoint", e);
} finally {
try {
httpClient.close();
if (response != null)
response.close();
} catch (IOException e) {
}
}
}
use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionService method transcriptionError.
@Override
public void transcriptionError(String mpId, Object obj) throws TranscriptionServiceException {
JSONObject jsonObj = null;
String jobId = null;
try {
jsonObj = (JSONObject) obj;
jobId = (String) jsonObj.get("id");
// Update state in database
database.updateJobControl(jobId, TranscriptionJobControl.Status.Error.name());
TranscriptionJobControl jobControl = database.findByJob(jobId);
logger.warn(String.format("Error callback received for media package %s, job id %s", jobControl.getMediaPackageId(), jobId));
// Send notification email
sendEmail("Transcription ERROR", String.format("There was a transcription error for for media package %s, job id %s.", jobControl.getMediaPackageId(), jobId));
} catch (TranscriptionDatabaseException e) {
logger.warn("Transcription error. State in db could not be updated to error for mpId {}, jobId {}", mpId, jobId);
throw new TranscriptionServiceException("Could not update transcription job control db", e);
}
}
use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class StartTranscriptionOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
String skipOption = StringUtils.trimToNull(operation.getConfiguration(SKIP_IF_FLAVOR_EXISTS));
if (skipOption != null) {
MediaPackageElement[] mpes = mediaPackage.getElementsByFlavor(MediaPackageElementFlavor.parseFlavor(skipOption));
if (mpes != null && mpes.length > 0) {
logger.info("Start transcription operation will be skipped because flavor {} already exists in the media package", skipOption);
return createResult(Action.SKIP);
}
}
logger.debug("Start transcription for mediapackage {} started", mediaPackage);
// Check which tags have been configured
String sourceTagOption = StringUtils.trimToNull(operation.getConfiguration(SOURCE_TAG));
String sourceFlavorOption = StringUtils.trimToNull(operation.getConfiguration(SOURCE_FLAVOR));
AbstractMediaPackageElementSelector<Track> elementSelector = new TrackSelector();
// Make sure either one of tags or flavors are provided
if (StringUtils.isBlank(sourceTagOption) && StringUtils.isBlank(sourceFlavorOption))
throw new WorkflowOperationException("No source tag or flavor have been specified!");
if (StringUtils.isNotBlank(sourceFlavorOption)) {
String flavor = StringUtils.trim(sourceFlavorOption);
try {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
}
}
if (sourceTagOption != null)
elementSelector.addTag(sourceTagOption);
Collection<Track> elements = elementSelector.select(mediaPackage, false);
Job job = null;
for (Track track : elements) {
if (track.hasVideo()) {
logger.info("Skipping track {} since it contains a video stream", track);
continue;
}
try {
job = service.startTranscription(mediaPackage.getIdentifier().compact(), track);
// Only one job per media package
break;
} catch (TranscriptionServiceException e) {
throw new WorkflowOperationException(e);
}
}
if (job == null) {
logger.info("No matching tracks found");
return createResult(mediaPackage, Action.CONTINUE);
}
// Wait for the jobs to return
if (!waitForStatus(job).isSuccess()) {
throw new WorkflowOperationException("Transcription job did not complete successfully");
}
// Return OK means that the ibm watson job was created, but not finished yet
logger.debug("External transcription job for mediapackage {} was created", mediaPackage);
// Results are empty, we should get a callback when transcription is done
return createResult(Action.CONTINUE);
}
Aggregations