UNPKG

n8n-nodes-supadata

Version:

Tool for extracting content from YouTube videos and web pages

293 lines 12.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Supadata = void 0; const GenericFunctions_1 = require("./GenericFunctions"); class Supadata { constructor() { this.description = { displayName: 'Supadata', name: 'supadata', icon: 'file:Supadata.svg', group: ['input'], version: 1, description: 'Access Supadata API to fetch YouTube and web data', subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', defaults: { name: 'Supadata', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'supadataApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'YouTube', value: 'youtube' }, { name: 'Web', value: 'webScrape' }, ], default: 'youtube', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['youtube'], }, }, options: [ { name: 'Get Channel', value: 'getChannel', description: 'Get details of a YouTube channel', action: 'Get channel details', }, { name: 'Get Channel Videos', value: 'getChannelVideos', description: 'Get videos of a YouTube channel', action: 'Get channel videos', }, { name: 'Get Playlist', value: 'getPlaylist', description: 'Get details of a YouTube playlist', action: 'Get playlist details', }, { name: 'Get Playlist Videos', value: 'getPlaylistVideos', description: 'Get videos of a YouTube playlist', action: 'Get playlist videos', }, { name: 'Get Transcript', value: 'getTranscript', description: 'Get the transcript of a YouTube video', action: 'Get video transcript', }, { name: 'Get Video', value: 'getVideo', description: 'Get details of a YouTube video', action: 'Get video details', }, ], default: 'getVideo', }, { displayName: 'Video', name: 'videoId', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['youtube'], operation: ['getVideo', 'getTranscript'], }, }, placeholder: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', description: 'The ID or URL of the YouTube video', }, { displayName: 'Return as Plain Text', name: 'text', type: 'boolean', default: false, displayOptions: { show: { resource: ['youtube'], operation: ['getTranscript'], }, }, description: 'Whether to return the transcript as plain text', }, { displayName: 'Channel', name: 'channelId', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['youtube'], operation: ['getChannel', 'getChannelVideos'], }, }, placeholder: 'https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw', description: 'The ID or URL of the YouTube channel', }, { displayName: 'Limit', name: 'limit', type: 'number', default: 50, displayOptions: { show: { resource: ['youtube'], operation: ['getChannelVideos'], }, }, typeOptions: { minValue: 1, maxValue: 5000, }, description: 'Max number of results to return', }, { displayName: 'Playlist', name: 'playlistId', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['youtube'], operation: ['getPlaylist', 'getPlaylistVideos'], }, }, placeholder: 'https://www.youtube.com/playlist?list=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc', description: 'The ID or URL of the YouTube playlist', }, { displayName: 'Limit', name: 'limit', type: 'number', default: 50, displayOptions: { show: { resource: ['youtube'], operation: ['getPlaylistVideos'], }, }, typeOptions: { minValue: 1, maxValue: 5000, }, description: 'Max number of results to return', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['webScrape'], }, }, options: [ { name: 'Scrape URL', value: 'scrapeUrl', description: 'Scrape data from a URL', action: 'Scrape data from a URL', }, ], default: 'scrapeUrl', }, { displayName: 'URL', name: 'url', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['webScrape'], operation: ['scrapeUrl'], }, }, placeholder: 'https://example.com', description: 'The URL to scrape', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const resource = this.getNodeParameter('resource', i); const operation = this.getNodeParameter('operation', i); let responseData; if (resource === 'youtube') { if (operation === 'getVideo') { const videoIdentifier = this.getNodeParameter('videoId', i); responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/youtube/video', {}, { id: videoIdentifier }); } else if (operation === 'getTranscript') { const videoInput = this.getNodeParameter('videoId', i); const qs = { text: this.getNodeParameter('text', i), }; if (videoInput.includes('youtube.com/') || videoInput.includes('youtu.be/')) { qs.url = videoInput; } else { qs.videoId = videoInput; } responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/youtube/transcript', {}, qs); } else if (operation === 'getChannel') { const channelIdentifier = this.getNodeParameter('channelId', i); responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/youtube/channel', {}, { id: channelIdentifier }); } else if (operation === 'getChannelVideos') { const channelId = this.getNodeParameter('channelId', i); const qs = { id: channelId, limit: this.getNodeParameter('limit', i), }; responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/youtube/channel/videos', {}, qs); responseData = responseData.videoIds; } else if (operation === 'getPlaylist') { const playlistIdentifier = this.getNodeParameter('playlistId', i); responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/youtube/playlist', {}, { id: playlistIdentifier }); } else if (operation === 'getPlaylistVideos') { const playlistIdentifier = this.getNodeParameter('playlistId', i); const qs = { id: playlistIdentifier, limit: this.getNodeParameter('limit', i), }; responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/youtube/playlist/videos', {}, qs); responseData = responseData.videoIds; } } else if (resource === 'webScrape') { if (operation === 'scrapeUrl') { const url = this.getNodeParameter('url', i); responseData = await GenericFunctions_1.supadataApiRequest.call(this, 'GET', '/web/scrape', {}, { url }); } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionData); continue; } throw error; } } return [returnData]; } } exports.Supadata = Supadata; //# sourceMappingURL=Supadata.node.js.map