From 6cc7f2b192bd8625995844e60af0860b25bb3da7 Mon Sep 17 00:00:00 2001
From: Kyli <50611440+leyuuu@users.noreply.github.com>
Date: Sun, 23 Aug 2020 04:41:12 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20add=20segmentfault=20=E7=94=A8=E6=88=B7?=
=?UTF-8?q?=E6=96=87=E7=AB=A0=20(#5465)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docs/programming.md | 4 ++
lib/router.js | 1 +
lib/routes/segmentfault/user.js | 90 +++++++++++++++++++++++++++++++++
3 files changed, 95 insertions(+)
create mode 100644 lib/routes/segmentfault/user.js
diff --git a/docs/programming.md b/docs/programming.md
index e675dc7e50..4f5b399977 100644
--- a/docs/programming.md
+++ b/docs/programming.md
@@ -314,6 +314,10 @@ GitHub 官方也提供了一些 RSS:
+### 用户
+
+
+
## TesterHome
### 最新发布
diff --git a/lib/router.js b/lib/router.js
index 7954258947..88fcad32da 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -1339,6 +1339,7 @@ router.get('/leetcode/submission/cn/:user', require('./routes/leetcode/check-cn'
// segmentfault
router.get('/segmentfault/channel/:name', require('./routes/segmentfault/channel'));
+router.get('/segmentfault/user/:name', require('./routes/segmentfault/user'));
// 虎扑
router.get('/hupu/bxj/:id/:order?', require('./routes/hupu/bbs'));
diff --git a/lib/routes/segmentfault/user.js b/lib/routes/segmentfault/user.js
new file mode 100644
index 0000000000..68b3f1656a
--- /dev/null
+++ b/lib/routes/segmentfault/user.js
@@ -0,0 +1,90 @@
+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: title,
+ link: itemUrl,
+ author: author,
+ description: 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: link,
+ item: out,
+ };
+};
\ No newline at end of file