feat(route/infzm): Support hot articles list. (#16454)

* feat(route/infzm): Support hot articles list.

* Update utils.ts

* Update hot.ts

* Update hot.ts
This commit is contained in:
Andvari 2024-08-17 22:43:37 +08:00 committed by GitHub
parent 9b089d26ee
commit dff8192729
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 76 additions and 30 deletions

39
lib/routes/infzm/hot.ts Normal file
View File

@ -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<Data> {
const link = 'https://www.infzm.com/';
const { data } = await got<ContentsResponse>({
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[],
};
}

View File

@ -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<Data> {
const id = ctx.req.param('id');
@ -40,31 +37,7 @@ async function handler(ctx): Promise<Data> {
},
});
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<string>({
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}`,

34
lib/routes/infzm/utils.ts Normal file
View File

@ -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<string>({
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,
};
});
})
);
}