Merge pull request #6570 from laampui/open

fix: outdated route /open163/vip
This commit is contained in:
NeverBehave
2021-01-10 13:22:56 -05:00
committed by GitHub
+22 -44
View File
@@ -2,60 +2,38 @@ const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = 'https://open.163.com/';
const url = 'https://vip.open.163.com';
const list_response = await got.get(url);
const $ = cheerio.load(list_response.data);
const list = $('.tabcon > div:nth-child(1) ul.list li').toArray();
const parseContent = (htmlString) => {
const $ = cheerio.load(htmlString);
const author = $('.CourseCommonDetail_info > div:nth-child(4)');
const content = $('.CourseCommonDetail_contentDesc');
return {
author: author.text().trim(),
description: content.html(),
pubDate: new Date(),
};
};
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('.ltxt').text().trim();
const link = $('a.item').attr('href').split('?')[0];
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const rssitem = {
title: title,
link: link,
const list = $('.CourseListItem_ListItem')
.map((index, ele) => {
$('.CourseListItem_oriPrice', ele).remove();
const price = $('.CourseListItem_price', ele).text().trim();
return {
title: price + ' - ' + $('.CourseListItem_title', ele).text(),
link: url + $(ele).attr('href'),
};
try {
const response = await got.get(link);
const result = parseContent(response.data);
rssitem.author = result.author;
rssitem.description = result.description;
rssitem.pubDate = result.pubDate;
} catch (err) {
return Promise.resolve('');
}
ctx.cache.set(link, JSON.stringify(rssitem));
return Promise.resolve(rssitem);
})
.get();
const item = await Promise.all(
list.slice(0, 10).map((item) =>
ctx.cache.tryGet(item.link, async () => {
const res = await got.get(item.link);
const s = cheerio.load(res.data);
s('img').css('max-width', '100%');
const toc = s('.CourseCommonDetail_contentItem').eq(1).html();
item.description = `<div style="text-align: center;">${toc}` + s('.CourseCommonDetail_contentItem').first().html() + `</div>`;
return item;
})
)
);
ctx.state.data = {
title: '网易公开课 - 精品课程',
link: url,
item: out.filter((item) => item !== ''),
item,
};
};