diff --git a/lib/router.js b/lib/router.js index 0661dad13..0c89d215e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -34,9 +34,6 @@ router.get('/disqus/posts/:forum', lazyloadRouteHandler('./routes/disqus/posts') // 极客时间 router.get('/geektime/column/:cid', lazyloadRouteHandler('./routes/geektime/column')); -// 南方周末 -router.get('/infzm/:id', lazyloadRouteHandler('./routes/infzm/news')); - // Dribbble // router.get('/dribbble/popular/:timeframe?', lazyloadRouteHandler('./routes/dribbble/popular')); // router.get('/dribbble/user/:name', lazyloadRouteHandler('./routes/dribbble/user')); diff --git a/lib/routes-deprecated/infzm/news.js b/lib/routes-deprecated/infzm/news.js deleted file mode 100644 index 44662081a..000000000 --- a/lib/routes-deprecated/infzm/news.js +++ /dev/null @@ -1,62 +0,0 @@ -const config = require('@/config').value; -const got = require('@/utils/got'); -const timezone = require('@/utils/timezone'); -const cheerio = require('cheerio'); - -const baseUrl = 'http://www.infzm.com/contents'; - -module.exports = async (ctx) => { - const { id } = ctx.params; - const link = `${baseUrl}?term_id=${id}`; - const response = await got({ - method: 'get', - url: `http://www.infzm.com/contents?term_id=${id}&page=1&format=json`, - headers: { - Referer: link, - }, - }); - const data = response.data.data; - const resultItem = await Promise.all( - data.contents.map(async ({ id, subject, author, publish_time }) => { - // the timezone is GMT+8 - const date = timezone(publish_time, +8); - const link = `http://www.infzm.com/contents/${id}`; - let description = ''; - - const key = `infzm: ${link}`; - const value = await ctx.cache.get(key); - - if (value) { - description = value; - } else { - const cookie = config.infzm.cookie; - const response = await got({ - method: 'get', - url: `http://www.infzm.com/content/${id}`, - headers: { - Referer: link, - Cookie: cookie || `passport_session=${Math.floor(Math.random() * 100)};`, - }, - }); - const $ = cheerio.load(response.data); - description = $('div.nfzm-content__content').html(); - - ctx.cache.set(key, description); - } - - return { - title: subject, - description, - pubDate: date.toUTCString(), - link, - author, - }; - }) - ); - - ctx.state.data = { - title: `南方周末-${data.current_term.title}`, - link, - item: resultItem, - }; -}; diff --git a/lib/routes/infzm/index.ts b/lib/routes/infzm/index.ts new file mode 100644 index 000000000..2b98371ec --- /dev/null +++ b/lib/routes/infzm/index.ts @@ -0,0 +1,75 @@ +import type { Data, DataItem, Route } from '@/types'; +import type { ContentsResponse } from './types'; +import { config } from '@/config'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import timezone from '@/utils/timezone'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/:id', + parameters: { id: '南方周末频道 id, 可在该频道的 URL 中找到(即 https://www.infzm.com/contents?term_id=:id)' }, + categories: ['traditional-media'], + example: '/infzm/1', + radar: [ + { + source: ['infzm.com/contents'], + target: (_, url) => (url ? `/infzm/${url.match(/contents\?term_id=(\d*)/)?.[1]}` : ''), + }, + ], + name: '频道', + maintainers: ['KarasuShin', 'ranpox', 'xyqfer'], + handler, + description: `下面给出部分参考: + + | 推荐 | 新闻 | 观点 | 文化 | 人物 | 影像 | 专题 | 生活 | 视频 | + | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | + | 1 | 2 | 3 | 4 | 7 | 8 | 6 | 5 | 131 |`, +}; + +const baseUrl = 'https://www.infzm.com/contents'; + +async function handler(ctx): Promise { + const id = ctx.req.param('id'); + const link = `${baseUrl}?term_id=${id}`; + const { data } = await got({ + method: 'get', + url: `${baseUrl}?term_id=${id}&page=1&format=json`, + headers: { + Referer: link, + }, + }); + + const resultItem = await Promise.all( + data.data.contents.map(({ id, subject, author, publish_time }) => { + const link = `${baseUrl}/${id}`; + + return cache.tryGet(link, async () => { + const cookie = config.infzm.cookie; + const response = await got.get({ + method: 'get', + url: link, + headers: { + Referer: link, + Cookie: cookie || `passport_session=${Math.floor(Math.random() * 100)};`, + }, + }); + const $ = load(response.data); + return { + title: subject, + description: $('div.nfzm-content__content').html() ?? '', + pubDate: timezone(publish_time, +8).toUTCString(), + link, + author, + }; + }); + }) + ); + + return { + title: `南方周末-${data.data.current_term.title}`, + link, + image: 'https://www.infzm.com/favicon.ico', + item: resultItem as DataItem[], + }; +} diff --git a/lib/routes/infzm/types.ts b/lib/routes/infzm/types.ts new file mode 100644 index 000000000..1f6ba32da --- /dev/null +++ b/lib/routes/infzm/types.ts @@ -0,0 +1,17 @@ +export interface ContentsResponse { + code: number; + msg: string; + data: { + contents: { + id: number; + subject: string; + author: string; + publish_time: string; + }[]; + current_term: { + id: number; + title: string; + type: string; + }; + }; +}