2020-06-01 17:42:38 -07:00
|
|
|
|
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
BINARY_ENCODING,
|
2020-06-01 17:42:38 -07:00
|
|
|
IExecuteFunctions,
|
2020-06-07 14:29:13 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-06-03 06:06:13 -07:00
|
|
|
} from 'n8n-core';
|
2020-06-01 17:42:38 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodePropertyOptions,
|
2020-06-01 17:42:38 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
tweetFields,
|
|
|
|
tweetOperations,
|
|
|
|
} from './TweetDescription';
|
|
|
|
|
|
|
|
import {
|
2020-06-07 14:29:13 -07:00
|
|
|
chunks,
|
2020-06-01 17:42:38 -07:00
|
|
|
twitterApiRequest,
|
2020-06-07 14:29:13 -07:00
|
|
|
twitterApiRequestAllItems,
|
2020-06-01 17:42:38 -07:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ITweet,
|
2020-06-03 06:06:13 -07:00
|
|
|
} from './TweetInterface';
|
2020-09-17 14:02:23 -07:00
|
|
|
import { isDate } from 'util';
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2020-06-07 14:29:13 -07:00
|
|
|
const ISO6391 = require('iso-639-1');
|
2020-06-01 17:42:38 -07:00
|
|
|
|
|
|
|
export class Twitter implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Twitter ',
|
|
|
|
name: 'twitter',
|
|
|
|
icon: 'file:twitter.png',
|
|
|
|
group: ['input', 'output'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Consume Twitter API',
|
|
|
|
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
|
|
|
defaults: {
|
|
|
|
name: 'Twitter',
|
|
|
|
color: '#1DA1F2',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'twitterOAuth1Api',
|
|
|
|
required: true,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Tweet',
|
|
|
|
value: 'tweet',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'tweet',
|
|
|
|
description: 'The resource to operate on.',
|
|
|
|
},
|
|
|
|
// TWEET
|
|
|
|
...tweetOperations,
|
|
|
|
...tweetFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-06-07 14:29:13 -07:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the available languages to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getLanguages(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const languages = ISO6391.getAllNames();
|
|
|
|
for (const language of languages) {
|
|
|
|
const languageName = language;
|
|
|
|
const languageId = ISO6391.getCode(language);
|
|
|
|
returnData.push({
|
|
|
|
name: languageName,
|
|
|
|
value: languageId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const length = items.length as unknown as number;
|
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
if (resource === 'tweet') {
|
2020-06-03 06:06:13 -07:00
|
|
|
// https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
|
2020-06-01 17:42:38 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
const text = this.getNodeParameter('text', i) as string;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const body: ITweet = {
|
|
|
|
status: text,
|
|
|
|
};
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2020-09-17 14:02:23 -07:00
|
|
|
if (additionalFields.inReplyToStatusId) {
|
|
|
|
body.in_reply_to_status_id = additionalFields.inReplyToStatusId as string;
|
2020-09-19 01:05:17 -07:00
|
|
|
body.auto_populate_reply_metadata = true;
|
2020-09-17 14:02:23 -07:00
|
|
|
}
|
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
if (additionalFields.attachments) {
|
2020-06-01 17:42:38 -07:00
|
|
|
const mediaIds = [];
|
2020-06-07 15:54:45 -07:00
|
|
|
const attachments = additionalFields.attachments as string;
|
2020-06-01 17:42:38 -07:00
|
|
|
const uploadUri = 'https://upload.twitter.com/1.1/media/upload.json';
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
const attachmentProperties: string[] = attachments.split(',').map((propertyName) => {
|
|
|
|
return propertyName.trim();
|
|
|
|
});
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
for (const binaryPropertyName of attachmentProperties) {
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
const binaryData = items[i].binary as IBinaryKeyData;
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
if (binaryData === undefined) {
|
|
|
|
throw new Error('No binary data set. So file can not be written!');
|
|
|
|
}
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
if (!binaryData[binaryPropertyName]) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
let attachmentBody = {};
|
|
|
|
let response: IDataObject = {};
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-08 13:13:17 -07:00
|
|
|
const isAnimatedWebp = (Buffer.from(binaryData[binaryPropertyName].data, 'base64').toString().indexOf('ANMF') !== -1);
|
|
|
|
|
|
|
|
const isImage = binaryData[binaryPropertyName].mimeType.includes('image');
|
|
|
|
|
|
|
|
if (isImage && isAnimatedWebp) {
|
2020-06-13 04:47:49 -07:00
|
|
|
throw new Error('Animated .webp images are not supported use .gif instead');
|
2020-06-08 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isImage) {
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
const attachmentBody = {
|
|
|
|
media_data: binaryData[binaryPropertyName].data,
|
|
|
|
};
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
} else {
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
// https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
attachmentBody = {
|
|
|
|
command: 'INIT',
|
|
|
|
total_bytes: Buffer.from(binaryData[binaryPropertyName].data, BINARY_ENCODING).byteLength,
|
|
|
|
media_type: binaryData[binaryPropertyName].mimeType,
|
|
|
|
};
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
const mediaId = response.media_id_string;
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
// break the data on 5mb chunks (max size that can be uploaded at once)
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
const binaryParts = chunks(Buffer.from(binaryData[binaryPropertyName].data, BINARY_ENCODING), 5242880);
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
let index = 0;
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
for (const binaryPart of binaryParts) {
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
//https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-append
|
2020-06-07 14:29:13 -07:00
|
|
|
|
|
|
|
attachmentBody = {
|
2020-06-07 15:54:45 -07:00
|
|
|
name: binaryData[binaryPropertyName].fileName,
|
|
|
|
command: 'APPEND',
|
2020-06-07 14:29:13 -07:00
|
|
|
media_id: mediaId,
|
2020-06-07 15:54:45 -07:00
|
|
|
media_data: Buffer.from(binaryPart).toString('base64'),
|
|
|
|
segment_index: index,
|
2020-06-07 14:29:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
index++;
|
2020-06-07 14:29:13 -07:00
|
|
|
}
|
|
|
|
|
2020-06-07 15:54:45 -07:00
|
|
|
//https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-finalize
|
|
|
|
|
|
|
|
attachmentBody = {
|
|
|
|
command: 'FINALIZE',
|
|
|
|
media_id: mediaId,
|
|
|
|
};
|
|
|
|
|
|
|
|
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
|
|
|
|
|
|
|
// data has not been uploaded yet, so wait for it to be ready
|
|
|
|
if (response.processing_info) {
|
|
|
|
const { check_after_secs } = (response.processing_info as IDataObject);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve();
|
|
|
|
}, (check_after_secs as number) * 1000);
|
|
|
|
});
|
|
|
|
}
|
2020-06-01 17:42:38 -07:00
|
|
|
}
|
2020-06-07 15:54:45 -07:00
|
|
|
|
|
|
|
mediaIds.push(response.media_id_string);
|
2020-06-01 17:42:38 -07:00
|
|
|
}
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
body.media_ids = mediaIds.join(',');
|
|
|
|
}
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
if (additionalFields.possiblySensitive) {
|
|
|
|
body.possibly_sensitive = additionalFields.possibly_sensitive as boolean;
|
|
|
|
}
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
if (additionalFields.locationFieldsUi) {
|
|
|
|
const locationUi = additionalFields.locationFieldsUi as IDataObject;
|
|
|
|
if (locationUi.locationFieldsValues) {
|
|
|
|
const values = locationUi.locationFieldsValues as IDataObject;
|
|
|
|
body.lat = parseFloat(values.lalatitude as string);
|
|
|
|
body.long = parseFloat(values.lalatitude as string);
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', '/statuses/update.json', body);
|
|
|
|
}
|
2020-06-07 14:29:13 -07:00
|
|
|
// https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
|
|
|
|
if (operation === 'search') {
|
|
|
|
const q = this.getNodeParameter('searchText', i) as string;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const qs: IDataObject = {
|
|
|
|
q
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.includeEntities) {
|
|
|
|
qs.include_entities = additionalFields.includeEntities as boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.resultType) {
|
|
|
|
qs.response_type = additionalFields.resultType as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.until) {
|
|
|
|
qs.until = additionalFields.until as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.lang) {
|
|
|
|
qs.lang = additionalFields.lang as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.locationFieldsUi) {
|
|
|
|
const locationUi = additionalFields.locationFieldsUi as IDataObject;
|
|
|
|
if (locationUi.locationFieldsValues) {
|
|
|
|
const values = locationUi.locationFieldsValues as IDataObject;
|
|
|
|
qs.geocode = `${values.latitude as string},${values.longitude as string},${values.distance}${values.radius}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await twitterApiRequestAllItems.call(this, 'statuses', 'GET', '/search/tweets.json', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.count = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = await twitterApiRequest.call(this, 'GET', '/search/tweets.json', {}, qs);
|
|
|
|
responseData = responseData.statuses;
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
//https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-create
|
|
|
|
if (operation === 'like') {
|
|
|
|
const tweetId = this.getNodeParameter('tweetId', i) as string;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const qs: IDataObject = {
|
|
|
|
id: tweetId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.includeEntities) {
|
|
|
|
qs.include_entities = additionalFields.includeEntities as boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', '/favorites/create.json', {}, qs);
|
|
|
|
}
|
|
|
|
//https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-retweet-id
|
|
|
|
if (operation === 'retweet') {
|
|
|
|
const tweetId = this.getNodeParameter('tweetId', i) as string;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const qs: IDataObject = {
|
|
|
|
id: tweetId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.trimUser) {
|
|
|
|
qs.trim_user = additionalFields.trimUser as boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', `/statuses/retweet/${tweetId}.json`, {}, qs);
|
|
|
|
}
|
2020-06-01 17:42:38 -07:00
|
|
|
}
|
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else if (responseData !== undefined) {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|