修复learnku descript 显示不正确和不加载内容的问题 (#8818)

Co-authored-by: Kai Wei <kai.wei@rightcapital.com>
This commit is contained in:
Kay W
2022-01-22 20:02:34 +08:00
committed by GitHub
parent 1c6033113f
commit c26b25b571
7 changed files with 105 additions and 56 deletions

View File

@@ -2681,9 +2681,6 @@ router.get('/krankenkassen', lazyloadRouteHandler('./routes/krankenkassen'));
// 桂林航天工业学院
router.get('/guat/news/:type?', lazyloadRouteHandler('./routes/guat/news'));
// LearnKu
router.get('/learnku/:community/:category?', lazyloadRouteHandler('./routes/learnku/topic'));
// NEEA
router.get('/neea/:type', lazyloadRouteHandler('./routes/neea'));

View File

@@ -1,53 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const community = ctx.params.community;
const category = ctx.params.category || '';
let url = `https://learnku.com/${community}`;
if (category !== '') {
url = `https://learnku.com/${community}/c/${category}`;
}
const response = await got({
method: 'get',
url,
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('.simple-topic').get();
const title = $('.sidebar .community-details .header span').text();
$('.sidebar .community-details .main div div a').remove();
const description = $('.sidebar .community-details .main div div').text();
const categoryTitle = new Map([
['translations', { name: '翻译' }],
['jobs', { name: '招聘' }],
['qa', { name: '问答' }],
['links', { name: '链接' }],
['', { name: '最新' }],
]);
ctx.state.data = {
title: `LearnKu - ${title} - ${categoryTitle.get(category).name}`,
link: url,
description,
item: list
.map((item) => {
const $ = cheerio.load(item);
const categoryName = $('.category-name').text().trim();
if (['置顶', '广告'].includes(categoryName)) {
return '';
}
$('.topic-title i').remove();
return {
title: $('.topic-title').text().trim(),
category: categoryName,
link: $('.topic-title-wrap').attr('href'),
pubDate: new Date($('.timeago').attr('title')).toUTCString(),
};
})
.filter((item) => item !== ''),
};
};

View File

@@ -0,0 +1,3 @@
module.exports = {
'/:community/:category?': ['kayw-geek'],
};

13
lib/v2/learnku/radar.js Normal file
View File

@@ -0,0 +1,13 @@
module.exports = {
'learnku.com': {
_name: 'Learn Ku 社区',
'.': [
{
title: '分区',
docs: 'https://docs.rsshub.app/bbs.html#learnku',
source: ['/:community'],
target: '/learnku/:community',
},
],
},
};

3
lib/v2/learnku/router.js Normal file
View File

@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:community/:category?', require('./topic.js'));
};

View File

@@ -0,0 +1,14 @@
<div style="background-color:#f0f2f5;border: 2px solid #e3eaef;box-shadow: 0 1px 2px 0 rgb(101 129 156 / 8%);font-size: 1rem;position: relative;background: #fff;box-shadow: 0px 2px 4px rgba(0,0,0,0.1);margin: 1rem 0;padding: 24px;transition: box-shadow 0.15s ease;border-radius: 8px;">
<h2>🦕正文</h2>
<HR style="FILTER: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color=#79ffe1 SIZE=3>
<div style="font-family:'Inter',-apple-system,BlinkMacSystemFont,'Segoe UI','Roboto','Oxygen','Ubuntu','Cantarell','Fira Sans','Droid Sans','Helvetica Neue',sans-serif">
{{@ article }}
</div>
</div>
{{if comment }}
<div style="background-color:#f0f2f5;border: 2px solid #e3eaef;box-shadow: 0 1px 2px 0 rgb(101 129 156 / 8%);font-size: 1rem;position: relative;background: #fff;box-shadow: 0px 2px 4px rgba(0,0,0,0.1);margin: 1rem 0;padding: 24px;transition: box-shadow 0.15s ease;border-radius: 8px;">
<h2>👨‍💻评论</h2>
<HR style="FILTER: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color=#79ffe1 SIZE=3>
{{@ comment }}
</div>
{{/if}}

72
lib/v2/learnku/topic.js Normal file
View File

@@ -0,0 +1,72 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { art } = require('@/utils/render');
const path = require('path');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const community = ctx.params.community;
const category = ctx.params.category || '';
let url = `https://learnku.com/${community}`;
if (category !== '') {
url = `https://learnku.com/${community}/c/${category}`;
}
const response = await got({
method: 'get',
url,
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('.simple-topic').get();
const item = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const categoryName = $('.category-name span').text().trim();
if (['置顶', '广告'].includes(categoryName)) {
return '';
}
$('.topic-title i').remove();
const itemLink = $('.topic-title-wrap').attr('href');
const title = $('.topic-title').text().trim();
const content = await ctx.cache.tryGet(itemLink, async () => {
const result = await got.get(itemLink);
return cheerio.load(result.data);
});
const article = content('.article-content .content-body').html();
const comment = content('#all-comments').html();
return {
title,
description: art(path.join(__dirname, 'templates/topic.art'), {
article,
comment,
}),
category: categoryName,
link: itemLink,
pubDate: parseDate($('.timeago').attr('title'), 'YYYY/MM/DD'),
};
})
);
const title = $('.sidebar .community-details .header span').text();
$('.sidebar .community-details .main div div a').remove();
const description = $('.sidebar .community-details .main div div').text();
const categoryTitle = new Map([
['translations', { name: '翻译' }],
['jobs', { name: '招聘' }],
['qa', { name: '问答' }],
['links', { name: '链接' }],
['', { name: '最新' }],
]);
ctx.state.data = {
title: `LearnKu - ${title} - ${categoryTitle.get(category).name}`,
link: url,
description,
item: item.filter((item) => item !== ''),
};
};