From 8b0bc5cc95f86ed13d1c4aebfba31ec01e5254a6 Mon Sep 17 00:00:00 2001
From: JimenezLi <75196426+JimenezLi@users.noreply.github.com>
Date: Tue, 19 Dec 2023 21:41:00 +0800
Subject: [PATCH] feat(route): migrate jianshu to v2 (#14081)
* feat(route): migrate jianshu to v2
* feat(docs): update jianshu docs
* Refactor ProcessFeed function to improve code readability and performance
---------
---
lib/router.js | 6 ++--
lib/{routes => v2}/jianshu/collection.js | 0
lib/{routes => v2}/jianshu/home.js | 0
lib/v2/jianshu/maintainer.js | 5 +++
lib/v2/jianshu/radar.js | 25 ++++++++++++++
lib/v2/jianshu/router.js | 5 +++
lib/{routes => v2}/jianshu/user.js | 0
lib/{routes => v2}/jianshu/utils.js | 44 ++++++++++--------------
website/docs/routes/social-media.mdx | 6 ++--
9 files changed, 59 insertions(+), 32 deletions(-)
rename lib/{routes => v2}/jianshu/collection.js (100%)
rename lib/{routes => v2}/jianshu/home.js (100%)
create mode 100644 lib/v2/jianshu/maintainer.js
create mode 100644 lib/v2/jianshu/radar.js
create mode 100644 lib/v2/jianshu/router.js
rename lib/{routes => v2}/jianshu/user.js (100%)
rename lib/{routes => v2}/jianshu/utils.js (56%)
diff --git a/lib/router.js b/lib/router.js
index fcb036837..0d814e7b9 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -26,9 +26,9 @@ router.get('/benedictevans', lazyloadRouteHandler('./routes/benedictevans/recent
router.get('/ziroom/room/:city/:iswhole/:room/:keyword', lazyloadRouteHandler('./routes/ziroom/room'));
// 简书
-router.get('/jianshu/home', lazyloadRouteHandler('./routes/jianshu/home'));
-router.get('/jianshu/collection/:id', lazyloadRouteHandler('./routes/jianshu/collection'));
-router.get('/jianshu/user/:id', lazyloadRouteHandler('./routes/jianshu/user'));
+// router.get('/jianshu/home', lazyloadRouteHandler('./routes/jianshu/home'));
+// router.get('/jianshu/collection/:id', lazyloadRouteHandler('./routes/jianshu/collection'));
+// router.get('/jianshu/user/:id', lazyloadRouteHandler('./routes/jianshu/user'));
// 妹子图
router.get('/mzitu/home/:type?', lazyloadRouteHandler('./routes/mzitu/home'));
diff --git a/lib/routes/jianshu/collection.js b/lib/v2/jianshu/collection.js
similarity index 100%
rename from lib/routes/jianshu/collection.js
rename to lib/v2/jianshu/collection.js
diff --git a/lib/routes/jianshu/home.js b/lib/v2/jianshu/home.js
similarity index 100%
rename from lib/routes/jianshu/home.js
rename to lib/v2/jianshu/home.js
diff --git a/lib/v2/jianshu/maintainer.js b/lib/v2/jianshu/maintainer.js
new file mode 100644
index 000000000..f9539faab
--- /dev/null
+++ b/lib/v2/jianshu/maintainer.js
@@ -0,0 +1,5 @@
+module.exports = {
+ '/collection/:id': ['DIYgod', 'HenryQW', 'JimenezLi'],
+ '/home': ['DIYgod', 'HenryQW', 'JimenezLi'],
+ '/user/:id': ['DIYgod', 'HenryQW', 'JimenezLi'],
+};
diff --git a/lib/v2/jianshu/radar.js b/lib/v2/jianshu/radar.js
new file mode 100644
index 000000000..50bf545d2
--- /dev/null
+++ b/lib/v2/jianshu/radar.js
@@ -0,0 +1,25 @@
+module.exports = {
+ 'jianshu.com': {
+ _name: '简书',
+ www: [
+ {
+ title: '首页',
+ docs: 'https://docs.rsshub.app/routes/social-media#jian-shu',
+ source: '/',
+ target: '/jianshu/home',
+ },
+ {
+ title: '专题',
+ docs: 'https://docs.rsshub.app/routes/social-media#jian-shu',
+ source: '/c/:id',
+ target: '/jianshu/collection/:id',
+ },
+ {
+ title: '作者',
+ docs: 'https://docs.rsshub.app/routes/social-media#jian-shu',
+ source: '/u/:id',
+ target: '/jianshu/user/:id',
+ },
+ ],
+ },
+};
diff --git a/lib/v2/jianshu/router.js b/lib/v2/jianshu/router.js
new file mode 100644
index 000000000..b7a633c22
--- /dev/null
+++ b/lib/v2/jianshu/router.js
@@ -0,0 +1,5 @@
+module.exports = (router) => {
+ router.get('/collection/:id', require('./collection'));
+ router.get('/home', require('./home'));
+ router.get('/user/:id', require('./user'));
+};
diff --git a/lib/routes/jianshu/user.js b/lib/v2/jianshu/user.js
similarity index 100%
rename from lib/routes/jianshu/user.js
rename to lib/v2/jianshu/user.js
diff --git a/lib/routes/jianshu/utils.js b/lib/v2/jianshu/utils.js
similarity index 56%
rename from lib/routes/jianshu/utils.js
rename to lib/v2/jianshu/utils.js
index 2a731e88a..c8715741d 100644
--- a/lib/routes/jianshu/utils.js
+++ b/lib/v2/jianshu/utils.js
@@ -1,18 +1,14 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
-const url = require('url');
+const { parseDate } = require('@/utils/parse-date');
+const timezone = require('@/utils/timezone');
// 加载文章页
-async function load(link) {
+const load = async (link) => {
const response = await got.get(link);
const $ = cheerio.load(response.data);
-
// 解析日期
- const date = new Date($('time').attr('datetime'));
- const timeZone = 8;
- const serverOffset = date.getTimezoneOffset() / 60;
- const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString();
-
+ const pubDate = timezone(parseDate($('time').attr('datetime')), +8);
// 去除样式
$('.image-container, .image-container-fill').removeAttr('style');
// 处理视频
@@ -30,33 +26,29 @@ async function load(link) {
const description = $('article').html();
return { description, pubDate };
-}
+};
const ProcessFeed = (list, caches) => {
const host = 'https://www.jianshu.com';
return Promise.all(
- list.map(async (item) => {
+ list.map((item) => {
const $ = cheerio.load(item);
-
const $title = $('.title');
// 还原相对链接为绝对链接
- const itemUrl = url.resolve(host, $title.attr('href'));
-
- // 列表上提取到的信息
- const single = {
- title: $title.text(),
- link: itemUrl,
- author: $('.nickname').text(),
- guid: itemUrl,
- };
-
+ const itemUrl = new URL($title.attr('href'), host).toString();
// 使用tryGet方法从缓存获取内容。
- // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。
- const other = await caches.tryGet(itemUrl, () => load(itemUrl));
-
- // 合并解析后的结果集作为该篇文章最终的输出结果
- return Promise.resolve(Object.assign({}, single, other));
+ // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容
+ return caches.tryGet(itemUrl, async () => {
+ const other = await load(itemUrl);
+ // 合并解析后的结果集作为该篇文章最终的输出结果
+ return {
+ title: $title.text(),
+ link: itemUrl,
+ author: $('.nickname').text(),
+ ...other,
+ };
+ });
})
);
};
diff --git a/website/docs/routes/social-media.mdx b/website/docs/routes/social-media.mdx
index 06c15013c..7246fb282 100644
--- a/website/docs/routes/social-media.mdx
+++ b/website/docs/routes/social-media.mdx
@@ -1317,15 +1317,15 @@ YouTube provides official RSS feeds for channels, for instance [https://www.yout
### 首页 {#jian-shu-shou-ye}
-
+
### 专题 {#jian-shu-zhuan-ti}
-
+
### 作者 {#jian-shu-zuo-zhe}
-
+
## 酷安 {#ku-an}