Add Google Translate node (#1086)

*  Add Google Translate node

* 🔨 Add autoload for target languages

*  Small improvements

Co-authored-by: Tanay Pant <tanaypant@protonmail.com>
This commit is contained in:
Ricardo Espinoza 2020-10-22 05:36:42 -04:00 committed by GitHub
parent 3e56d5fc08
commit b2e3b8de16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 384 additions and 0 deletions

View file

@ -0,0 +1,25 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
const scopes = [
'https://www.googleapis.com/auth/cloud-translation',
];
export class GoogleTranslateOAuth2Api implements ICredentialType {
name = 'googleTranslateOAuth2Api';
extends = [
'googleOAuth2Api',
];
displayName = 'Google Translate OAuth2 API';
documentationUrl = 'google';
properties = [
{
displayName: 'Scope',
name: 'scope',
type: 'hidden' as NodePropertyTypes,
default: scopes.join(' '),
},
];
}

View file

@ -0,0 +1,128 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
import * as moment from 'moment-timezone';
import * as jwt from 'jsonwebtoken';
export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount') as string;
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: uri || `https://translation.googleapis.com${resource}`,
json: true
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
if (authenticationMethod === 'serviceAccount') {
const credentials = this.getCredentials('googleApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const { access_token } = await getAccessToken.call(this, credentials as IDataObject);
options.headers!.Authorization = `Bearer ${access_token}`;
//@ts-ignore
return await this.helpers.request(options);
} else {
//@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'googleTranslateOAuth2Api', options);
}
} catch (error) {
if (error.response && error.response.body && error.response.body.message) {
// Try to return the error prettier
throw new Error(`Google Translate error response [${error.statusCode}]: ${error.response.body.message}`);
}
throw error;
}
}
export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
query.maxResults = 100;
do {
responseData = await googleApiRequest.call(this, method, endpoint, body, query);
query.pageToken = responseData['nextPageToken'];
returnData.push.apply(returnData, responseData[propertyName]);
} while (
responseData['nextPageToken'] !== undefined &&
responseData['nextPageToken'] !== ''
);
return returnData;
}
function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, credentials: IDataObject): Promise<IDataObject> {
//https://developers.google.com/identity/protocols/oauth2/service-account#httprest
const scopes = [
'https://www.googleapis.com/auth/cloud-translation',
'https://www.googleapis.com/auth/cloud-platform',
];
const now = moment().unix();
const signature = jwt.sign(
{
'iss': credentials.email as string,
'sub': credentials.email as string,
'scope': scopes.join(' '),
'aud': `https://oauth2.googleapis.com/token`,
'iat': now,
'exp': now + 3600,
},
credentials.privateKey as string,
{
algorithm: 'RS256',
header: {
'kid': credentials.privateKey as string,
'typ': 'JWT',
'alg': 'RS256',
},
}
);
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signature,
},
uri: 'https://oauth2.googleapis.com/token',
json: true
};
//@ts-ignore
return this.helpers.request(options);
}

View file

