Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

4.6 KB
Raw Blame
import {
  DedupeStrategy,
  httpClient,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import {
  AppConnectionValueForAuthProperty,
  PieceAuth,
  Store,
  TriggerStrategy,
  createTrigger,
} from '@activepieces/pieces-framework';
import { rssFeedUrl } from '../common/props';
import FeedParser from 'feedparser';
import { isNil } from '@activepieces/pieces-framework';
import dayjs from 'dayjs';
import { getId } from '../common/getId';
import { sampleData } from '../common/sampleData';
import { newItemTriggerOutputSchema } from '../output-schemas';

export const rssNewItemTrigger = createTrigger({
  name: 'new-item',
  classification: 'READ',
  displayName: 'New Item in Feed',
  description: 'Runs when a new item is published in an RSS feed.',
  aiMetadata: {
    description:
      'Fires when a new entry is published in a single monitored RSS/Atom feed. Polls the configured feed URL and emits each newly detected item (deduplicated by item id and publish date), representing a freshly published article, post, or update from that feed.',
  },
  type: TriggerStrategy.POLLING,
  sampleData: sampleData,
  auth: PieceAuth.None(),
  props: {
    rss_feed_url: rssFeedUrl,
  },
  outputSchema: newItemTriggerOutputSchema,
  async test(context): Promise<unknown[]> {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context): Promise<void> {
    await pollingHelper.onEnable(polling, context);
  },

  async onDisable(context): Promise<void> {
    if (context.isRepublish) {
      return;
    }
    const { store } = context;
    const lastFetchDate = await store.get<number>('_lastRssPublishDate');
    if (!isNil(lastFetchDate)) {
      await store.delete('_lastRssPublishDate');
    }
    await pollingHelper.onDisable(polling, context);
  },

  async run(context): Promise<unknown[]> {
    const { store } = context;
    const lastFetchDate = await store.get<number>('_lastRssPublishDate');
    const newItems = (
      await pollingHelper.poll(polling, context)
    ).filter((f) => {
      if (isNil(lastFetchDate)) {
        return true;
      }
      const newItem = f as { pubdate: string; pubDate: string };
      const newDate = newItem.pubdate ?? newItem.pubDate;
      if (isNil(newDate)) {
        return true;
      }
      return dayjs(newDate).unix() > lastFetchDate;
    });
    let newFetchDateUnix = lastFetchDate;
    for (const item of newItems) {
      const newItem = item as { pubdate: string; pubDate: string };
      const newDate = newItem.pubdate ?? newItem.pubDate;
      if (!isNil(newDate)) {
        const newDateUnix = dayjs(newDate).unix();
        if (isNil(newFetchDateUnix) || newDateUnix > newFetchDateUnix) {
          newFetchDateUnix = newDateUnix;
        }
      }
    }
    if (!isNil(newFetchDateUnix)) {
      await store.put('_lastRssPublishDate', newFetchDateUnix);
    }
    return newItems.sort((a, b) => {
      const aDate =
        (a as { pubdate: string; pubDate: string }).pubdate ??
        (a as { pubdate: string; pubDate: string }).pubDate;
      const bDate =
        (b as { pubdate: string; pubDate: string }).pubdate ??
        (b as { pubdate: string; pubDate: string }).pubDate;
      if (aDate && bDate) {
        const aUnix = dayjs(aDate).unix();
        const bUnix = dayjs(bDate).unix();
        if (aUnix === bUnix) {
          return newItems.indexOf(a) - newItems.indexOf(b);
        } else {
          return bUnix - aUnix;
        }
      } else {
        return newItems.indexOf(a) - newItems.indexOf(b);
      }
    });
  },
});

const polling: Polling<
  AppConnectionValueForAuthProperty<undefined>,
  { rss_feed_url: string }
> = {
  strategy: DedupeStrategy.LAST_ITEM,
  items: async ({
    propsValue,
  }: {
    store: Store;
    propsValue: { rss_feed_url: string };
  }) => {
    const items = await getRssItems(propsValue.rss_feed_url);
    return items.map((item) => ({
      id: getId(item),
      data: item,
    }));
  },
};

async function getRssItems(url: string): Promise<any[]> {
  const response = await httpClient.sendRequest<Buffer>({
    method: HttpMethod.GET,
    url,
    responseType: 'arraybuffer',
  });

  return new Promise((resolve, reject) => {
    const feedparser = new FeedParser({
      addmeta: true,
    });
    const items: any[] = [];

    feedparser.on('readable', () => {
      let item = feedparser.read();
      while (item) {
        items.push(item);
        item = feedparser.read();
      }
    });

    feedparser.on('end', () => {
      resolve(items);
    });

    feedparser.on('error', (error: any) => {
      reject(error);
    });

    feedparser.write(response.body);
    feedparser.end();
  });
}