Search in sources :

Example 6 with TranscriptionServiceException

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) {
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) TranscriptionDatabaseException(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException)

Example 7 with TranscriptionServiceException

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);
    }
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) JSONObject(org.json.simple.JSONObject) TranscriptionDatabaseException(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException)

Example 8 with TranscriptionServiceException

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);
}
Also used : TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException) Track(org.opencastproject.mediapackage.Track)

Aggregations

TranscriptionServiceException (org.opencastproject.transcription.api.TranscriptionServiceException)8 TranscriptionDatabaseException (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException)6 IOException (java.io.IOException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 JSONObject (org.json.simple.JSONObject)4 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)4 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 TranscriptionJobControl (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl)3 HttpEntity (org.apache.http.HttpEntity)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 JSONParser (org.json.simple.parser.JSONParser)2 File (java.io.File)1 URI (java.net.URI)1 StatusLine (org.apache.http.StatusLine)1 FileEntity (org.apache.http.entity.FileEntity)1 Test (org.junit.Test)1 Job (org.opencastproject.job.api.Job)1 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1