n8n-nodes-instagram-private-api-wrapped
Version:
n8n community node for Instagram automation using private API capabilities
327 lines (326 loc) • 14.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Instagram = void 0;
const n8n_workflow_1 = require("n8n-workflow");
// Import the Instagram Client class
const client_1 = require("../lib/client");
class Instagram {
constructor() {
this.description = {
displayName: 'Instagram Private API',
name: 'instagram',
icon: 'file:instagram.svg',
group: ['social'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Interact with Instagram using private API methods',
defaults: {
name: 'Instagram Private API',
},
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
credentials: [
{
name: 'instagramApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'User',
value: 'user',
},
{
name: 'Media',
value: 'media',
},
{
name: 'Feed',
value: 'feed',
},
],
default: 'user',
},
// User Operations
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['user'],
},
},
options: [
{
name: 'Get Profile Info',
value: 'getUserInfo',
description: 'Get user profile information',
action: 'Get user profile information',
},
{
name: 'Search Users',
value: 'searchUsers',
description: 'Search for users by username',
action: 'Search for users by username',
},
{
name: 'Get Followers',
value: 'getFollowers',
description: 'Get user followers',
action: 'Get user followers',
},
{
name: 'Get Following',
value: 'getFollowing',
description: 'Get users that a user is following',
action: 'Get users that a user is following',
},
],
default: 'getUserInfo',
},
// Media Operations
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['media'],
},
},
options: [
{
name: 'Get User Media',
value: 'getUserMedia',
description: 'Get media posts from a user',
action: 'Get media posts from a user',
},
{
name: 'Get Media Info',
value: 'getMediaInfo',
description: 'Get detailed information about a media item',
action: 'Get detailed information about a media item',
},
{
name: 'Like Media',
value: 'likeMedia',
description: 'Like a media post',
action: 'Like a media post',
},
{
name: 'Unlike Media',
value: 'unlikeMedia',
description: 'Unlike a media post',
action: 'Unlike a media post',
},
],
default: 'getUserMedia',
},
// Feed Operations
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['feed'],
},
},
options: [
{
name: 'Get Timeline Feed',
value: 'getTimelineFeed',
description: 'Get posts from timeline feed',
action: 'Get posts from timeline feed',
},
],
default: 'getTimelineFeed',
},
// Common Parameters
{
displayName: 'Username',
name: 'username',
type: 'string',
displayOptions: {
show: {
resource: ['user'],
operation: ['getUserInfo', 'getFollowers', 'getFollowing'],
},
},
default: '',
description: 'Instagram username (without @)',
required: true,
},
{
displayName: 'Username',
name: 'username',
type: 'string',
displayOptions: {
show: {
resource: ['media'],
operation: ['getUserMedia'],
},
},
default: '',
description: 'Instagram username (without @)',
required: true,
},
{
displayName: 'Search Query',
name: 'query',
type: 'string',
displayOptions: {
show: {
resource: ['user'],
operation: ['searchUsers'],
},
},
default: '',
description: 'Search term for finding users',
required: true,
},
{
displayName: 'Media ID',
name: 'mediaId',
type: 'string',
displayOptions: {
show: {
resource: ['media'],
operation: ['getMediaInfo', 'likeMedia', 'unlikeMedia'],
},
},
default: '',
description: 'Instagram media ID',
required: true,
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: ['media', 'user', 'feed'],
operation: ['getUserMedia', 'getFollowers', 'getFollowing', 'getTimelineFeed'],
},
},
typeOptions: {
minValue: 1,
maxValue: 100,
},
default: 20,
description: 'Maximum number of items to return',
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
// Get credentials
const credentials = await this.getCredentials('instagramApi');
// Initialize Instagram client
const client = new client_1.InstagramClient(credentials);
// Try to use saved session first, then fallback to credentials
try {
if (credentials.sessionData && credentials.sessionData.trim()) {
console.log('Attempting to use saved session...');
await client.loadSession(credentials.sessionData);
}
else {
console.log('No session data found, using username/password authentication...');
await client.authenticateWithRetry(credentials, 2);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Instagram authentication failed: ${error instanceof Error ? error.message : 'Unknown error'}. Please check your credentials and try again.`, { itemIndex: 0 });
}
for (let i = 0; i < items.length; i++) {
try {
let responseData;
if (resource === 'user') {
if (operation === 'getUserInfo') {
const username = this.getNodeParameter('username', i);
responseData = await client.getUserInfo(username);
}
else if (operation === 'searchUsers') {
const query = this.getNodeParameter('query', i);
responseData = await client.searchUsers(query);
}
else if (operation === 'getFollowers') {
const username = this.getNodeParameter('username', i);
const limit = this.getNodeParameter('limit', i, 20);
// First get user info to get the user ID
const userInfo = await client.getUserInfo(username);
responseData = await client.getFollowers(userInfo.pk, limit);
}
else if (operation === 'getFollowing') {
const username = this.getNodeParameter('username', i);
const limit = this.getNodeParameter('limit', i, 20);
// First get user info to get the user ID
const userInfo = await client.getUserInfo(username);
responseData = await client.getFollowing(userInfo.pk, limit);
}
}
else if (resource === 'media') {
if (operation === 'getUserMedia') {
const username = this.getNodeParameter('username', i);
const limit = this.getNodeParameter('limit', i, 20);
// First get user info to get the user ID
const userInfo = await client.getUserInfo(username);
responseData = await client.getUserMedia(userInfo.pk, limit);
}
else if (operation === 'getMediaInfo') {
const mediaId = this.getNodeParameter('mediaId', i);
responseData = await client.getMediaInfo(mediaId);
}
else if (operation === 'likeMedia') {
const mediaId = this.getNodeParameter('mediaId', i);
await client.likeMedia(mediaId);
responseData = { success: true };
}
else if (operation === 'unlikeMedia') {
const mediaId = this.getNodeParameter('mediaId', i);
await client.unlikeMedia(mediaId);
responseData = { success: true };
}
}
else if (resource === 'feed') {
if (operation === 'getTimelineFeed') {
const maxId = this.getNodeParameter('maxId', i, undefined);
responseData = await client.getTimelineFeed(maxId);
}
}
if (responseData === undefined) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The operation "${operation}" is not supported for resource "${resource}"`, { itemIndex: i });
}
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), {
itemData: { item: i },
});
returnData.push(...executionData);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: errorMessage }), { itemData: { item: i } });
returnData.push(...executionErrorData);
continue;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
}
}
return [returnData];
}
}
exports.Instagram = Instagram;