n8n-nodes-bandi-youtube-transcript
Version:
유튜브 자막 + 기타 유튜브 정보를 가져오는 n8n 커뮤니티 노드 패키지
157 lines • 6.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.YoutubeTranscriptNode = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const youtubei_1 = require("youtubei");
class YoutubeTranscriptNode {
constructor() {
this.description = {
displayName: 'Youtube Transcript',
name: 'youtubeTranscriptNode',
icon: 'file:youTube.png',
group: ['transform'],
version: 1,
description: 'Get Transcript of a youtube video',
defaults: {
name: 'Youtube Transcript',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Youtube Video ID or Url',
name: 'youtubeId',
type: 'string',
default: '',
placeholder: 'Youtube Video ID or Url',
},
{
displayName: 'Preferred Caption Language',
name: 'preferCapLang',
type: 'string',
default: 'en',
placeholder: 'en, ko, jp...',
},
{
displayName: 'Return Channel ID',
name: 'returnChannelId',
type: 'boolean',
default: false,
},
{
displayName: 'Return Channel Name',
name: 'returnChannelName',
type: 'boolean',
default: false,
},
{
displayName: 'Return Title',
name: 'returnTitle',
type: 'boolean',
default: false,
},
],
};
}
async execute() {
var _a, _b, _c;
const items = this.getInputData();
const returnData = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const youtubeIdOrUrl = this.getNodeParameter('youtubeId', itemIndex, '');
const preferCapLang = this.getNodeParameter('preferCapLang', itemIndex, 'en');
const returnChannelId = this.getNodeParameter('returnChannelId', itemIndex, false);
const returnChannelName = this.getNodeParameter('returnChannelName', itemIndex, false);
const returnTitle = this.getNodeParameter('returnTitle', itemIndex, false);
let youtubeId = youtubeIdOrUrl;
const urlRegex = /^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+/;
if (urlRegex.test(youtubeId)) {
const url = new URL(youtubeId);
if (url.hostname === 'youtu.be') {
youtubeId = url.pathname.slice(1);
}
else {
const v = url.searchParams.get('v');
if (!v) {
throw new n8n_workflow_1.ApplicationError(`The provided URL doesn't contain a valid YouTube video identifier. URL: ${youtubeId}`);
}
youtubeId = v;
}
}
const youtube = new youtubei_1.Client();
const videoInfo = await youtube.getVideo(youtubeId);
if (!videoInfo) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Failed to retrieve video information', {
itemIndex,
});
}
const availableCaptionLanguages = (_a = videoInfo.captions) === null || _a === void 0 ? void 0 : _a.languages;
let text = '';
if (videoInfo.captions &&
availableCaptionLanguages &&
availableCaptionLanguages.length > 0) {
try {
let targetLangCode = undefined;
if (availableCaptionLanguages.some((lang) => lang.code === preferCapLang)) {
targetLangCode = preferCapLang;
}
else if (availableCaptionLanguages.some((lang) => lang.code === 'en')) {
targetLangCode = 'en';
}
else {
targetLangCode = availableCaptionLanguages[0].code;
}
if (targetLangCode) {
const captions = await videoInfo.captions.get(targetLangCode);
if (captions && captions.length > 0) {
text = captions.map((caption) => caption.text).join(' ');
}
}
}
catch (captionError) {
console.error(`[Youtubei] Failed to extract transcript for language: ${captionError.message}`);
}
}
const output = {
youtubeId: youtubeId,
};
if (text) {
let cleanedTranscript = text.trim();
cleanedTranscript = cleanedTranscript.replace(/\s+/g, ' ');
cleanedTranscript = cleanedTranscript.replace(/,/g, '');
cleanedTranscript = cleanedTranscript.replace(/\n/g, ' ');
cleanedTranscript = cleanedTranscript.replace(/\s+/g, ' ');
output.transcript = cleanedTranscript;
}
if (returnChannelId)
output.channelId = (_b = videoInfo.channel) === null || _b === void 0 ? void 0 : _b.id;
if (returnChannelName)
output.channelName = (_c = videoInfo.channel) === null || _c === void 0 ? void 0 : _c.name;
if (returnTitle)
output.title = videoInfo.title;
returnData.push({
json: output,
pairedItem: { item: itemIndex },
});
}
catch (error) {
if (this.continueOnFail()) {
items.push({ json: this.getInputData(itemIndex)[0].json, error, pairedItem: itemIndex });
}
else {
if (error.context) {
error.context.itemIndex = itemIndex;
throw error;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, {
itemIndex,
});
}
}
}
return [returnData];
}
}
exports.YoutubeTranscriptNode = YoutubeTranscriptNode;
//# sourceMappingURL=YoutubeTranscriptNode.node.js.map