Files
RSSHub/lib/routes/psyche/topic.ts
T
Enoch MaandTony ec4a03e221 feat(route): support psyche.co (#15772)
* support psyche

* Update lib/routes/psyche/topic.ts

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/routes/psyche/topic.ts

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/routes/psyche/topic.ts

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* graphql + _next/data/buildID

* add link

* delete json

* cache link

* hotfix: redundent 's'

---------
2024-06-02 13:15:26 +08:00

47 lines
1.4 KiB
TypeScript

import { Route } from '@/types';
import { load } from 'cheerio';
import { ofetch } from 'ofetch';
import { getData } from './utils';
export const route: Route = {
path: '/topic/:topic',
categories: ['new-media'],
example: '/psyche/topic/therapeia',
parameters: { topic: 'Topic' },
radar: [
{
source: ['psyche.co/:topic'],
},
],
name: 'Topics',
maintainers: ['emdoe'],
handler,
description: 'Supported categories: Therapeia, Eudaimonia, and Poiesis.',
};
async function handler(ctx) {
const url = `https://psyche.co/${ctx.req.param('topic')}`;
const response = await ofetch(url);
const $ = load(response);
const data = JSON.parse($('script#__NEXT_DATA__').text());
const articles = data.props.pageProps.articles;
const prefix = `https://psyche.co/_next/data/${data.buildId}`;
const list = Object.keys(articles).flatMap((type) =>
articles[type].edges.map((item) => ({
title: item.node.title,
link: `https://psyche.co/${type}/${item.node.slug}`,
json: `${prefix}/${type}/${item.node.slug}.json`,
}))
);
const items = await getData(list);
return {
title: `Psyche | ${data.props.pageProps.section.title}`,
link: url,
description: data.props.pageProps.section.metaDescription,
item: items,
};
}