@ -0,0 +1,194 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
googleApiRequest,
} from './GenericFunctions';
export interface IGoogleAuthCredentials {
email: string;
privateKey: string;
}
export class GoogleTranslate implements INodeType {
description: INodeTypeDescription = {
displayName: 'Google Translate',
name: 'googleTranslate',
icon: 'file:googletranslate.svg',
group: ['input', 'output'],
version: 1,
description: 'Translate data using Google Translate',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
defaults: {
name: 'Google Translate',
color: '#5390f5',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'googleApi',
required: true,
displayOptions: {
show: {
authentication: [
'serviceAccount',
],
},
},
},
{
name: 'googleTranslateOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Service Account',
value: 'serviceAccount',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'serviceAccount',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Language',
value: 'language',
},
],
default: 'language',
description: 'The operation to perform',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'language',
],
},
},
options: [
{
name: 'Translate',
value: 'translate',
description: 'Translate data',
},
],
default: 'translate',
description: 'The operation to perform',
},
// ----------------------------------
// All
// ----------------------------------
{
displayName: 'Query',
name: 'query',
type: 'string',
default: '',
description: 'The input text to translate',
required: true,
displayOptions: {
show: {
operation: [
'translate',
],
},
},
},
{
displayName: 'Translate To',
name: 'translateTo',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getLanguages',
},
default: '',
description: 'The language to use for translation of the input text, set to one of the<br/> language codes listed in <a href="https://cloud.google.com/translate/docs/languages">Language Support</a>',
required: true,
displayOptions: {
show: {
operation: [
'translate',
],
},
},
},
],
};
methods = {
loadOptions: {
async getLanguages(
this: ILoadOptionsFunctions
): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { data: { languages } } = await googleApiRequest.call(
this,
'GET',
'/language/translate/v2/languages'
);
for (const language of languages) {
returnData.push({
name: language.language.toUpperCase(),
value: language.language
});
}
return returnData;
},
}
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const length = items.length as unknown as number;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
const responseData = [];
for (let i = 0; i < length; i++) {
if (resource === 'language') {
if (operation === 'translate') {
const query = this.getNodeParameter('query', i) as string;
const translateTo = this.getNodeParameter('translateTo', i) as string;
const response = await googleApiRequest.call(this, 'POST', `/language/translate/v2`, { q: query, target: translateTo });
responseData.push(response.data.translations[0]);
}
}
}
return [this.helpers.returnJsonArray(responseData)];
}
}

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Ebene_1" xmlns:x="http://ns.adobe.com/Extensibility/1.0/" xmlns:i="http://ns.adobe.com/AdobeIllustrator/10.0/" xmlns:graph="http://ns.adobe.com/Graphs/1.0/" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 998.1 998.3" enable-background="new 0 0 998.1 998.3" xml:space="preserve">
<metadata>
<sfw xmlns="http://ns.adobe.com/SaveForWeb/1.0/">
<slices/>
<sliceSourceBounds bottomLeftOrigin="true" height="998.3" width="998.1" x="1.9" y="1.5"/>
</sfw>
</metadata>
<g>
<path fill="#DBDBDB" d="M931.7,998.3c36.5,0,66.4-29.4,66.4-65.4V265.8c0-36-29.9-65.4-66.4-65.4H283.6l260.1,797.9H931.7z"/>
<g>
<g>
<path fill="#DCDCDC" d="M931.7,230.4c9.7,0,18.9,3.8,25.8,10.6c6.8,6.7,10.6,15.5,10.6,24.8v667.1c0,9.3-3.7,18.1-10.6,24.8 c-6.9,6.8-16.1,10.6-25.8,10.6H565.5L324.9,230.4H931.7 M931.7,200.4H283.6l260.1,797.9h388c36.5,0,66.4-29.4,66.4-65.4V265.8 C998.1,229.8,968.2,200.4,931.7,200.4L931.7,200.4z"/>
</g>
</g>
<g>
<polygon fill="#4352B8" points="482.3,809.8 543.7,998.3 714.4,809.8 "/>
</g>
<path fill="#607988" d="M936.1,476.1V437H747.6v-63.2h-61.2V437H566.1v39.1h239.4c-12.8,45.1-41.1,87.7-68.7,120.8 c-48.9-57.9-49.1-76.7-49.1-76.7h-50.8c0,0,2.1,28.2,70.7,108.6c-22.3,22.8-39.2,36.3-39.2,36.3l15.6,48.8c0,0,23.6-20.3,53.1-51.6 c29.6,32.1,67.8,70.7,117.2,116.7l32.1-32.1c-52.9-48-91.7-86.1-120.2-116.7c38.2-45.2,77-102.1,85.2-154.2h84.6V476.1z"/>
<path fill="#4285F4" d="M66.4,0C29.9,0,0,29.9,0,66.5v677c0,36.5,29.9,66.4,66.4,66.4h648.1L454.4,0H66.4z"/>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="534.3" y1="433.2" x2="998.1" y2="433.2">
<stop offset="0" style="stop-color:#FFFFFF;stop-opacity:0.2"/>
<stop offset="1" style="stop-color:#FFFFFF;stop-opacity:2.000000e-02"/>
</linearGradient>
<path fill="url(#SVGID_1_)" enable-background="new " d="M534.3,200.4h397.4c36.5,0,66.4,29.4,66.4,65.4V666L534.3,200.4z"/>
<path fill="#EEEEEE" d="M371.4,430.6c-2.5,30.3-28.4,75.2-91.1,75.2c-54.3,0-98.3-44.9-98.3-100.2s44-100.2,98.3-100.2 c30.9,0,51.5,13.4,63.3,24.3l41.2-39.6c-27.1-25-62.4-40.6-104.5-40.6c-86.1,0-156,69.9-156,156s69.9,156,156,156 c90.2,0,149.8-63.3,149.8-152.6c0-12.8-1.6-22.2-3.7-31.8h-146v53.4L371.4,430.6L371.4,430.6z"/>
</g>
<radialGradient id="SVGID_2_" cx="65.2077" cy="19.3655" r="1398.2709" gradientUnits="userSpaceOnUse">
<stop offset="0" style="stop-color:#FFFFFF;stop-opacity:0.1"/>
<stop offset="1" style="stop-color:#FFFFFF;stop-opacity:0"/>
</radialGradient>
<path fill="url(#SVGID_2_)" d="M931.7,200.4H518.8L454.4,0h-388C29.9,0,0,29.9,0,66.5v677c0,36.5,29.9,66.4,66.4,66.4h415.9 l61.4,188.4h388c36.5,0,66.4-29.4,66.4-65.4V666V265.8C998.1,229.8,968.2,200.4,931.7,200.4z"/>
</svg>

