Add continueOnFail support to AWS Transcribe Node

This commit is contained in:
Jan Oberhauser 2021-05-29 21:50:07 -05:00
parent 2d9c2e1a1e
commit a9184e38f8

View file

@ -411,132 +411,141 @@ export class AwsTranscribe implements INodeType {
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
if (resource === 'transcriptionJob') { try {
//https://docs.aws.amazon.com/comprehend/latest/dg/API_DetectDominantLanguage.html if (resource === 'transcriptionJob') {
if (operation === 'create') { //https://docs.aws.amazon.com/comprehend/latest/dg/API_DetectDominantLanguage.html
const transcriptionJobName = this.getNodeParameter('transcriptionJobName', i) as string; if (operation === 'create') {
const mediaFileUri = this.getNodeParameter('mediaFileUri', i) as string; const transcriptionJobName = this.getNodeParameter('transcriptionJobName', i) as string;
const detectLang = this.getNodeParameter('detectLanguage', i) as boolean; const mediaFileUri = this.getNodeParameter('mediaFileUri', i) as string;
const detectLang = this.getNodeParameter('detectLanguage', i) as boolean;
const options = this.getNodeParameter('options', i, {}) as IDataObject; const options = this.getNodeParameter('options', i, {}) as IDataObject;
const body: IDataObject = { const body: IDataObject = {
TranscriptionJobName: transcriptionJobName, TranscriptionJobName: transcriptionJobName,
Media: { Media: {
MediaFileUri: mediaFileUri, MediaFileUri: mediaFileUri,
}, },
}; };
if (detectLang) { if (detectLang) {
body.IdentifyLanguage = detectLang; body.IdentifyLanguage = detectLang;
} else { } else {
body.LanguageCode = this.getNodeParameter('languageCode', i) as string; body.LanguageCode = this.getNodeParameter('languageCode', i) as string;
}
if (options.channelIdentification) {
Object.assign(body.Settings, { ChannelIdentification: options.channelIdentification });
}
if (options.MaxAlternatives) {
Object.assign(body.Settings, {
ShowAlternatives: options.maxAlternatives,
MaxAlternatives: options.maxAlternatives,
});
}
if (options.showSpeakerLabels) {
Object.assign(body.Settings, {
ShowSpeakerLabels: options.showSpeakerLabels,
MaxSpeakerLabels: options.maxSpeakerLabels,
});
}
if (options.vocabularyName) {
Object.assign(body.Settings, {
VocabularyName: options.vocabularyName,
});
}
if (options.vocabularyFilterName) {
Object.assign(body.Settings, {
VocabularyFilterName: options.vocabularyFilterName,
});
}
if (options.vocabularyFilterMethod) {
Object.assign(body.Settings, {
VocabularyFilterMethod: options.vocabularyFilterMethod,
});
}
const action = 'Transcribe.StartTranscriptionJob';
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = responseData.TranscriptionJob;
} }
//https://docs.aws.amazon.com/transcribe/latest/dg/API_DeleteTranscriptionJob.html
if (operation === 'delete') {
const transcriptionJobName = this.getNodeParameter('transcriptionJobName', i) as string;
if (options.channelIdentification) { const body: IDataObject = {
Object.assign(body.Settings, { ChannelIdentification: options.channelIdentification }); TranscriptionJobName: transcriptionJobName,
};
const action = 'Transcribe.DeleteTranscriptionJob';
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = { success: true };
} }
//https://docs.aws.amazon.com/transcribe/latest/dg/API_GetTranscriptionJob.html
if (operation === 'get') {
const transcriptionJobName = this.getNodeParameter('transcriptionJobName', i) as string;
const resolve = this.getNodeParameter('returnTranscript', 0) as boolean;
if (options.MaxAlternatives) { const body: IDataObject = {
Object.assign(body.Settings, { TranscriptionJobName: transcriptionJobName,
ShowAlternatives: options.maxAlternatives, };
MaxAlternatives: options.maxAlternatives,
}); const action = 'Transcribe.GetTranscriptionJob';
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = responseData.TranscriptionJob;
if (resolve === true && responseData.TranscriptionJobStatus === 'COMPLETED') {
responseData = await this.helpers.request({ method: 'GET', uri: responseData.Transcript.TranscriptFileUri, json: true });
const simple = this.getNodeParameter('simple', 0) as boolean;
if (simple === true) {
responseData = { transcript: responseData.results.transcripts.map((data: IDataObject) => data.transcript).join(' ') };
}
}
} }
//https://docs.aws.amazon.com/transcribe/latest/dg/API_ListTranscriptionJobs.html
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject;
const action = 'Transcribe.ListTranscriptionJobs';
const body: IDataObject = {};
if (options.showSpeakerLabels) { if (filters.status) {
Object.assign(body.Settings, { body['Status'] = filters.status;
ShowSpeakerLabels: options.showSpeakerLabels, }
MaxSpeakerLabels: options.maxSpeakerLabels,
});
}
if (options.vocabularyName) { if (filters.jobNameContains) {
Object.assign(body.Settings, { body['JobNameContains'] = filters.jobNameContains;
VocabularyName: options.vocabularyName, }
});
}
if (options.vocabularyFilterName) { if (returnAll === true) {
Object.assign(body.Settings, { responseData = await awsApiRequestRESTAllItems.call(this, 'TranscriptionJobSummaries', 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
VocabularyFilterName: options.vocabularyFilterName,
});
}
if (options.vocabularyFilterMethod) { } else {
Object.assign(body.Settings, { const limit = this.getNodeParameter('limit', i) as number;
VocabularyFilterMethod: options.vocabularyFilterMethod, body['MaxResults'] = limit;
}); responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
} responseData = responseData.TranscriptionJobSummaries;
const action = 'Transcribe.StartTranscriptionJob';
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = responseData.TranscriptionJob;
}
//https://docs.aws.amazon.com/transcribe/latest/dg/API_DeleteTranscriptionJob.html
if (operation === 'delete') {
const transcriptionJobName = this.getNodeParameter('transcriptionJobName', i) as string;
const body: IDataObject = {
TranscriptionJobName: transcriptionJobName,
};
const action = 'Transcribe.DeleteTranscriptionJob';
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = { success: true };
}
//https://docs.aws.amazon.com/transcribe/latest/dg/API_GetTranscriptionJob.html
if (operation === 'get') {
const transcriptionJobName = this.getNodeParameter('transcriptionJobName', i) as string;
const resolve = this.getNodeParameter('returnTranscript', 0) as boolean;
const body: IDataObject = {
TranscriptionJobName: transcriptionJobName,
};
const action = 'Transcribe.GetTranscriptionJob';
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = responseData.TranscriptionJob;
if (resolve === true && responseData.TranscriptionJobStatus === 'COMPLETED') {
responseData = await this.helpers.request({ method: 'GET', uri: responseData.Transcript.TranscriptFileUri, json: true });
const simple = this.getNodeParameter('simple', 0) as boolean;
if (simple === true) {
responseData = { transcript: responseData.results.transcripts.map((data: IDataObject) => data.transcript).join(' ') };
} }
} }
} }
//https://docs.aws.amazon.com/transcribe/latest/dg/API_ListTranscriptionJobs.html
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject;
const action = 'Transcribe.ListTranscriptionJobs';
const body: IDataObject = {};
if (filters.status) { if (Array.isArray(responseData)) {
body['Status'] = filters.status; returnData.push.apply(returnData, responseData as IDataObject[]);
} } else {
returnData.push(responseData as IDataObject);
if (filters.jobNameContains) {
body['JobNameContains'] = filters.jobNameContains;
}
if (returnAll === true) {
responseData = await awsApiRequestRESTAllItems.call(this, 'TranscriptionJobSummaries', 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
} else {
const limit = this.getNodeParameter('limit', i) as number;
body['MaxResults'] = limit;
responseData = await awsApiRequestREST.call(this, 'transcribe', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
responseData = responseData.TranscriptionJobSummaries;
}
} }
}
if (Array.isArray(responseData)) { } catch (error) {
returnData.push.apply(returnData, responseData as IDataObject[]); if (this.continueOnFail()) {
} else { returnData.push({ error: error.message });
returnData.push(responseData as IDataObject); continue;
}
throw error;
} }
} }
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];