n8n-nodes-vidfetch
Version:
This node allows the user to make an API call to Vidfetch.
212 lines • 9.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vidfetch = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const tools_1 = require("@langchain/core/tools");
const zod_1 = require("zod");
class Vidfetch {
constructor() {
this.description = {
displayName: 'Vidfetch',
name: 'n8n-nodes-vidfetch',
icon: 'file:vidfetch.svg',
group: ['transform'],
version: 1,
description: 'Makes API calls to the Vidfetch service',
defaults: {
name: 'Vidfetch',
color: '#772244',
},
inputs: ['main'],
outputs: ['main'],
usableAsTool: true,
credentials: [
{
name: 'vidfetchApi',
required: true,
},
],
properties: [
{
displayName: 'Video URL',
name: 'url',
type: 'string',
default: '',
description: 'The URL of the video you want to download',
required: true,
},
{
displayName: 'Download Type',
name: 'outputType',
type: 'options',
options: [
{ name: 'Video', value: 'video' },
{ name: 'Audio', value: 'audio' },
],
default: 'video',
description: 'Choose the download type',
},
{
displayName: 'Video Quality',
name: 'quality',
type: 'options',
displayOptions: {
show: {
outputType: ['video'],
},
},
options: [
{ name: '1080p', value: '1080p' },
{ name: '720p', value: '720p' },
{ name: '480p', value: '480p' },
],
default: '720p',
description: 'Choose the video quality',
},
],
};
}
async execute() {
var _a;
const returnData = [];
try {
const isCalledByAiAgent = (_a = this.getNode().parameters) === null || _a === void 0 ? void 0 : _a.isCalledByAiAgent;
if (isCalledByAiAgent) {
const paramSchema = zod_1.z.object({
url: zod_1.z.string().describe('The URL of the video you want to download'),
outputType: zod_1.z.enum(['video', 'audio']).describe('Choose the download type (video or audio)'),
quality: zod_1.z.enum(['1080p', '720p', '480p']).optional().describe('Choose the video quality (only for video downloads)'),
});
const vidfetchTool = new tools_1.DynamicStructuredTool({
name: 'vidfetch',
description: 'Download videos or extract audio from online videos',
schema: paramSchema,
func: async (params) => {
try {
const { url, outputType, quality } = params;
const credentials = await this.getCredentials('vidfetchApi');
const apiUrl = credentials.apiUrl;
const apiToken = credentials.apiToken;
let body = {
url,
output_type: outputType,
};
if (outputType === 'video' && quality) {
body.quality = quality;
}
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${apiToken}`,
};
let endpoint = apiUrl;
if (!endpoint.endsWith('/download')) {
endpoint = endpoint.endsWith('/') ? `${endpoint}download` : `${endpoint}/download`;
}
const options = {
method: 'POST',
body: JSON.stringify(body),
headers,
uri: endpoint,
};
const response = await this.helpers.request(options);
const responseData = typeof response === 'string' ? JSON.parse(response) : response;
return JSON.stringify({
download_url: responseData.download_url,
title: responseData.title,
duration: responseData.duration,
thumbnail: responseData.thumbnail,
});
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Error processing Vidfetch request: ${error.message}`);
}
else {
throw new Error('An unknown error occurred while processing the Vidfetch request');
}
}
},
});
returnData.push({
json: {
tool: vidfetchTool,
},
});
}
else {
const credentials = await this.getCredentials('vidfetchApi');
const apiUrl = credentials.apiUrl;
const apiToken = credentials.apiToken;
const url = this.getNodeParameter('url', 0);
const outputType = this.getNodeParameter('outputType', 0);
let body = {
url,
output_type: outputType,
};
if (outputType === 'video') {
const quality = this.getNodeParameter('quality', 0);
body.quality = quality;
}
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${apiToken}`,
};
let endpoint = apiUrl;
if (!endpoint.endsWith('/download')) {
endpoint = endpoint.endsWith('/') ? `${endpoint}download` : `${endpoint}/download`;
}
const options = {
method: 'POST',
body: JSON.stringify(body),
headers,
uri: endpoint,
};
try {
const response = await this.helpers.request(options);
let responseData;
try {
responseData = typeof response === 'string' ? JSON.parse(response) : response;
}
catch (parseError) {
throw new Error(`Error parsing API response: ${parseError.message}`);
}
returnData.push({
json: {
download_url: responseData.download_url,
title: responseData.title,
duration: responseData.duration,
thumbnail: responseData.thumbnail,
},
});
}
catch (error) {
if (error.response) {
throw new Error(`Error processing Vidfetch request: ${error.response.statusCode} - ${JSON.stringify(error.response.body)}`);
}
else if (error.request) {
throw new Error(`Error processing Vidfetch request: No response received from server`);
}
else if (error instanceof Error) {
throw new Error(`Error processing Vidfetch request: ${error.message}`);
}
else {
throw new Error('An unknown error occurred while processing the Vidfetch request');
}
}
}
}
catch (error) {
if (error instanceof Error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error.message);
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'An unknown error occurred');
}
}
return [returnData];
}
}
exports.Vidfetch = Vidfetch;
//# sourceMappingURL=Vidfetch.node.js.map