fix(route): segmentfault return empty and refactor to V2 (#9270)

* fix(route): return empty and refactor to V2

* docs: update segmentfault user example
This commit is contained in:
Fatpandac 2022-03-08 22:01:44 +08:00 committed by GitHub
parent b808cdb78f
commit 7e3bdc45fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 118 additions and 186 deletions

View File

@ -497,11 +497,11 @@ GitHub 官方也提供了一些 RSS:
### 频道
<Route author="LogicJake" example="/segmentfault/channel/frontend" path="/segmentfault/channel/:name" :paramsDesc="['频道名称,在频道 URL 可以找到']"/>
<Route author="LogicJake Fatpandac" example="/segmentfault/channel/frontend" path="/segmentfault/channel/:name" :paramsDesc="['频道名称,在频道 URL 可以找到']"/>
### 用户
<Route author="leyuuu" example="/segmentfault/user/yunqishequ_5aa899aad5395" path="/segmentfault/user/:name" :paramsDesc="['用户Id用户详情页URL可以找到']"/>
<Route author="leyuuu Fatpandac" example="/segmentfault/user/minnanitkong" path="/segmentfault/user/:name" :paramsDesc="['用户Id用户详情页URL可以找到']"/>
## TesterHome

View File

@ -1371,10 +1371,6 @@ router.get('/leetcode/articles', lazyloadRouteHandler('./routes/leetcode/article
router.get('/leetcode/submission/us/:user', lazyloadRouteHandler('./routes/leetcode/check-us'));
router.get('/leetcode/submission/cn/:user', lazyloadRouteHandler('./routes/leetcode/check-cn'));
// segmentfault
router.get('/segmentfault/channel/:name', lazyloadRouteHandler('./routes/segmentfault/channel'));
router.get('/segmentfault/user/:name', lazyloadRouteHandler('./routes/segmentfault/user'));
// 虎扑
router.get('/hupu/bxj/:id/:order?', lazyloadRouteHandler('./routes/hupu/bbs'));
router.get('/hupu/bbs/:id/:order?', lazyloadRouteHandler('./routes/hupu/bbs'));

View File

@ -1,90 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const host = 'https://segmentfault.com';
module.exports = async (ctx) => {
const name = ctx.params.name;
const link = `https://segmentfault.com/channel/${name}`;
const response = await got.get(link);
const $ = cheerio.load(response.data);
const channel_name = $('div.feed-option h5').text();
const list = $('div.news-item.stream__item.clearfix.mt15')
.slice(0, 10)
.map(function () {
const info = {
link: $(this).find('div.news__item-info.clearfix a').attr('href'),
title: $(this).find('div.mb5.mt5 h4').text(),
author: $(this).find('span.author.pr20 a').text(),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const itemUrl = url.resolve(host, info.link);
const author = info.author;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl);
const $ = cheerio.load(response.data);
let date_value = '';
let description = '';
if (info.link.indexOf('p') !== -1) {
// 分享
description =
$('div.fmt.mb10').html().trim() +
$('div.content h3')
.html()
.replace(/href="\//g, `href="${url.resolve(host, '.')}`)
.trim();
const date_list = $('div.news__item-info > p > span').text().trim().split(' ');
date_list.shift();
let date_v = '';
if (date_list.length > 1) {
date_v = date_list.join('');
} else {
date_v = date_list[0];
}
date_value = date_v.substring(0, date_v.length - 3);
} else {
// 文章
description = $('article.article.fmt.article-content')
.html()
.replace(/data-src="/g, 'src="https://segmentfault.com')
.trim();
const date_v = $('div.article__authorright > span').text().trim().replace(' ', '');
date_value = date_v.substring(0, date_v.length - 2);
}
const single = {
title,
link: itemUrl,
author,
description,
pubDate: date(date_value, 8),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${channel_name}——segmentfault`,
link,
item: out,
};
};

View File

@ -1,90 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const host = 'https://segmentfault.com';
module.exports = async (ctx) => {
const username = ctx.params.name;
const link = `https://segmentfault.com/u/${username}/articles`;
const response = await got.get(link);
const $ = cheerio.load(response.data);
const channel_name = $('h2.profile__heading--name').text().trim().split(' ')[0];
const list = $('ul.profile-mine__content li')
.slice(0, 10)
.map(function () {
const info = {
link: $(this).find('a.profile-mine__content--title').attr('href'),
title: $(this).find('a.profile-mine__content--title').text(),
author: channel_name,
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const itemUrl = url.resolve(host, info.link);
const author = info.author;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl);
const $ = cheerio.load(response.data);
let date_value = '';
let description = '';
if (info.link.indexOf('p') !== -1) {
// 分享
description =
$('div.fmt.mb10').html().trim() +
$('div.content h3')
.html()
.replace(/href="\//g, `href="${url.resolve(host, '.')}`)
.trim();
const date_list = $('div.news__item-info > p > span').text().trim().split(' ');
date_list.shift();
let date_v = '';
if (date_list.length > 1) {
date_v = date_list.join('');
} else {
date_v = date_list[0];
}
date_value = date_v.substring(0, date_v.length - 3);
} else {
// 文章
description = $('article.article.fmt.article-content')
.html()
.replace(/data-src="/g, 'src="https://segmentfault.com')
.trim();
const date_v = $('div.article__authorright > span').text().trim().replace(' ', '');
date_value = date_v.substring(0, date_v.length - 2);
}
const single = {
title,
link: itemUrl,
author,
description,
pubDate: date(date_value, 8),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${channel_name}-segmentfault`,
link,
item: out,
};
};

View File

@ -0,0 +1,46 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const host = 'https://segmentfault.com';
module.exports = async (ctx) => {
const name = ctx.params.name;
const link = `${host}/channel/${name}`;
const response = await got(link);
const $ = cheerio.load(response.data);
const channel_name = $('#leftNav > a.active').text();
const list = $('ul.bg-transparent.list-group.list-group-flush > li')
.slice(0, 10)
.map((_, item) => ({
link: new URL($(item).find('div.content > h5 > a').attr('href'), host).href,
title: $(item).find('div.content > h5 > a').text(),
author: $(item).find('span.name').text(),
}))
.get();
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link);
const content = cheerio.load(detailResponse.data);
item.description = content('article')
.html()
.replace(/data-src="/g, `src="${host}`);
item.pubDate = parseDate(content('time').attr('datetime'));
return item;
})
)
);
ctx.state.data = {
title: `segmentfault - ${channel_name}`,
link,
item: items,
};
};

View File

@ -0,0 +1,4 @@
module.exports = {
'/channel/:name': ['LogicJake', 'Fatpandac'],
'/user/:name': ['leyuuu', 'Fatpandac'],
};

View File

@ -0,0 +1,19 @@
module.exports = {
'segmentfault.com': {
_name: 'SegmentFault',
'.': [
{
title: '频道',
docs: 'https://docs.rsshub.app/programming.html#segmentfault',
source: ['/channel/:name'],
target: '/channel/:name',
},
{
title: '用户',
docs: 'https://docs.rsshub.app/programming.html#segmentfault',
source: ['/u/:name'],
target: '/user/:name',
},
],
},
};

View File

@ -0,0 +1,4 @@
module.exports = function (router) {
router.get('/channel/:name', require('./channel'));
router.get('/user/:name', require('./user'));
};

View File

@ -0,0 +1,43 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const host = 'https://segmentfault.com';
module.exports = async (ctx) => {
const name = ctx.params.name;
const apiURL = `https://segmentfault.com/gateway/homepage/${name}/timeline?size=20&offset=`;
const response = await got(apiURL);
const data = response.data.rows;
const author = data[0].user.name;
const list = data.map((item) => ({
title: item.title,
description: item.excerpt,
link: new URL(item.url, host).href,
author,
}));
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got(item.link);
const content = cheerio.load(response.data);
item.description = content('article')
.html()
.replace(/data-src="/g, `src="${host}`);
item.pubDate = parseDate(content('time').attr('datetime'));
return item;
})
)
);
ctx.state.data = {
title: `segmentfault - ${author}`,
link: `${host}/u/${name}`,
item: items,
};
};