fix(route): netease (#8083)

Co-authored-by: MA Junyi <mjysci@live.com>
This commit is contained in:
SettingDust
2021-09-09 01:56:52 -07:00
committed by GitHub
co-authored by MA Junyi
parent 080282fc01
commit a7635464d2
+25 -41
View File
@@ -1,78 +1,62 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const id = ctx.params.id;
const { id } = ctx.params;
const limit = ctx.query.limit || 5;
const url = `https://www.163.com/dy/media/${id}.html`;
const response = await got.get(url, { responseType: 'buffer' });
const response = await got(url, { responseType: 'buffer' });
const charset = response.headers['content-type'].split('=')[1];
const data = iconv.decode(response.data, charset);
const $ = cheerio.load(data, { decodeEntities: false });
const list = $('.media_articles ul li')
.slice(0, 5)
.slice(0, limit)
.map((_, item) => {
item = $(item);
const a = item.find('h2.media_article_title a');
let pubDate = item.find('.media_article_date').text();
pubDate = new Date(pubDate).toUTCString();
return {
title: a.text(),
link: a.attr('href'),
pubDate,
pubDate: parseDate(item.find('.media_article_date').text()),
};
})
.get();
let link;
const items = await Promise.all(
list.map(async (item) => {
let content;
if (item.link.startsWith('https://www.163.com')) {
const itemData = await ctx.cache.tryGet(item.link, async () =>
iconv.decode(
(
await got.get(item.link, {
responseType: 'buffer',
})
).data,
charset
)
);
content = cheerio.load(itemData, { decodeEntities: false });
} else {
const itemData = await ctx.cache.tryGet(
item.link,
async () =>
(
await got.get(item.link, {
responseType: 'buffer',
})
).data
);
content = cheerio.load(itemData, { decodeEntities: false });
}
const eDescription = content('.post_body').html();
const single = {
const itemData = await ctx.cache.tryGet(item.link, () =>
got(item.link, {
responseType: 'buffer',
}).then((it) => it.data)
);
const content = cheerio.load(itemData, { decodeEntities: false });
const postBody = content('.post_body');
postBody.find('p, br, section').each((_, elem) => elem.attribs && Object.keys(elem.attribs).forEach((attr) => content(elem).removeAttr(attr)));
postBody
.parent()
.find('*')
.contents()
.filter((_, elem) => elem.type === 'comment')
.remove();
return {
title: item.title,
link: item.link,
description: eDescription,
description: postBody.html(),
pubDate: item.pubDate,
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title: $('.media_info h1').text(),
link,
description: '',
link: url,
description: $('.media_degist').text(),
item: items,
author: $('.media_name').text(),
};
};