fix(route): 南方周末 (#14886)

* fix(route): 南方周末

* update image

* update radar rules

* remove the deprecated content

* Update lib/routes/infzm/index.ts

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

* Update lib/routes/infzm/index.ts

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

* use cache.tryGet

---------
This commit is contained in:
karasu 2024-03-21 23:45:34 +08:00 committed by GitHub
parent fe79896615
commit 3351e51f3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 92 additions and 65 deletions

View File

@ -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'));

View File

@ -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,
};
};

75
lib/routes/infzm/index.ts Normal file
View File

@ -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<Data> {
const id = ctx.req.param('id');
const link = `${baseUrl}?term_id=${id}`;
const { data } = await got<ContentsResponse>({
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<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,
};
});
})
);
return {
title: `南方周末-${data.data.current_term.title}`,
link,
image: 'https://www.infzm.com/favicon.ico',
item: resultItem as DataItem[],
};
}

17
lib/routes/infzm/types.ts Normal file
View File

@ -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;
};
};
}