After

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -82,6 +82,7 @@
"dist/credentials/GoogleSheetsOAuth2Api.credentials.js", "dist/credentials/GoogleSheetsOAuth2Api.credentials.js",
"dist/credentials/GSuiteAdminOAuth2Api.credentials.js", "dist/credentials/GSuiteAdminOAuth2Api.credentials.js",
"dist/credentials/GoogleTasksOAuth2Api.credentials.js", "dist/credentials/GoogleTasksOAuth2Api.credentials.js",
"dist/credentials/GoogleTranslateOAuth2Api.credentials.js",
"dist/credentials/YouTubeOAuth2Api.credentials.js", "dist/credentials/YouTubeOAuth2Api.credentials.js",
"dist/credentials/GumroadApi.credentials.js", "dist/credentials/GumroadApi.credentials.js",
"dist/credentials/HarvestApi.credentials.js", "dist/credentials/HarvestApi.credentials.js",
@ -273,6 +274,7 @@
"dist/nodes/Google/GSuiteAdmin/GSuiteAdmin.node.js", "dist/nodes/Google/GSuiteAdmin/GSuiteAdmin.node.js",
"dist/nodes/Google/Sheet/GoogleSheets.node.js", "dist/nodes/Google/Sheet/GoogleSheets.node.js",
"dist/nodes/Google/Task/GoogleTasks.node.js", "dist/nodes/Google/Task/GoogleTasks.node.js",
"dist/nodes/Google/Translate/GoogleTranslate.node.js",
"dist/nodes/Google/YouTube/YouTube.node.js", "dist/nodes/Google/YouTube/YouTube.node.js",
"dist/nodes/GraphQL/GraphQL.node.js", "dist/nodes/GraphQL/GraphQL.node.js",
"dist/nodes/Gumroad/GumroadTrigger.node.js", "dist/nodes/Gumroad/GumroadTrigger.node.js",