2023-10-19 00:49:18 -07:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
IPollFunctions,
|
|
|
|
} from 'n8n-workflow';
|
2024-08-29 06:55:53 -07:00
|
|
|
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
|
2023-10-19 00:49:18 -07:00
|
|
|
import Parser from 'rss-parser';
|
2024-01-15 06:45:33 -08:00
|
|
|
import moment from 'moment-timezone';
|
2023-10-19 00:49:18 -07:00
|
|
|
|
2024-09-17 06:09:35 -07:00
|
|
|
interface PollData {
|
|
|
|
lastItemDate?: string;
|
|
|
|
lastTimeChecked?: string;
|
|
|
|
}
|
|
|
|
|
2023-10-19 00:49:18 -07:00
|
|
|
export class RssFeedReadTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'RSS Feed Trigger',
|
|
|
|
name: 'rssFeedReadTrigger',
|
|
|
|
icon: 'fa:rss',
|
2024-06-26 00:35:59 -07:00
|
|
|
iconColor: 'orange-red',
|
2023-10-19 00:49:18 -07:00
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Starts a workflow when an RSS feed is updated',
|
|
|
|
subtitle: '={{$parameter["event"]}}',
|
|
|
|
defaults: {
|
|
|
|
name: 'RSS Feed Trigger',
|
|
|
|
color: '#b02020',
|
|
|
|
},
|
|
|
|
polling: true,
|
|
|
|
inputs: [],
|
2024-08-29 06:55:53 -07:00
|
|
|
outputs: [NodeConnectionType.Main],
|
2023-10-19 00:49:18 -07:00
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Feed URL',
|
|
|
|
name: 'feedUrl',
|
|
|
|
type: 'string',
|
|
|
|
default: 'https://blog.n8n.io/rss/',
|
|
|
|
required: true,
|
|
|
|
description: 'URL of the RSS feed to poll',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
|
2024-09-17 06:09:35 -07:00
|
|
|
const pollData = this.getWorkflowStaticData('node') as PollData;
|
2023-10-19 00:49:18 -07:00
|
|
|
const feedUrl = this.getNodeParameter('feedUrl') as string;
|
|
|
|
|
2024-09-17 06:09:35 -07:00
|
|
|
const dateToCheck = Date.parse(
|
|
|
|
pollData.lastItemDate ?? pollData.lastTimeChecked ?? moment().utc().format(),
|
|
|
|
);
|
2023-10-19 00:49:18 -07:00
|
|
|
|
|
|
|
if (!feedUrl) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'The parameter "URL" has to be set!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser = new Parser();
|
|
|
|
|
|
|
|
let feed: Parser.Output<IDataObject>;
|
|
|
|
try {
|
|
|
|
feed = await parser.parseURL(feedUrl);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ECONNREFUSED') {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`It was not possible to connect to the URL. Please make sure the URL "${feedUrl}" it is valid!`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NodeOperationError(this.getNode(), error as Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
if (feed.items) {
|
|
|
|
if (this.getMode() === 'manual') {
|
|
|
|
return [this.helpers.returnJsonArray(feed.items[0])];
|
|
|
|
}
|
|
|
|
feed.items.forEach((item) => {
|
2024-09-17 06:09:35 -07:00
|
|
|
if (Date.parse(item.isoDate as string) > dateToCheck) {
|
2023-10-19 00:49:18 -07:00
|
|
|
returnData.push(item);
|
|
|
|
}
|
|
|
|
});
|
2024-09-17 06:09:35 -07:00
|
|
|
|
|
|
|
if (feed.items.length) {
|
|
|
|
const maxIsoDate = Math.max(...feed.items.map(({ isoDate }) => Date.parse(isoDate!)));
|
|
|
|
pollData.lastItemDate = new Date(maxIsoDate).toISOString();
|
|
|
|
}
|
2023-10-19 00:49:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(returnData) && returnData.length !== 0) {
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|