feat(route): add 36kr资讯热榜 (#12681)

* feat(route): add 36kr资讯热榜

* fix docs

* update lib/v2/36kr/utils.js
---------
This commit is contained in:
Ethan Shen 2023-06-21 08:04:50 +08:00 committed by GitHub
parent f8cf1d6e55
commit a4e2eda25a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 116 additions and 30 deletions

View File

@ -68,6 +68,16 @@ pageClass: routes
<Route author="nczitzk" example="/36kr/search/newsflashes/ofo" path="/36kr/search/newsflashes/:keyword" :paramsDesc="['关键字']" />
### 资讯热榜
<Route author="nczitzk" example="/36kr/hot-list" path="/36kr/hot-list/:category?" :paramsDesc="['分类默认为24小时热榜']">
| 24小时热榜 | 资讯人气榜 | 资讯综合榜 | 资讯收藏榜 |
| ---------- | ---------- | ---------- | ---------- |
| 24 | renqi | zonghe | shoucang |
</Route>
## 52hrtt 华人头条
### 新闻

59
lib/v2/36kr/hot-list.js Normal file
View File

@ -0,0 +1,59 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const { rootUrl, ProcessItem } = require('./utils');
const categories = {
24: {
title: '24小时热榜',
key: 'homeData.data.hotlist.data',
},
renqi: {
title: '资讯人气榜',
key: 'hotListData.topList',
},
zonghe: {
title: '资讯综合榜',
key: 'hotListData.hotList',
},
shoucang: {
title: '资讯综合榜',
key: 'hotListData.collectList',
},
};
module.exports = async (ctx) => {
const category = ctx.params.category ?? '24';
const currentUrl = category === '24' ? rootUrl : `${rootUrl}/hot-list/catalog`;
const response = await got({
method: 'get',
url: currentUrl,
});
const getProperty = (object, key) => key.split('.').reduce((o, k) => o && o[k], object);
const data = getProperty(JSON.parse(response.data.match(/window.initialState=({.*})/)[1]), categories[category].key);
let items = data
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10)
.filter((item) => item.itemType !== 0)
.map((item) => {
item = item.templateMaterial ?? item;
return {
title: item.widgetTitle.replace(/<\/?em>/g, ''),
author: item.authorName,
pubDate: parseDate(item.publishTime),
link: `${rootUrl}/p/${item.itemId}`,
description: item.summary,
};
});
items = await Promise.all(items.map((item) => ProcessItem(item, ctx.cache.tryGet)));
ctx.state.data = {
title: `36氪 - ${categories[category].title}`,
link: currentUrl,
item: items,
};
};

View File

@ -1,7 +1,8 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const CryptoJS = require('crypto-js');
const { rootUrl, ProcessItem } = require('./utils');
const shortcuts = {
'/information': '/information/web_news',
@ -15,7 +16,6 @@ const shortcuts = {
module.exports = async (ctx) => {
const path = ctx.path.replace(/^\/news(?!flashes)/, '/information').replace(/\/search\/article/, '/search/articles');
const rootUrl = 'https://www.36kr.com';
const currentUrl = `${rootUrl}${shortcuts.hasOwnProperty(path) ? shortcuts[path] : path}`;
const response = await got({
@ -42,34 +42,7 @@ module.exports = async (ctx) => {
});
if (!/^\/(search|newsflashes)/.test(path)) {
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const cipherTextList = detailResponse.data.match(/{"state":"(.*)","isEncrypt":true}/) ?? [];
if (cipherTextList.length !== 0) {
const key = CryptoJS.enc.Utf8.parse('efabccee-b754-4c');
const content = JSON.parse(
CryptoJS.AES.decrypt(cipherTextList[1], key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
})
.toString(CryptoJS.enc.Utf8)
.toString()
).articleDetail.articleDetailData.data;
item.description = content.widgetContent;
} else {
const $ = cheerio.load(detailResponse.body);
item.description = $('div.articleDetailContent').html();
}
return item;
})
)
);
items = await Promise.all(items.map((item) => ProcessItem(item, ctx.cache.tryGet)));
}
ctx.state.data = {

View File

@ -1,4 +1,5 @@
module.exports = {
'/hot-list/:category?': ['nczitzk'],
'/information/:category?': ['nczitzk'],
'/motif/:id': ['nczitzk'],
'/newsflashes': ['hillerliao', 'nczitzk'],

View File

@ -44,6 +44,12 @@ module.exports = {
source: ['/search/newsflashes/:keyword', '/'],
target: '/36kr/search/newsflashes/:keyword',
},
{
title: '资讯热榜',
docs: 'https://docs.rsshub.app/new-media.html#_36kr-zi-xun-re-bang',
source: ['/hot-list/:category', '/'],
target: '/36kr/hot-list/:category',
},
],
},
};

View File

@ -1,3 +1,4 @@
module.exports = (router) => {
router.get('/hot-list/:category?', require('./hot-list'));
router.get(/([\w-/]+)?/, require('./index'));
};

36
lib/v2/36kr/utils.js Normal file
View File

@ -0,0 +1,36 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const CryptoJS = require('crypto-js');
const rootUrl = 'https://www.36kr.com';
module.exports = {
rootUrl,
ProcessItem: (item, tryGet) =>
tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const cipherTextList = detailResponse.data.match(/{"state":"(.*)","isEncrypt":true}/) ?? [];
if (cipherTextList.length !== 0) {
const key = CryptoJS.enc.Utf8.parse('efabccee-b754-4c');
const content = JSON.parse(
CryptoJS.AES.decrypt(cipherTextList[1], key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
})
.toString(CryptoJS.enc.Utf8)
.toString()
).articleDetail.articleDetailData.data;
item.description = content.widgetContent;
} else {
const $ = cheerio.load(detailResponse.body);
item.description = $('div.articleDetailContent').html();
}
return item;
}),
};