diff --git a/lib/router.js b/lib/router.js index 708cadd58..9873875f7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1002,9 +1002,6 @@ router.get('/haohaozhu/discover/:keyword?', lazyloadRouteHandler('./routes/haoha router.get('/magireco/announcements', lazyloadRouteHandler('./routes/magireco/announcements')); router.get('/magireco/event_banner', lazyloadRouteHandler('./routes/magireco/event-banner')); -// 我有一片芝麻地 -router.get('/blogs/hedwig/:type', lazyloadRouteHandler('./routes/blogs/hedwig')); - // 拉勾 router.get('/lagou/jobs/:position/:city', lazyloadRouteHandler('./routes/lagou/jobs')); diff --git a/lib/routes-deprecated/blogs/hedwig.js b/lib/routes-deprecated/blogs/hedwig.js deleted file mode 100644 index 6a26fe48f..000000000 --- a/lib/routes-deprecated/blogs/hedwig.js +++ /dev/null @@ -1,42 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const md = require('markdown-it')({ - html: true, -}); -const dayjs = require('dayjs'); -const { isValidHost } = require('@/utils/valid-host'); - -module.exports = async (ctx) => { - const type = ctx.params.type; - if (!isValidHost(type)) { - throw new Error('Invalid type'); - } - - const url = `https://${type}.hedwig.pub`; - const res = await got({ - method: 'get', - url, - }); - const $ = cheerio.load(res.data); - const content = JSON.parse($('#__NEXT_DATA__')[0].children[0].data); - const title = $('title').text(); - - const list = content.props.pageProps.issuesByNewsletter.map((item) => { - let description = ''; - for (const block of item.blocks) { - description += md.render(block.markdown.text); - } - return { - title: item.subject, - description, - pubDate: dayjs(`${item.publishAt} +0800`, "YYYY-MM-DD'T'HH:mm:ss.SSS'Z'").toString(), - link: `https://${type}.hedwig.pub/i/${item.urlFriendlyName}`, - }; - }); - ctx.state.data = { - title: `Ⓙ Hedwig - ${title}`, - description: content.props.pageProps.newsletter.about, - link: `https://${type}.hedwig.pub`, - item: list, - }; -}; diff --git a/lib/routes/hedwig/namespace.ts b/lib/routes/hedwig/namespace.ts new file mode 100644 index 000000000..2379f7442 --- /dev/null +++ b/lib/routes/hedwig/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Hedwig', + url: 'hedwig.pub', + lang: 'zh-CN', +}; diff --git a/lib/routes/hedwig/posts.ts b/lib/routes/hedwig/posts.ts new file mode 100644 index 000000000..bfdc8b9ad --- /dev/null +++ b/lib/routes/hedwig/posts.ts @@ -0,0 +1,60 @@ +import { Route, ViewType } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; +import { isValidHost } from '@/utils/valid-host'; +import InvalidParameterError from '@/errors/types/invalid-parameter'; + +import MarkdownIt from 'markdown-it'; +const md = MarkdownIt({ + html: true, +}); + +export const route: Route = { + path: '/posts/:site', + categories: ['blog'], + example: '/posts/walnut', + parameters: { site: '站点名,原则上只要是 `{site}.hedwig.pub` 都可以匹配' }, + features: { + supportRadar: false, + }, + name: 'Posts', + url: 'hedwig.pub', + maintainers: ['zwithz', 'GetToSet'], + view: ViewType.Articles, + handler: async (ctx) => { + const { site } = ctx.req.param(); + + if (!isValidHost(site)) { + throw new InvalidParameterError('Invalid site'); + } + + const baseUrl = `https://${site}.hedwig.pub`; + + const response = await ofetch(baseUrl); + const $ = load(response); + + const text = $('script#__NEXT_DATA__').text(); + const json = JSON.parse(text); + + const pageProps = json.props.pageProps; + + const list = pageProps.issuesByNewsletter.map((item) => { + const description = item.blocks.reduce((desc, block) => desc + md.render(block.markdown.text), ''); + return { + title: item.subject, + description, + pubDate: timezone(parseDate(item.publishAt, 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'), +0), + link: `${baseUrl}/i/${item.urlFriendlyName}`, + }; + }); + + return { + title: pageProps.newsletter.name, + description: pageProps.newsletter.about, + link: baseUrl, + item: list, + }; + }, +};