From a7635464d29003bf9d14e27ad43734db79598fe1 Mon Sep 17 00:00:00 2001 From: SettingDust Date: Thu, 9 Sep 2021 16:56:52 +0800 Subject: [PATCH] fix(route): netease (#8083) Co-authored-by: MA Junyi --- lib/routes/netease/dy2.js | 66 +++++++++++++++------------------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/lib/routes/netease/dy2.js b/lib/routes/netease/dy2.js index 3e7f247fc2..059db7c9ac 100644 --- a/lib/routes/netease/dy2.js +++ b/lib/routes/netease/dy2.js @@ -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(), }; };