fix(route): 软餐 (#11708)

This commit is contained in:
Ethan Shen 2023-01-28 00:26:22 +08:00 committed by GitHub
parent 1636b09b71
commit e90afcb6d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 118 additions and 71 deletions

View File

@ -3423,15 +3423,15 @@ column 为 third 时可选的 category:
### 分类
<Route author="nczitzk" example="/ruancan/sort/news" path="/ruancan/sort/:sort" :paramsDesc="['分类 id可在对应分类页 URL 中找到']"/>
### 标签
<Route author="nczitzk" example="/ruancan/tag/oxygenos" path="/ruancan/tag/:tag" :paramsDesc="['标签 id可在对应标签页 URL 中找到']"/>
<Route author="nczitzk" example="/ruancan/category/news" path="/ruancan/category/:category?" :paramsDesc="['分类 id可在对应分类页 URL 中找到,默认为业界']"/>
### 搜索
<Route author="nczitzk" example="/ruancan/search/ColorOS" path="/ruancan/search/:keyword?" :paramsDesc="['关键字,默认为空']"/>
<Route author="nczitzk" example="/ruancan/search/Windows" path="/ruancan/search/:keyword?" :paramsDesc="['关键字,默认为空']"/>
### 用户文章
<Route author="nczitzk" example="/ruancan/user/72" path="/ruancan/user/:id?" :paramsDesc="['用户 id可在对应用户页 URL 中找到']"/>
## 上下游 News\&Market

View File

@ -0,0 +1,8 @@
const fetchFeed = require('./utils');
module.exports = async (ctx) => {
const category = ctx.params.category;
const currentUrl = `/cat/${category}`;
ctx.state.data = await fetchFeed(ctx, currentUrl);
};

View File

@ -1,59 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const fetchFeed = require('./utils');
module.exports = async (ctx) => {
const todo = ctx.params.do ?? '';
const keyword = ctx.params.keyword ?? '';
const currentUrl = '';
const rootUrl = 'https://www.ruancan.com';
const currentUrl = `${rootUrl}${todo ? (todo === 'search' ? `/page/1?s=${keyword}` : `/${todo}/${keyword}`) : ''}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $('.entry-title a')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
content('.entry-content .heateorSssClear').first().nextAll().remove();
content('.adsbygoogle, .heateorSssClear').remove();
item.description = content('.entry-content').html();
item.category = content('.cat-links a')
.toArray()
.map((a) => content(a).text());
item.pubDate = parseDate(content('meta[property="article:published_time"]').attr('content'));
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
ctx.state.data = await fetchFeed(ctx, currentUrl);
};

View File

@ -1,6 +1,6 @@
module.exports = {
'/ruancan': ['nczitzk'],
'/ruancan/sort/:sort?': ['nczitzk'],
'/ruancan/tag/:tag': ['nczitzk'],
'/ruancan/category/:category?': ['nczitzk'],
'/ruancan/search/:keyword?': ['nczitzk'],
'/ruancan/user/:id': ['nczitzk'],
'/ruancan': ['nczitzk'],
};

View File

@ -11,14 +11,20 @@ module.exports = {
{
title: '分类',
docs: 'https://docs.rsshub.app/new-media.html#ruan-can-fen-lei',
source: ['/sort/:sort', '/'],
target: '/ruancan/sort/:sort',
source: ['/cat/:category', '/'],
target: '/ruancan/category/:category',
},
{
title: '标签',
docs: 'https://docs.rsshub.app/new-media.html#ruan-can-biao-qian',
source: ['/tag/:tag', '/'],
target: '/ruancan/tag/:tag',
title: '搜索',
docs: 'https://docs.rsshub.app/new-media.html#ruan-can-sou-suo',
source: ['/'],
target: (params, url) => `/ruancan/search/${new URL(url).searchParams.get('s')}`,
},
{
title: '用户文章',
docs: 'https://docs.rsshub.app/new-media.html#ruan-can-yong-hu-wen-zhang',
source: ['/i/:id', '/'],
target: '/ruancan/user/:id',
},
],
},

View File

@ -1,3 +1,6 @@
module.exports = function (router) {
router.get('/:do?/:keyword?', require('./index'));
router.get('/category/:category?', require('./category'));
router.get('/search/:keyword?', require('./search'));
router.get('/user/:id', require('./user'));
router.get('/', require('./index'));
};

8
lib/v2/ruancan/search.js Normal file
View File

@ -0,0 +1,8 @@
const fetchFeed = require('./utils');
module.exports = async (ctx) => {
const keyword = ctx.params.keyword;
const currentUrl = `/?s=${keyword}`;
ctx.state.data = await fetchFeed(ctx, currentUrl);
};

8
lib/v2/ruancan/user.js Normal file
View File

@ -0,0 +1,8 @@
const fetchFeed = require('./utils');
module.exports = async (ctx) => {
const id = ctx.params.id;
const currentUrl = `/i/${id}`;
ctx.state.data = await fetchFeed(ctx, currentUrl);
};

66
lib/v2/ruancan/utils.js Normal file
View File

@ -0,0 +1,66 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx, currentUrl) => {
const rootUrl = 'https://www.ruancan.com';
currentUrl = `${rootUrl}${currentUrl}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $('.item-title a')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 15)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
content('.entry-copyright').remove();
content('.entry-content div').each(function () {
if (/^ruanc-\d+/.test(content(this).attr('id'))) {
content(this).remove();
}
});
content('figure').each(function () {
content(this).html(`<img src="${content(this).find('a').attr('href')}">`);
});
item.description = content('.entry-content').html();
item.category = content('.entry-info a[rel="category tag"]')
.toArray()
.map((c) => content(c).text());
item.pubDate = parseDate(content('.entry-info .entry-date').attr('datetime'));
return item;
})
)
);
return {
title: $('title').text(),
link: currentUrl,
item: items,
};
};