From c4f1869625570d9160743fd23dcc23c23c93d706 Mon Sep 17 00:00:00 2001
From: Fatpandac <1779196284@qq.com>
Date: Wed, 2 Feb 2022 22:42:31 +0800
Subject: [PATCH] feat(route): zhihu get all collections (#8943)
---
docs/social-media.md | 2 +-
lib/router.js | 2 +-
lib/routes/zhihu/collection.js | 28 ++++++++++++++++++++++++++--
lib/routes/zhihu/pin/utils.js | 24 +++++++++++++++++-------
4 files changed, 45 insertions(+), 11 deletions(-)
diff --git a/docs/social-media.md b/docs/social-media.md
index 9577de5085..979808a1df 100644
--- a/docs/social-media.md
+++ b/docs/social-media.md
@@ -1220,7 +1220,7 @@ rule
### 收藏夹
-
+
### 用户动态
diff --git a/lib/router.js b/lib/router.js
index 6cbf6d8800..25d1c081c5 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -128,7 +128,7 @@ router.get('/jianshu/collection/:id', lazyloadRouteHandler('./routes/jianshu/col
router.get('/jianshu/user/:id', lazyloadRouteHandler('./routes/jianshu/user'));
// 知乎
-router.get('/zhihu/collection/:id', lazyloadRouteHandler('./routes/zhihu/collection'));
+router.get('/zhihu/collection/:id/:getAll?', lazyloadRouteHandler('./routes/zhihu/collection'));
router.get('/zhihu/people/activities/:id', lazyloadRouteHandler('./routes/zhihu/activities'));
router.get('/zhihu/people/answers/:id', lazyloadRouteHandler('./routes/zhihu/answers'));
router.get('/zhihu/posts/:usertype/:id', lazyloadRouteHandler('./routes/zhihu/posts'));
diff --git a/lib/routes/zhihu/collection.js b/lib/routes/zhihu/collection.js
index 3d0e0d37fb..ef80a49ef6 100644
--- a/lib/routes/zhihu/collection.js
+++ b/lib/routes/zhihu/collection.js
@@ -5,6 +5,7 @@ const { generateData } = require('./pin/utils');
module.exports = async (ctx) => {
const id = ctx.params.id;
+ const getAll = ctx.params.getAll;
const response = await got({
method: 'get',
@@ -17,6 +18,29 @@ module.exports = async (ctx) => {
const list = response.data.data;
+ if (getAll) {
+ const totals = response.data.paging.totals;
+
+ const offsetList = [...new Array(Math.round(totals / 20)).keys()].map((item) => item * 20).slice(1);
+ const otherList = await Promise.all(
+ offsetList.map((offset) =>
+ ctx.cache.tryGet(`https://www.zhihu.com/api/v4/collections/${id}/items?offset=${offset}&limit=20`, async () => {
+ const response = await got({
+ method: 'get',
+ url: `https://www.zhihu.com/api/v4/collections/${id}/items?offset=${offset}&limit=20`,
+ headers: {
+ ...utils.header,
+ Referer: `https://www.zhihu.com/collection/${id}`,
+ },
+ });
+
+ return response.data.data;
+ })
+ )
+ ).then((item) => item.flat());
+ list.push(...otherList);
+ }
+
const response2 = await got({
method: 'get',
url: `https://www.zhihu.com/collection/${id}`,
@@ -42,9 +66,9 @@ module.exports = async (ctx) => {
item.content.type === 'pin'
? generateDataPin(item)
: {
- title: item.content.type === 'article' ? item.content.title : item.content.question.title,
+ title: item.content.type === 'article' || item.content.type === 'zvideo' ? item.content.title : item.content.question.title,
link: item.content.url,
- description: item.content.content,
+ description: item.content.type === 'zvideo' ? `
` : item.content.content,
pubDate: new Date((item.content.type === 'article' ? item.content.updated : item.content.updated_time) * 1000).toUTCString(),
}
),
diff --git a/lib/routes/zhihu/pin/utils.js b/lib/routes/zhihu/pin/utils.js
index 36f3893ed1..6cc3ae04c4 100644
--- a/lib/routes/zhihu/pin/utils.js
+++ b/lib/routes/zhihu/pin/utils.js
@@ -17,13 +17,23 @@ module.exports = {
break;
case 'video':
- description += `