mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
+25
-41
@@ -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(),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user