UNPKG

n8n-nodes-kih-youtube-transcript

Version:

n8n community node to fetch YouTube video transcripts using KI-H API

186 lines (185 loc) 7.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KihYoutubeTranscript = void 0; const n8n_workflow_1 = require("n8n-workflow"); class KihYoutubeTranscript { constructor() { this.description = { displayName: 'KIH YouTube Transcript', name: 'kihYoutubeTranscript', icon: 'file:youtube.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Fetch transcripts from YouTube videos', defaults: { name: 'KIH YouTube Transcript', }, inputs: ['main'], outputs: ['main'], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Get Transcript', value: 'getTranscript', description: 'Get the transcript of a YouTube video', action: 'Get the transcript of a YouTube video', }, ], default: 'getTranscript', }, { displayName: 'YouTube URL', name: 'url', type: 'string', default: '', required: true, displayOptions: { show: { operation: ['getTranscript'], }, }, placeholder: 'https://www.youtube.com/watch?v=VIDEO_ID', description: 'The URL of the YouTube video', }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Option', default: {}, displayOptions: { show: { operation: ['getTranscript'], }, }, options: [ { displayName: 'Output Format', name: 'outputFormat', type: 'options', default: 'fullData', options: [ { name: 'Full Data', value: 'fullData', description: 'Return transcript with timestamps and metadata', }, { name: 'Text Only', value: 'textOnly', description: 'Return only the combined text without timestamps', }, { name: 'Formatted', value: 'formatted', description: 'Return formatted text with timestamps', }, ], }, { displayName: 'Language', name: 'lang', type: 'string', default: '', placeholder: 'en, de, fr...', description: 'Language code for the transcript (if available)', }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < items.length; i++) { try { if (operation === 'getTranscript') { const url = this.getNodeParameter('url', i); const options = this.getNodeParameter('options', i, {}); // Make request to KI-H API const requestOptions = { method: 'POST', url: 'https://api.ki-h.net/api/youtube-transcript', body: { url: url, }, json: true, }; try { const response = await this.helpers.httpRequest(requestOptions); if (!response.success) { throw new Error(response.error || 'Failed to fetch transcript'); } // Format output based on selected format let output = {}; switch (options.outputFormat) { case 'textOnly': output = { text: response.fullText, videoId: response.videoId, url, }; break; case 'formatted': output = { formattedText: response.transcript .map((item) => { const time = formatTime(item.start || 0); return `[${time}] ${item.text}`; }) .join('\n'), videoId: response.videoId, url, }; break; case 'fullData': default: output = { transcript: response.transcript, fullText: response.fullText, videoId: response.videoId, url, totalItems: response.totalItems, }; break; } returnData.push({ json: output, pairedItem: { item: i }, }); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to fetch transcript: ${error.message}`, { itemIndex: i }); } } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error instanceof Error ? error.message : 'Unknown error', }, pairedItem: { item: i }, }); continue; } throw error; } } return [returnData]; } } exports.KihYoutubeTranscript = KihYoutubeTranscript; function formatTime(seconds) { const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins}:${secs.toString().padStart(2, '0')}`; }