fix(route): 36kr dom parse errors (#11199)

* fix 36kr router error when a ads in list.

* require missing depend: querystring

* filter out ads instead of parse it

* remove a redundant line

* remove redundant package and format code

* fix dom parse error cause possible encrypt content

* lint & fix missing change cause accident commit

* fix errors

* fix lint

* fix error

* revert conditional clause
This commit is contained in:
Gerardyang
2022-11-10 20:44:34 +08:00
committed by GitHub
parent c7e0a099b7
commit 3f82eedbf3
+23 -19
View File
@@ -27,16 +27,18 @@ module.exports = async (ctx) => {
const data = JSON.parse(response.data.match(/"itemList":(\[.*?\])/)[1]);
let items = data.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 30).map((item) => {
item = item.templateMaterial ?? item;
return {
title: item.widgetTitle.replace(/<\/?em>/g, ''),
author: item.author,
pubDate: parseDate(item.publishTime),
link: `${rootUrl}/${path === '/newsflashes' ? 'newsflashes' : 'p'}/${item.itemId}`,
description: item.widgetContent ?? item.content,
};
});
let items = data.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 30)
.filter((item) => item.itemType !== 0)
.map((item) => {
item = item.templateMaterial ?? item;
return {
title: item.widgetTitle.replace(/<\/?em>/g, ''),
author: item.author,
pubDate: parseDate(item.publishTime),
link: `${rootUrl}/${path === '/newsflashes' ? 'newsflashes' : 'p'}/${item.itemId}`,
description: item.widgetContent ?? item.content,
};
});
if (!/^\/(search|newsflashes)/.test(path)) {
items = await Promise.all(
@@ -47,20 +49,22 @@ module.exports = async (ctx) => {
url: item.link,
});
const key = CryptoJS.enc.Utf8.parse('efabccee-b754-4c');
const cipherText = detailResponse.data.match(/{"state":"(.*)","isEncrypt":true}/)[1];
const content = JSON.parse(
CryptoJS.AES.decrypt(cipherText, key, {
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;
).articleDetail.articleDetailData.data;
item.description = content.widgetContent;
} else {
const $ = cheerio.load(detailResponse.body);
item.description = $('div.articleDetailContent').html();
}
return item;
})
)