mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
⚡ Improvements
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
BINARY_ENCODING,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
@@ -9,6 +11,7 @@ import {
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
INodePropertyOptions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
@@ -17,16 +20,16 @@ import {
|
||||
} from './TweetDescription';
|
||||
|
||||
import {
|
||||
chunks,
|
||||
twitterApiRequest,
|
||||
twitterApiRequestAllItems,
|
||||
} from './GenericFunctions';
|
||||
|
||||
import {
|
||||
ITweet,
|
||||
} from './TweetInterface';
|
||||
|
||||
import {
|
||||
snakeCase,
|
||||
} from 'change-case';
|
||||
const ISO6391 = require('iso-639-1');
|
||||
|
||||
export class Twitter implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
@@ -69,6 +72,26 @@ export class Twitter implements INodeType {
|
||||
],
|
||||
};
|
||||
|
||||
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;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: IDataObject[] = [];
|
||||
@@ -106,11 +129,74 @@ export class Twitter implements INodeType {
|
||||
continue;
|
||||
}
|
||||
|
||||
const attachmentBody = {
|
||||
media_data: binaryData[binaryPropertyName].data,
|
||||
media_category: snakeCase(attachment.category as string).toUpperCase(),
|
||||
};
|
||||
const response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
||||
let attachmentBody = {};
|
||||
let response: IDataObject = {};
|
||||
|
||||
if (binaryData[binaryPropertyName].mimeType.includes('image')) {
|
||||
|
||||
const attachmentBody = {
|
||||
media_data: binaryData[binaryPropertyName].data,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
||||
|
||||
} else {
|
||||
|
||||
// https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init
|
||||
|
||||
attachmentBody = {
|
||||
command: 'INIT',
|
||||
total_bytes: Buffer.from(binaryData[binaryPropertyName].data, BINARY_ENCODING).byteLength,
|
||||
media_type: binaryData[binaryPropertyName].mimeType,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
||||
|
||||
const mediaId = response.media_id_string;
|
||||
|
||||
// break the data on 5mb chunks (max size that can be uploaded at once)
|
||||
|
||||
const binaryParts = chunks(Buffer.from(binaryData[binaryPropertyName].data, BINARY_ENCODING), 5242880);
|
||||
|
||||
let index = 0;
|
||||
|
||||
for (const binaryPart of binaryParts) {
|
||||
|
||||
//https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-append
|
||||
|
||||
attachmentBody = {
|
||||
name: binaryData[binaryPropertyName].fileName,
|
||||
command: 'APPEND',
|
||||
media_id: mediaId,
|
||||
media_data: Buffer.from(binaryPart).toString('base64'),
|
||||
segment_index: index,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri);
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
//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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
mediaIds.push(response.media_id_string);
|
||||
}
|
||||
}
|
||||
@@ -133,6 +219,47 @@ export class Twitter implements INodeType {
|
||||
|
||||
responseData = await twitterApiRequest.call(this, 'POST', '/statuses/update.json', body);
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Array.isArray(responseData)) {
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
|
||||
Reference in New Issue
Block a user