Skip to content

Commit

Permalink
feat(route/substack): add substack subscription by official rss
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Jan 4, 2025
1 parent a1d8565 commit 384712d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/routes/substack/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Substack',
url: 'substack.com',
lang: 'en',
};
48 changes: 48 additions & 0 deletions lib/routes/substack/subscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Route, ViewType } from '@/types';
import parser from '@/utils/rss-parser';
import { parseDate } from '@/utils/parse-date';
import InvalidParameterError from '@/errors/types/invalid-parameter';

export const route: Route = {
path: '/subscribe/:user',
categories: ['blog'],
view: ViewType.SocialMedia,
example: '/substack/subscribe/mangoread',
parameters: { user: 'Username of the Substack' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: 'Substack Subscription',
maintainers: ['pseudoyu'],
handler,
};

async function handler(ctx) {
const user = ctx.req.param('user');

if (!user) {
throw new InvalidParameterError('Invalid user');
}

const feed = await parser.parseURL(`https://${user}.substack.com/feed`);

return {
title: feed.title ?? 'Substack',
description: feed.description ?? `${user}'s Substack`,
link: feed.link ?? `https://${user}.substack.com`,
image: feed.image?.url ?? '',
item: feed.items.map((item) => ({
title: item.title ?? 'Untitled',
description: item['content:encoded'] ?? item.content ?? '',
link: item.link ?? '',
pubDate: item.pubDate ? parseDate(item.pubDate) : undefined,
guid: item.guid ?? '',
author: item.creator ?? user,
})),
};
}

0 comments on commit 384712d

Please sign in to comment.