From dff81927298c5bb036bf33f051f93fd377a7fbb2 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Sat, 17 Aug 2024 22:43:37 +0800 Subject: [PATCH] feat(route/infzm): Support hot articles list. (#16454) * feat(route/infzm): Support hot articles list. * Update utils.ts * Update hot.ts * Update hot.ts --- lib/routes/infzm/hot.ts | 39 +++++++++++++++++++++++++++++++++++++++ lib/routes/infzm/index.ts | 33 +++------------------------------ lib/routes/infzm/utils.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 lib/routes/infzm/hot.ts create mode 100644 lib/routes/infzm/utils.ts diff --git a/lib/routes/infzm/hot.ts b/lib/routes/infzm/hot.ts new file mode 100644 index 000000000..5801f9c6e --- /dev/null +++ b/lib/routes/infzm/hot.ts @@ -0,0 +1,39 @@ +import type { Data, DataItem, Route } from '@/types'; +import type { ContentsResponse } from './types'; +import got from '@/utils/got'; +import { fetchArticles } from './utils'; + +export const route: Route = { + path: '/hot', + parameters: {}, + categories: ['traditional-media'], + example: '/infzm/hot', + radar: [ + { + source: ['infzm.com/'], + }, + ], + name: '热门文章', + maintainers: ['KarasuShin', 'ranpox', 'xyqfer'], + handler, +}; + +async function handler(): Promise { + const link = 'https://www.infzm.com/'; + const { data } = await got({ + method: 'get', + url: `https://www.infzm.com/hot_contents`, + headers: { + Referer: link, + }, + }); + + const resultItem = await fetchArticles(data.data.hot_contents); + + return { + title: `南方周末-热门文章`, + link, + image: 'https://www.infzm.com/favicon.ico', + item: resultItem as DataItem[], + }; +} diff --git a/lib/routes/infzm/index.ts b/lib/routes/infzm/index.ts index 2b98371ec..27c76a468 100644 --- a/lib/routes/infzm/index.ts +++ b/lib/routes/infzm/index.ts @@ -1,10 +1,7 @@ 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'; +import { fetchArticles } from './utils'; export const route: Route = { path: '/:id', @@ -27,7 +24,7 @@ export const route: Route = { | 1 | 2 | 3 | 4 | 7 | 8 | 6 | 5 | 131 |`, }; -const baseUrl = 'https://www.infzm.com/contents'; +export const baseUrl = 'https://www.infzm.com/contents'; async function handler(ctx): Promise { const id = ctx.req.param('id'); @@ -40,31 +37,7 @@ async function handler(ctx): Promise { }, }); - 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, - }; - }); - }) - ); + const resultItem = await fetchArticles(data.data.contents); return { title: `南方周末-${data.data.current_term.title}`, diff --git a/lib/routes/infzm/utils.ts b/lib/routes/infzm/utils.ts new file mode 100644 index 000000000..83ca75389 --- /dev/null +++ b/lib/routes/infzm/utils.ts @@ -0,0 +1,34 @@ +import { config } from '@/config'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import timezone from '@/utils/timezone'; +import { load } from 'cheerio'; +import { baseUrl } from '.'; + +export async function fetchArticles(data) { + return await Promise.all( + data.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, + }; + }); + }) + ); +}