@jackwener/opencli 1.7.22 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -194
- package/README.zh-CN.md +42 -260
- package/cli-manifest.json +8160 -4392
- package/clis/12306/me.js +73 -0
- package/clis/12306/orders.js +96 -0
- package/clis/12306/passengers.js +90 -0
- package/clis/12306/price.js +166 -0
- package/clis/12306/stations.js +66 -0
- package/clis/12306/train.js +91 -0
- package/clis/12306/trains.js +119 -0
- package/clis/12306/utils.js +272 -0
- package/clis/12306/utils.test.js +331 -0
- package/clis/36kr/article.js +6 -3
- package/clis/36kr/article.test.js +46 -0
- package/clis/_atlassian/shared.js +577 -0
- package/clis/_atlassian/shared.test.js +170 -0
- package/clis/apple-podcasts/commands.test.js +20 -0
- package/clis/apple-podcasts/search.js +2 -2
- package/clis/barchart/greeks.js +144 -56
- package/clis/barchart/greeks.test.js +138 -0
- package/clis/bilibili/comment.js +125 -0
- package/clis/bilibili/comment.test.js +153 -0
- package/clis/bilibili/comments.js +116 -21
- package/clis/bilibili/comments.test.js +77 -18
- package/clis/bilibili/subtitle.js +76 -31
- package/clis/bilibili/subtitle.test.js +156 -9
- package/clis/bilibili/summary.js +167 -0
- package/clis/bilibili/summary.test.js +210 -0
- package/clis/bilibili/utils.js +63 -5
- package/clis/bilibili/utils.test.js +45 -1
- package/clis/booking/booking.test.js +356 -0
- package/clis/booking/search.js +351 -0
- package/clis/chatgpt/envelope.test.js +108 -0
- package/clis/chatgpt/image.js +2 -2
- package/clis/chatgpt/image.test.js +6 -0
- package/clis/chatgpt/utils.js +148 -41
- package/clis/chatgpt/utils.test.js +92 -2
- package/clis/chess/analyze.js +35 -0
- package/clis/chess/analyze.test.js +79 -0
- package/clis/chess/game.js +114 -0
- package/clis/chess/game.test.js +178 -0
- package/clis/chess/games.js +67 -0
- package/clis/chess/games.test.js +164 -0
- package/clis/chess/stats.js +32 -0
- package/clis/chess/stats.test.js +79 -0
- package/clis/chess/utils.js +170 -0
- package/clis/chess/utils.test.js +230 -0
- package/clis/confluence/commands.test.js +195 -0
- package/clis/confluence/create.js +39 -0
- package/clis/confluence/page.js +23 -0
- package/clis/confluence/search.js +34 -0
- package/clis/confluence/shared.js +173 -0
- package/clis/confluence/update.js +38 -0
- package/clis/douyin/_shared/browser-fetch.js +44 -20
- package/clis/douyin/_shared/browser-fetch.test.js +22 -1
- package/clis/douyin/_shared/evaluate-result.js +16 -0
- package/clis/douyin/_shared/tos-upload.js +105 -69
- package/clis/douyin/_shared/vod-upload.js +212 -0
- package/clis/douyin/_shared/vod-upload.test.js +38 -0
- package/clis/douyin/delete.js +137 -4
- package/clis/douyin/delete.test.js +90 -1
- package/clis/douyin/hashtag.js +84 -23
- package/clis/douyin/hashtag.test.js +113 -0
- package/clis/douyin/publish-upload-id.test.js +170 -0
- package/clis/douyin/publish.js +88 -42
- package/clis/douyin/user-videos.js +9 -2
- package/clis/douyin/user-videos.test.js +43 -0
- package/clis/flomo/memos.js +228 -0
- package/clis/flomo/memos.test.js +144 -0
- package/clis/geogebra/add-circle.js +46 -0
- package/clis/geogebra/add-line.js +35 -0
- package/clis/geogebra/add-point.js +27 -0
- package/clis/geogebra/add-polygon.js +25 -0
- package/clis/geogebra/eval.js +35 -0
- package/clis/geogebra/geogebra.test.js +175 -0
- package/clis/geogebra/hexagon.js +62 -0
- package/clis/geogebra/info.js +72 -0
- package/clis/geogebra/list.js +35 -0
- package/clis/geogebra/triangle.js +60 -0
- package/clis/geogebra/utils.js +271 -0
- package/clis/gitee/search.js +2 -2
- package/clis/gitee/search.test.js +65 -0
- package/clis/jike/post.js +27 -17
- package/clis/jike/read.test.js +86 -0
- package/clis/jike/topic.js +32 -19
- package/clis/jike/user.js +33 -20
- package/clis/jira/attachments.js +28 -0
- package/clis/jira/commands.test.js +287 -0
- package/clis/jira/comments.js +28 -0
- package/clis/jira/issue.js +28 -0
- package/clis/jira/links.js +28 -0
- package/clis/jira/search.js +47 -0
- package/clis/jira/shared.js +256 -0
- package/clis/lesswrong/comments.js +1 -1
- package/clis/lesswrong/curated.js +1 -1
- package/clis/lesswrong/frontpage.js +1 -1
- package/clis/lesswrong/frontpage.test.js +37 -0
- package/clis/lesswrong/new.js +1 -1
- package/clis/lesswrong/read.js +1 -1
- package/clis/lesswrong/sequences.js +1 -1
- package/clis/lesswrong/shortform.js +1 -1
- package/clis/lesswrong/tag.js +1 -1
- package/clis/lesswrong/top-month.js +1 -1
- package/clis/lesswrong/top-week.js +1 -1
- package/clis/lesswrong/top-year.js +1 -1
- package/clis/lesswrong/top.js +1 -1
- package/clis/linkedin/connect.js +401 -0
- package/clis/linkedin/connect.test.js +213 -0
- package/clis/linkedin/inbox.js +234 -0
- package/clis/linkedin/inbox.test.js +152 -0
- package/clis/linkedin/job-detail.js +167 -0
- package/clis/linkedin/job-detail.test.js +38 -0
- package/clis/linkedin/jobs-preferences.js +113 -0
- package/clis/linkedin/jobs-preferences.test.js +43 -0
- package/clis/linkedin/people-search.js +262 -0
- package/clis/linkedin/people-search.test.js +216 -0
- package/clis/linkedin/post-analytics.js +74 -0
- package/clis/linkedin/post-analytics.test.js +40 -0
- package/clis/linkedin/posts-core.js +241 -0
- package/clis/linkedin/posts.js +22 -0
- package/clis/linkedin/posts.test.js +40 -0
- package/clis/linkedin/profile-analytics.js +104 -0
- package/clis/linkedin/profile-analytics.test.js +67 -0
- package/clis/linkedin/profile-experience.js +671 -0
- package/clis/linkedin/profile-experience.test.js +152 -0
- package/clis/linkedin/profile-projects.js +311 -0
- package/clis/linkedin/profile-projects.test.js +111 -0
- package/clis/linkedin/profile-read.js +148 -0
- package/clis/linkedin/profile-read.test.js +77 -0
- package/clis/linkedin/safe-send.js +357 -0
- package/clis/linkedin/safe-send.test.js +204 -0
- package/clis/linkedin/salesnav-inbox.js +210 -0
- package/clis/linkedin/salesnav-inbox.test.js +113 -0
- package/clis/linkedin/salesnav-message.js +360 -0
- package/clis/linkedin/salesnav-message.test.js +172 -0
- package/clis/linkedin/salesnav-search.js +186 -0
- package/clis/linkedin/salesnav-search.test.js +76 -0
- package/clis/linkedin/salesnav-thread.js +212 -0
- package/clis/linkedin/salesnav-thread.test.js +79 -0
- package/clis/linkedin/sent-invitations.js +92 -0
- package/clis/linkedin/sent-invitations.test.js +62 -0
- package/clis/linkedin/services-read.js +213 -0
- package/clis/linkedin/services-read.test.js +105 -0
- package/clis/linkedin/shared.js +124 -0
- package/clis/linkedin/thread-snapshot.js +214 -0
- package/clis/linkedin/thread-snapshot.test.js +89 -0
- package/clis/linkedin/timeline.js +14 -7
- package/clis/linkedin-learning/course.js +138 -0
- package/clis/linkedin-learning/course.test.js +114 -0
- package/clis/linkedin-learning/search.js +155 -0
- package/clis/linkedin-learning/search.test.js +144 -0
- package/clis/linkedin-learning/trending.js +133 -0
- package/clis/linkedin-learning/trending.test.js +123 -0
- package/clis/notebooklm/add-source.js +269 -0
- package/clis/notebooklm/add-source.test.js +97 -0
- package/clis/notebooklm/create.js +76 -0
- package/clis/notebooklm/create.test.js +58 -0
- package/clis/notebooklm/generate-audio.js +91 -0
- package/clis/notebooklm/generate-audio.test.js +63 -0
- package/clis/notebooklm/generate-slides.js +106 -0
- package/clis/notebooklm/generate-slides.test.js +75 -0
- package/clis/notebooklm/open.test.js +10 -10
- package/clis/notebooklm/rpc.js +20 -6
- package/clis/notebooklm/rpc.test.js +27 -1
- package/clis/notebooklm/utils.js +100 -24
- package/clis/notebooklm/utils.test.js +60 -1
- package/clis/notebooklm/write-note.js +103 -0
- package/clis/notebooklm/write-note.test.js +70 -0
- package/clis/pixiv/detail.js +41 -34
- package/clis/pixiv/detail.test.js +93 -0
- package/clis/pixiv/user.js +36 -31
- package/clis/pixiv/user.test.js +100 -0
- package/clis/pixiv/utils.js +56 -7
- package/clis/powerchina/search.js +3 -3
- package/clis/powerchina/search.test.js +27 -1
- package/clis/reddit/extract-media.test.js +149 -0
- package/clis/reddit/frontpage.js +47 -9
- package/clis/reddit/frontpage.test.js +34 -0
- package/clis/reddit/home.js +31 -1
- package/clis/reddit/home.test.js +46 -3
- package/clis/reddit/hot.js +32 -1
- package/clis/reddit/hot.test.js +15 -1
- package/clis/reddit/popular.js +39 -1
- package/clis/reddit/popular.test.js +26 -0
- package/clis/reddit/saved.js +1 -1
- package/clis/reddit/search.js +38 -1
- package/clis/reddit/search.test.js +26 -0
- package/clis/reddit/subreddit.js +52 -7
- package/clis/reddit/subreddit.test.js +31 -0
- package/clis/reddit/subscribed.js +165 -0
- package/clis/reddit/subscribed.test.js +168 -0
- package/clis/reddit/upvoted.js +1 -1
- package/clis/suno/commands.test.js +188 -0
- package/clis/suno/download.js +140 -0
- package/clis/suno/download.test.js +151 -0
- package/clis/suno/generate.js +231 -0
- package/clis/suno/generate.test.js +252 -0
- package/clis/suno/list.js +79 -0
- package/clis/suno/status.js +63 -0
- package/clis/suno/utils.js +549 -0
- package/clis/suno/utils.test.js +329 -0
- package/clis/twitter/device-follow.js +193 -0
- package/clis/twitter/device-follow.test.js +287 -0
- package/clis/twitter/download.js +443 -73
- package/clis/twitter/download.test.js +457 -0
- package/clis/twitter/followers.js +6 -2
- package/clis/twitter/followers.test.js +19 -1
- package/clis/twitter/following.js +14 -5
- package/clis/twitter/following.test.js +29 -0
- package/clis/twitter/likes.js +12 -4
- package/clis/twitter/likes.test.js +26 -1
- package/clis/twitter/list-add.js +1 -1
- package/clis/twitter/list-create.js +155 -0
- package/clis/twitter/list-create.test.js +169 -0
- package/clis/twitter/list-remove.js +13 -6
- package/clis/twitter/list-remove.test.js +74 -0
- package/clis/twitter/list-tweets.js +6 -2
- package/clis/twitter/list-tweets.test.js +41 -1
- package/clis/twitter/lists.js +31 -4
- package/clis/twitter/lists.test.js +152 -16
- package/clis/twitter/notifications.js +4 -4
- package/clis/twitter/post.js +62 -4
- package/clis/twitter/post.test.js +35 -3
- package/clis/twitter/profile.js +81 -28
- package/clis/twitter/profile.test.js +113 -2
- package/clis/twitter/quote.js +9 -4
- package/clis/twitter/reply.js +13 -10
- package/clis/twitter/reply.test.js +41 -0
- package/clis/twitter/search.js +7 -3
- package/clis/twitter/search.test.js +41 -0
- package/clis/twitter/shared.js +155 -0
- package/clis/twitter/shared.test.js +465 -1
- package/clis/twitter/thread.js +10 -2
- package/clis/twitter/thread.test.js +58 -0
- package/clis/twitter/timeline.js +6 -2
- package/clis/twitter/timeline.test.js +2 -0
- package/clis/twitter/tweets.js +3 -2
- package/clis/twitter/tweets.test.js +1 -1
- package/clis/twitter/utils.js +53 -16
- package/clis/upwork/detail.js +132 -0
- package/clis/upwork/feed.js +109 -0
- package/clis/upwork/search.js +115 -0
- package/clis/upwork/upwork.test.js +566 -0
- package/clis/upwork/utils.js +323 -0
- package/clis/weibo/delete.js +172 -0
- package/clis/weibo/delete.test.js +94 -0
- package/clis/weibo/publish.js +37 -14
- package/clis/weibo/publish.test.js +14 -5
- package/clis/weibo/user-posts.js +234 -0
- package/clis/weibo/user-posts.test.js +92 -0
- package/clis/weread/book-search.js +438 -0
- package/clis/weread/book-search.test.js +242 -0
- package/clis/weread/search-regression.test.js +98 -11
- package/clis/weread/search.js +32 -9
- package/clis/weread-official/book.js +135 -0
- package/clis/weread-official/commands.test.js +385 -0
- package/clis/weread-official/discover.js +107 -0
- package/clis/weread-official/list-apis.js +95 -0
- package/clis/weread-official/notes.js +171 -0
- package/clis/weread-official/readdata.js +158 -0
- package/clis/weread-official/review.js +93 -0
- package/clis/weread-official/search.js +106 -0
- package/clis/weread-official/shelf.js +97 -0
- package/clis/weread-official/utils.js +293 -0
- package/clis/weread-official/utils.test.js +242 -0
- package/clis/wikipedia/trending.js +7 -3
- package/clis/wikipedia/trending.test.js +57 -0
- package/clis/xianyu/chat.js +24 -109
- package/clis/xianyu/chat.test.js +5 -0
- package/clis/xianyu/im.js +322 -0
- package/clis/xianyu/im.test.js +253 -0
- package/clis/xianyu/inbox.js +96 -0
- package/clis/xianyu/messages.js +91 -0
- package/clis/xianyu/reply.js +82 -0
- package/clis/xiaohongshu/creator-note-detail.js +166 -28
- package/clis/xiaohongshu/creator-note-detail.test.js +196 -36
- package/clis/xiaohongshu/creator-notes-summary.js +2 -1
- package/clis/xiaohongshu/creator-notes-summary.test.js +7 -0
- package/clis/xiaohongshu/creator-notes.js +252 -2
- package/clis/xiaohongshu/creator-notes.test.js +90 -1
- package/clis/xiaohongshu/creator-stats.js +2 -1
- package/clis/xiaohongshu/creator-stats.test.js +24 -0
- package/clis/xiaohongshu/delete-note.js +260 -0
- package/clis/xiaohongshu/delete-note.test.js +172 -0
- package/clis/xiaohongshu/download.js +97 -39
- package/clis/xiaohongshu/download.test.js +201 -0
- package/clis/xiaohongshu/publish.js +48 -8
- package/clis/xiaohongshu/publish.test.js +65 -10
- package/clis/xiaohongshu/user-helpers.test.js +41 -0
- package/clis/xiaohongshu/user.js +27 -4
- package/clis/xiaoyuzhou/download.js +1 -1
- package/clis/xiaoyuzhou/transcript.js +1 -1
- package/clis/youdao/note.js +258 -0
- package/clis/youdao/note.test.js +99 -0
- package/clis/youtube/transcript.js +397 -24
- package/clis/youtube/transcript.test.js +196 -6
- package/clis/zhihu/answer-comments.js +280 -0
- package/clis/zhihu/answer-comments.test.js +287 -0
- package/clis/zhihu/answer-detail.js +2 -19
- package/clis/zhihu/answer-detail.test.js +8 -0
- package/clis/zhihu/collection.js +17 -16
- package/clis/zhihu/collection.test.js +50 -3
- package/clis/zhihu/download.js +1 -1
- package/clis/zhihu/question.js +42 -17
- package/clis/zhihu/question.test.js +113 -11
- package/clis/zhihu/search.js +195 -43
- package/clis/zhihu/search.test.js +198 -0
- package/clis/zhihu/text.js +29 -0
- package/clis/zhihu/text.test.js +24 -0
- package/dist/src/browser/errors.js +4 -2
- package/dist/src/browser/errors.test.js +6 -0
- package/dist/src/browser/network-cache.js +13 -1
- package/dist/src/browser/network-cache.test.js +17 -0
- package/dist/src/browser/page.js +30 -4
- package/dist/src/browser/page.test.js +42 -0
- package/dist/src/browser/utils.d.ts +1 -1
- package/dist/src/cli-argv-preprocess.d.ts +26 -0
- package/dist/src/cli-argv-preprocess.js +138 -0
- package/dist/src/cli-argv-preprocess.test.js +79 -0
- package/dist/src/convention-audit.js +15 -8
- package/dist/src/convention-audit.test.js +21 -0
- package/dist/src/download/index.js +13 -1
- package/dist/src/download/index.test.js +23 -1
- package/dist/src/download/media-download.js +15 -2
- package/dist/src/download/media-download.test.d.ts +1 -0
- package/dist/src/download/media-download.test.js +112 -0
- package/dist/src/download/progress.js +2 -2
- package/dist/src/download/progress.test.js +12 -1
- package/dist/src/electron-apps.js +1 -1
- package/dist/src/electron-apps.test.js +7 -2
- package/dist/src/errors.d.ts +17 -0
- package/dist/src/errors.js +22 -0
- package/dist/src/external-clis.yaml +8 -0
- package/dist/src/main.js +14 -2
- package/dist/src/output.js +11 -1
- package/dist/src/output.test.js +6 -0
- package/dist/src/registry.js +1 -0
- package/dist/src/registry.test.js +11 -0
- package/dist/src/utils.d.ts +43 -0
- package/dist/src/utils.js +97 -0
- package/dist/src/utils.test.d.ts +1 -0
- package/dist/src/utils.test.js +155 -0
- package/package.json +8 -2
- package/scripts/silent-column-drop-baseline.json +0 -52
- package/scripts/typed-error-lint-baseline.json +28 -380
- package/clis/slock/_utils.js +0 -12
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { getRegistry } from '@jackwener/opencli/registry';
|
|
3
|
+
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
4
|
+
import './answer-comments.js';
|
|
5
|
+
import { __test__ as helpers } from './answer-comments.js';
|
|
6
|
+
|
|
7
|
+
describe('zhihu answer-comments', () => {
|
|
8
|
+
it('registers as a cookie read command', () => {
|
|
9
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
10
|
+
expect(cmd).toBeDefined();
|
|
11
|
+
expect(cmd.access).toBe('read');
|
|
12
|
+
expect(cmd.strategy).toBe('cookie');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('returns flattened comments while limiting only top-level comments', async () => {
|
|
16
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
17
|
+
const goto = vi.fn().mockResolvedValue(undefined);
|
|
18
|
+
const evaluate = vi.fn().mockImplementation(async (js) => {
|
|
19
|
+
expect(js).toContain('/api/v4/answers/2036567240334653053/comments?order=normal&limit=20');
|
|
20
|
+
expect(js).toContain("credentials: 'include'");
|
|
21
|
+
return {
|
|
22
|
+
data: [
|
|
23
|
+
{
|
|
24
|
+
id: 'c1',
|
|
25
|
+
author: { member: { id: 'u1', name: 'alice' } },
|
|
26
|
+
vote_count: 3,
|
|
27
|
+
created_time: 1700000000,
|
|
28
|
+
content: '<p>top "one"</p>',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'r1',
|
|
32
|
+
author: { member: { id: 'u2', name: 'bob' } },
|
|
33
|
+
reply_to_author: { member: { id: 'u1', name: 'alice' } },
|
|
34
|
+
vote_count: 1,
|
|
35
|
+
created_time: 1700000100,
|
|
36
|
+
content: '<p>reply one</p>',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: 'r2',
|
|
40
|
+
author: { member: { id: 'u3', name: 'carol' } },
|
|
41
|
+
reply_to_author: { member: { id: 'u1', name: 'alice' } },
|
|
42
|
+
vote_count: 2,
|
|
43
|
+
created_time: 1700000200,
|
|
44
|
+
content: '<p>reply two should be capped</p>',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: 'c2',
|
|
48
|
+
author: { member: { id: 'u4', name: 'dave' } },
|
|
49
|
+
vote_count: 4,
|
|
50
|
+
created_time: 1700000300,
|
|
51
|
+
content: '<p>top two</p>',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: 'c3',
|
|
55
|
+
author: { member: { id: 'u5', name: 'erin' } },
|
|
56
|
+
vote_count: 5,
|
|
57
|
+
content: '<p>top three should stop the page walk</p>',
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
paging: { is_end: false, next: 'https://www.zhihu.com/api/v4/answers/2036567240334653053/comments?offset=20' },
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
const page = {
|
|
64
|
+
goto,
|
|
65
|
+
getCurrentUrl: vi.fn().mockResolvedValue('https://www.zhihu.com/question/2022852734622114542/answer/2036567240334653053'),
|
|
66
|
+
evaluate,
|
|
67
|
+
};
|
|
68
|
+
await expect(cmd.func(page, {
|
|
69
|
+
id: 'https://www.zhihu.com/question/2022852734622114542/answer/2036567240334653053',
|
|
70
|
+
limit: 2,
|
|
71
|
+
'replies-limit': 1,
|
|
72
|
+
})).resolves.toEqual([
|
|
73
|
+
{
|
|
74
|
+
rank: 1,
|
|
75
|
+
comment_rank: 1,
|
|
76
|
+
reply_rank: 0,
|
|
77
|
+
depth: 0,
|
|
78
|
+
id: 'c1',
|
|
79
|
+
parent_id: '',
|
|
80
|
+
author: 'alice',
|
|
81
|
+
reply_to: '',
|
|
82
|
+
likes: 3,
|
|
83
|
+
created_at: '2023-11-14T22:13:20.000Z',
|
|
84
|
+
url: 'https://www.zhihu.com/question/2022852734622114542/answer/2036567240334653053#comment-c1',
|
|
85
|
+
content: 'top "one"',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
rank: 2,
|
|
89
|
+
comment_rank: 1,
|
|
90
|
+
reply_rank: 1,
|
|
91
|
+
depth: 0,
|
|
92
|
+
id: 'r1',
|
|
93
|
+
parent_id: '',
|
|
94
|
+
author: 'bob',
|
|
95
|
+
reply_to: 'alice',
|
|
96
|
+
likes: 1,
|
|
97
|
+
created_at: '2023-11-14T22:15:00.000Z',
|
|
98
|
+
url: 'https://www.zhihu.com/question/2022852734622114542/answer/2036567240334653053#comment-r1',
|
|
99
|
+
content: 'reply one',
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
rank: 3,
|
|
103
|
+
comment_rank: 2,
|
|
104
|
+
reply_rank: 0,
|
|
105
|
+
depth: 0,
|
|
106
|
+
id: 'c2',
|
|
107
|
+
parent_id: '',
|
|
108
|
+
author: 'dave',
|
|
109
|
+
reply_to: '',
|
|
110
|
+
likes: 4,
|
|
111
|
+
created_at: '2023-11-14T22:18:20.000Z',
|
|
112
|
+
url: 'https://www.zhihu.com/question/2022852734622114542/answer/2036567240334653053#comment-c2',
|
|
113
|
+
content: 'top two',
|
|
114
|
+
},
|
|
115
|
+
]);
|
|
116
|
+
expect(goto).toHaveBeenCalledWith('https://www.zhihu.com/answer/2036567240334653053');
|
|
117
|
+
expect(evaluate).toHaveBeenCalledTimes(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('follows paging.next until enough top-level comments are collected', async () => {
|
|
121
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
122
|
+
const evaluate = vi.fn()
|
|
123
|
+
.mockResolvedValueOnce({
|
|
124
|
+
data: [
|
|
125
|
+
{ id: 'c1', author: { member: { id: 'u1', name: 'alice' } }, content: 'first' },
|
|
126
|
+
{ id: 'r1', author: { member: { id: 'u2', name: 'bob' } }, reply_to_author: { member: { id: 'u1', name: 'alice' } }, content: 'reply' },
|
|
127
|
+
],
|
|
128
|
+
paging: { is_end: false, next: 'https://www.zhihu.com/api/v4/answers/1/comments?offset=20' },
|
|
129
|
+
})
|
|
130
|
+
.mockResolvedValueOnce({
|
|
131
|
+
data: [
|
|
132
|
+
{ id: 'c2', author: { member: { id: 'u3', name: 'carol' } }, content: 'second' },
|
|
133
|
+
],
|
|
134
|
+
paging: { is_end: true },
|
|
135
|
+
});
|
|
136
|
+
const page = { goto: vi.fn().mockResolvedValue(undefined), evaluate };
|
|
137
|
+
const rows = await cmd.func(page, { id: '1', limit: 2, 'replies-limit': 0 });
|
|
138
|
+
expect(rows.map((row) => row.id)).toEqual(['c1', 'c2']);
|
|
139
|
+
expect(evaluate).toHaveBeenCalledTimes(2);
|
|
140
|
+
expect(evaluate.mock.calls[1][0]).toContain('offset=20');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('supports typed answer targets', async () => {
|
|
144
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
145
|
+
const page = {
|
|
146
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
147
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
148
|
+
data: [
|
|
149
|
+
{ id: 'c1', author: { member: { id: 'u1', name: 'alice' } }, content: 'typed target comment' },
|
|
150
|
+
],
|
|
151
|
+
paging: { is_end: true },
|
|
152
|
+
}),
|
|
153
|
+
};
|
|
154
|
+
await expect(cmd.func(page, { id: 'answer:2022852734622114542:2036567240334653053', limit: 1, 'replies-limit': 0 }))
|
|
155
|
+
.resolves.toMatchObject([{ id: 'c1', url: 'https://www.zhihu.com/question/2022852734622114542/answer/2036567240334653053#comment-c1' }]);
|
|
156
|
+
expect(page.goto).toHaveBeenCalledWith('https://www.zhihu.com/answer/2036567240334653053');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('maps auth failures to AuthRequiredError', async () => {
|
|
160
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
161
|
+
const page = {
|
|
162
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
163
|
+
evaluate: vi.fn().mockResolvedValue({ __httpError: 403 }),
|
|
164
|
+
};
|
|
165
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': 0 })).rejects.toBeInstanceOf(AuthRequiredError);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('maps 404 not found to EmptyResultError', async () => {
|
|
169
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
170
|
+
const page = {
|
|
171
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
172
|
+
evaluate: vi.fn().mockResolvedValue({ __httpError: 404 }),
|
|
173
|
+
};
|
|
174
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': 0 })).rejects.toBeInstanceOf(EmptyResultError);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('maps malformed responses to CommandExecutionError', async () => {
|
|
178
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
179
|
+
const page = {
|
|
180
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
181
|
+
evaluate: vi.fn().mockResolvedValue({ data: {} }),
|
|
182
|
+
};
|
|
183
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': 0 })).rejects.toBeInstanceOf(CommandExecutionError);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('maps valid empty comments to EmptyResultError', async () => {
|
|
187
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
188
|
+
const page = {
|
|
189
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
190
|
+
evaluate: vi.fn().mockResolvedValue({ data: [], paging: { is_end: true } }),
|
|
191
|
+
};
|
|
192
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': 0 })).rejects.toBeInstanceOf(EmptyResultError);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('rejects malformed pagination next URLs and repeated next URLs', async () => {
|
|
196
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
197
|
+
const malformedNextPage = {
|
|
198
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
199
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
200
|
+
data: [{ id: 'c1', author: { member: { id: 'u1', name: 'alice' } }, content: 'first' }],
|
|
201
|
+
paging: { is_end: false, next: 'https://evil.example/api/v4/answers/1/comments?offset=20' },
|
|
202
|
+
}),
|
|
203
|
+
};
|
|
204
|
+
await expect(cmd.func(malformedNextPage, { id: '1', limit: 2, 'replies-limit': 0 }))
|
|
205
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
206
|
+
|
|
207
|
+
const repeatedNextPage = {
|
|
208
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
209
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
210
|
+
data: [{ id: 'c1', author: { member: { id: 'u1', name: 'alice' } }, content: 'first' }],
|
|
211
|
+
paging: { is_end: false, next: 'https://www.zhihu.com/api/v4/answers/1/comments?order=normal&limit=20&offset=0&status=open' },
|
|
212
|
+
}),
|
|
213
|
+
};
|
|
214
|
+
await expect(cmd.func(repeatedNextPage, { id: '1', limit: 2, 'replies-limit': 0 }))
|
|
215
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('rejects comment rows without stable comment id anchors', async () => {
|
|
219
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
220
|
+
const page = {
|
|
221
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
222
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
223
|
+
data: [{ author: { member: { id: 'u1', name: 'alice' } }, content: 'missing id' }],
|
|
224
|
+
paging: { is_end: true },
|
|
225
|
+
}),
|
|
226
|
+
};
|
|
227
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': 0 })).rejects.toBeInstanceOf(CommandExecutionError);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('rejects null comment items and non-primitive comment ids', async () => {
|
|
231
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
232
|
+
const basePage = (data) => ({
|
|
233
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
234
|
+
evaluate: vi.fn().mockResolvedValue({ data, paging: { is_end: true } }),
|
|
235
|
+
});
|
|
236
|
+
await expect(cmd.func(basePage([null]), { id: '1', limit: 1, 'replies-limit': 0 }))
|
|
237
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
238
|
+
await expect(cmd.func(basePage([{ id: { value: 'c1' }, content: 'object id' }]), { id: '1', limit: 1, 'replies-limit': 0 }))
|
|
239
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
240
|
+
await expect(cmd.func(basePage([{ id: true, content: 'boolean id' }]), { id: '1', limit: 1, 'replies-limit': 0 }))
|
|
241
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('rejects invalid inputs before navigation', async () => {
|
|
245
|
+
const cmd = getRegistry().get('zhihu/answer-comments');
|
|
246
|
+
const page = { goto: vi.fn(), evaluate: vi.fn() };
|
|
247
|
+
await expect(cmd.func(page, { id: 'not-an-answer', limit: 1, 'replies-limit': 0 })).rejects.toBeInstanceOf(ArgumentError);
|
|
248
|
+
await expect(cmd.func(page, { id: '1', limit: 0, 'replies-limit': 0 })).rejects.toBeInstanceOf(ArgumentError);
|
|
249
|
+
await expect(cmd.func(page, { id: '1', limit: 1001, 'replies-limit': 0 })).rejects.toBeInstanceOf(ArgumentError);
|
|
250
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': -1 })).rejects.toBeInstanceOf(ArgumentError);
|
|
251
|
+
await expect(cmd.func(page, { id: '1', limit: 1, 'replies-limit': 101 })).rejects.toBeInstanceOf(ArgumentError);
|
|
252
|
+
expect(page.goto).not.toHaveBeenCalled();
|
|
253
|
+
expect(page.evaluate).not.toHaveBeenCalled();
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe('zhihu answer-comments helpers', () => {
|
|
258
|
+
it('parseAnswerTarget handles exact input shapes', () => {
|
|
259
|
+
expect(helpers.parseAnswerTarget('123')).toEqual({ answerId: '123', questionId: '' });
|
|
260
|
+
expect(helpers.parseAnswerTarget('answer:10:123')).toEqual({ answerId: '123', questionId: '10' });
|
|
261
|
+
expect(helpers.parseAnswerTarget('https://www.zhihu.com/question/10/answer/123')).toEqual({ answerId: '123', questionId: '10' });
|
|
262
|
+
expect(helpers.parseAnswerTarget('https://zhihu.com/answer/123?utm=1')).toEqual({ answerId: '123', questionId: '' });
|
|
263
|
+
expect(helpers.parseAnswerTarget('https://example.com/question/10/answer/123')).toBeNull();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('normalizeCommentsApiUrl only accepts same-answer Zhihu comments API URLs', () => {
|
|
267
|
+
expect(helpers.normalizeCommentsApiUrl('https://www.zhihu.com/api/v4/answers/123/comments?offset=20', '123'))
|
|
268
|
+
.toBe('https://www.zhihu.com/api/v4/answers/123/comments?offset=20');
|
|
269
|
+
expect(helpers.normalizeCommentsApiUrl('https://api.zhihu.com/answers/123/comments?offset=20', '123'))
|
|
270
|
+
.toBe('https://www.zhihu.com/api/v4/answers/123/comments?offset=20');
|
|
271
|
+
expect(helpers.normalizeCommentsApiUrl('https://www.zhihu.com/api/v4/answers/999/comments?offset=20', '123')).toBe('');
|
|
272
|
+
expect(helpers.normalizeCommentsApiUrl('https://evil.example/api/v4/answers/123/comments?offset=20', '123')).toBe('');
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('buildRows keeps replies flat without guessing parent comment ids', () => {
|
|
276
|
+
const rows = helpers.buildRows([
|
|
277
|
+
{ id: 'c1', author: { member: { id: 'u1', name: 'alice' } }, content: 'top' },
|
|
278
|
+
{ id: 'r1', author: { member: { id: 'u2', name: 'bob' } }, reply_to_author: { member: { id: 'u1', name: 'alice' } }, content: 'reply' },
|
|
279
|
+
{ id: 'r2', author: { member: { id: 'u3', name: 'carol' } }, reply_to_author: { member: { id: 'u2', name: 'bob' } }, content: 'nested' },
|
|
280
|
+
], { answerId: 'a1', questionId: 'q1', topLevelLimit: 1, repliesLimit: 5 }).rows;
|
|
281
|
+
expect(rows.map((row) => [row.id, row.parent_id, row.depth, row.comment_rank, row.reply_rank])).toEqual([
|
|
282
|
+
['c1', '', 0, 1, 0],
|
|
283
|
+
['r1', '', 0, 1, 1],
|
|
284
|
+
['r2', '', 0, 1, 2],
|
|
285
|
+
]);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
@@ -1,26 +1,9 @@
|
|
|
1
1
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
2
2
|
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
3
|
+
import { stripHtml as stripHtmlText } from './text.js';
|
|
3
4
|
|
|
4
|
-
// Light-weight HTML → text, preserving paragraph / heading / list-item
|
|
5
|
-
// line breaks. Zhihu answer `content` is HTML, so we map block-level
|
|
6
|
-
// closing tags + `<br>` to newlines before stripping the rest.
|
|
7
5
|
function stripHtml(html) {
|
|
8
|
-
|
|
9
|
-
return html
|
|
10
|
-
.replace(/<br\s*\/?\s*>/gi, '\n')
|
|
11
|
-
// Block-level closing tags become paragraph breaks (double
|
|
12
|
-
// newline) so the stripped text stays readable. The trailing
|
|
13
|
-
// `\n{3,}` collapse pass below normalizes accidental triples.
|
|
14
|
-
.replace(/<\/(?:p|div|h[1-6]|li|blockquote)>/gi, '\n\n')
|
|
15
|
-
.replace(/<[^>]+>/g, '')
|
|
16
|
-
.replace(/ /g, ' ')
|
|
17
|
-
.replace(/</g, '<')
|
|
18
|
-
.replace(/>/g, '>')
|
|
19
|
-
.replace(/&/g, '&')
|
|
20
|
-
.replace(/"/g, '"')
|
|
21
|
-
.replace(/'/g, "'")
|
|
22
|
-
.replace(/\n{3,}/g, '\n\n')
|
|
23
|
-
.trim();
|
|
6
|
+
return stripHtmlText(html, { preserveBlocks: true });
|
|
24
7
|
}
|
|
25
8
|
|
|
26
9
|
const ANSWER_ID_RE = /^\d+$/;
|
|
@@ -304,6 +304,14 @@ describe('zhihu answer-detail helpers', () => {
|
|
|
304
304
|
expect(out).toBe('hi there & you\n\nsecond');
|
|
305
305
|
});
|
|
306
306
|
|
|
307
|
+
it('stripHtml decodes numeric entities', () => {
|
|
308
|
+
expect(helpers.stripHtml('"中文" & 'test'')).toBe('"中文" & \'test\'');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('stripHtml keeps invalid numeric entities unchanged', () => {
|
|
312
|
+
expect(helpers.stripHtml('bad � entity')).toBe('bad � entity');
|
|
313
|
+
});
|
|
314
|
+
|
|
307
315
|
it('stripHtml maps <br> to single newline', () => {
|
|
308
316
|
expect(helpers.stripHtml('a<br>b<br/>c')).toBe('a\nb\nc');
|
|
309
317
|
});
|
package/clis/zhihu/collection.js
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
2
2
|
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
3
3
|
import { log } from '@jackwener/opencli/logger';
|
|
4
|
-
|
|
5
|
-
function stripHtml(html) {
|
|
6
|
-
return html
|
|
7
|
-
.replace(/<[^>]+>/g, '')
|
|
8
|
-
.replace(/ /g, ' ')
|
|
9
|
-
.replace(/</g, '<')
|
|
10
|
-
.replace(/>/g, '>')
|
|
11
|
-
.replace(/&/g, '&')
|
|
12
|
-
.replace(/"/g, '"')
|
|
13
|
-
.replace(/<em>/g, '')
|
|
14
|
-
.replace(/<\/em>/g, '')
|
|
15
|
-
.trim();
|
|
16
|
-
}
|
|
4
|
+
import { stripHtml } from './text.js';
|
|
17
5
|
|
|
18
6
|
function validatePositiveInt(value, name) {
|
|
19
7
|
const n = Number(value);
|
|
@@ -56,12 +44,18 @@ async function fetchCollectionPage(page, collectionId, offset, limit) {
|
|
|
56
44
|
|
|
57
45
|
function itemKey(item) {
|
|
58
46
|
const content = item?.content || {};
|
|
59
|
-
return `${content.type || '
|
|
47
|
+
return `${content.type || ''}:${content.id || content.url || JSON.stringify(content).slice(0, 80)}`;
|
|
60
48
|
}
|
|
61
49
|
|
|
62
50
|
function mapCollectionItem(item, rank) {
|
|
63
51
|
const content = item.content || {};
|
|
64
|
-
const type = content.type || '
|
|
52
|
+
const type = content.type || '';
|
|
53
|
+
if (!['answer', 'article', 'pin'].includes(type)) {
|
|
54
|
+
throw new CommandExecutionError(
|
|
55
|
+
`Zhihu collection returned unsupported content type: ${type || 'missing'}`,
|
|
56
|
+
'Collection items require a supported content.type so the row identity, title, and URL are not silently blank.',
|
|
57
|
+
);
|
|
58
|
+
}
|
|
65
59
|
|
|
66
60
|
let title = '';
|
|
67
61
|
let excerpt = '';
|
|
@@ -90,10 +84,17 @@ function mapCollectionItem(item, rank) {
|
|
|
90
84
|
votes = content.reaction_count || 0;
|
|
91
85
|
}
|
|
92
86
|
|
|
87
|
+
if (!String(title || '').trim() || !String(url || '').trim() || url.includes('undefined')) {
|
|
88
|
+
throw new CommandExecutionError(
|
|
89
|
+
'Zhihu collection returned a malformed item without title or URL identity',
|
|
90
|
+
'Collection item rows require type, title, and URL so malformed payloads do not become blank listing rows.',
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
93
94
|
return {
|
|
94
95
|
rank,
|
|
95
96
|
type,
|
|
96
|
-
title: title.substring(0, 100),
|
|
97
|
+
title: stripHtml(title).substring(0, 100),
|
|
97
98
|
author,
|
|
98
99
|
votes,
|
|
99
100
|
excerpt,
|
|
@@ -34,10 +34,10 @@ describe('zhihu collection', () => {
|
|
|
34
34
|
content: {
|
|
35
35
|
type: 'answer',
|
|
36
36
|
id: 123456,
|
|
37
|
-
question: { id: 789012, title: 'Test Question' },
|
|
37
|
+
question: { id: 789012, title: '"Test" & Question' },
|
|
38
38
|
author: { name: 'test_author' },
|
|
39
39
|
voteup_count: 42,
|
|
40
|
-
content: '<p
|
|
40
|
+
content: '<p>"Test" & answer content</p>',
|
|
41
41
|
url: 'https://www.zhihu.com/question/789012/answer/123456',
|
|
42
42
|
},
|
|
43
43
|
},
|
|
@@ -54,9 +54,10 @@ describe('zhihu collection', () => {
|
|
|
54
54
|
expect(result[0]).toMatchObject({
|
|
55
55
|
rank: 1,
|
|
56
56
|
type: 'answer',
|
|
57
|
-
title: 'Test Question',
|
|
57
|
+
title: '"Test" & Question',
|
|
58
58
|
author: 'test_author',
|
|
59
59
|
votes: 42,
|
|
60
|
+
excerpt: '"Test" & answer content',
|
|
60
61
|
url: 'https://www.zhihu.com/question/789012/answer/123456',
|
|
61
62
|
});
|
|
62
63
|
|
|
@@ -287,4 +288,50 @@ describe('zhihu collection', () => {
|
|
|
287
288
|
await expect(cmd.func(page, { id: '83283292', offset: 0, limit: 20 }))
|
|
288
289
|
.rejects.toBeInstanceOf(EmptyResultError);
|
|
289
290
|
});
|
|
291
|
+
|
|
292
|
+
it('fails typed for missing content.type instead of emitting a blank identity row', async () => {
|
|
293
|
+
const cmd = getRegistry().get('zhihu/collection');
|
|
294
|
+
const evaluate = vi.fn().mockResolvedValue({
|
|
295
|
+
data: [
|
|
296
|
+
{
|
|
297
|
+
content: {
|
|
298
|
+
id: 555,
|
|
299
|
+
question: { id: 666, title: 'No-type Question' },
|
|
300
|
+
author: { name: 'a' },
|
|
301
|
+
voteup_count: 1,
|
|
302
|
+
content: '<p>x</p>',
|
|
303
|
+
url: 'https://www.zhihu.com/question/666',
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
],
|
|
307
|
+
paging: { totals: 1 },
|
|
308
|
+
});
|
|
309
|
+
const page = { goto: vi.fn().mockResolvedValue(undefined), evaluate };
|
|
310
|
+
await expect(cmd.func(page, { id: '83283292', offset: 0, limit: 20 }))
|
|
311
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('fails typed for supported collection items missing title/url identity', async () => {
|
|
315
|
+
const cmd = getRegistry().get('zhihu/collection');
|
|
316
|
+
const page = {
|
|
317
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
318
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
319
|
+
data: [
|
|
320
|
+
{
|
|
321
|
+
content: {
|
|
322
|
+
type: 'answer',
|
|
323
|
+
id: 555,
|
|
324
|
+
question: { id: 666, title: '' },
|
|
325
|
+
author: { name: 'a' },
|
|
326
|
+
voteup_count: 1,
|
|
327
|
+
content: '<p>x</p>',
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
],
|
|
331
|
+
paging: { totals: 1 },
|
|
332
|
+
}),
|
|
333
|
+
};
|
|
334
|
+
await expect(cmd.func(page, { id: '83283292', offset: 0, limit: 20 }))
|
|
335
|
+
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
336
|
+
});
|
|
290
337
|
});
|
package/clis/zhihu/download.js
CHANGED
|
@@ -41,7 +41,7 @@ cli({
|
|
|
41
41
|
|
|
42
42
|
// Get author
|
|
43
43
|
const authorEl = document.querySelector('.AuthorInfo-name, .UserLink-link');
|
|
44
|
-
result.author = authorEl?.textContent?.trim() || '
|
|
44
|
+
result.author = authorEl?.textContent?.trim() || '';
|
|
45
45
|
|
|
46
46
|
// Get publish time
|
|
47
47
|
const timeEl = document.querySelector('.ContentItem-time, .Post-Time');
|
package/clis/zhihu/question.js
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
1
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
2
2
|
import { AuthRequiredError, CliError } from '@jackwener/opencli/errors';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
.
|
|
10
|
-
.
|
|
3
|
+
import { stripHtml } from './text.js';
|
|
4
|
+
|
|
5
|
+
function answerIdFromUrl(url) {
|
|
6
|
+
if (typeof url !== 'string') return '';
|
|
7
|
+
try {
|
|
8
|
+
const parsed = new URL(url);
|
|
9
|
+
if (parsed.hostname !== 'www.zhihu.com' && parsed.hostname !== 'zhihu.com') return '';
|
|
10
|
+
return parsed.pathname.match(/^\/question\/\d+\/answer\/(\d+)\/?$/)?.[1]
|
|
11
|
+
|| parsed.pathname.match(/^\/api\/v4\/answers\/(\d+)\/?$/)?.[1]
|
|
12
|
+
|| parsed.pathname.match(/^\/answer\/(\d+)\/?$/)?.[1]
|
|
13
|
+
|| '';
|
|
14
|
+
} catch {
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function answerId(item) {
|
|
20
|
+
const fromUrl = answerIdFromUrl(item.url);
|
|
21
|
+
if (fromUrl) return fromUrl;
|
|
22
|
+
if (typeof item.id === 'string' && /^\d+$/.test(item.id)) return item.id;
|
|
23
|
+
if (typeof item.id === 'number' && Number.isSafeInteger(item.id) && item.id > 0) return String(item.id);
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function answerDedupeKey(item) {
|
|
28
|
+
const id = answerId(item);
|
|
29
|
+
if (id) return `id:${id}`;
|
|
30
|
+
return `fallback:${item.author?.name || 'anonymous'}:${item.content || ''}`;
|
|
11
31
|
}
|
|
12
32
|
|
|
13
33
|
const MAX_LIMIT = 1000;
|
|
@@ -24,7 +44,7 @@ cli({
|
|
|
24
44
|
{ name: 'limit', type: 'int', default: 5, help: 'Number of answers (max 1000; use normal-sized requests)' },
|
|
25
45
|
{ name: 'sort', default: 'default', choices: ['default', 'created'], help: 'Answer order: default or created' },
|
|
26
46
|
],
|
|
27
|
-
columns: ['rank', 'author', 'votes', 'content'],
|
|
47
|
+
columns: ['rank', 'id', 'author', 'votes', 'url', 'content'],
|
|
28
48
|
func: async (page, kwargs) => {
|
|
29
49
|
const { id, limit = 5 } = kwargs;
|
|
30
50
|
const questionId = String(id);
|
|
@@ -48,7 +68,7 @@ cli({
|
|
|
48
68
|
// costs one over-fetched page worth of bandwidth and never silently
|
|
49
69
|
// clamps the user-requested count.
|
|
50
70
|
const ZHIHU_PAGE_SIZE = 20;
|
|
51
|
-
let url = `https://www.zhihu.com/api/v4/questions/${questionId}/answers?limit=${ZHIHU_PAGE_SIZE}&offset=0&sort_by=${sort}&include=data[*].content,voteup_count,comment_count,author`;
|
|
71
|
+
let url = `https://www.zhihu.com/api/v4/questions/${questionId}/answers?limit=${ZHIHU_PAGE_SIZE}&offset=0&sort_by=${sort}&include=data[*].content,url,voteup_count,comment_count,author`;
|
|
52
72
|
const answers = [];
|
|
53
73
|
const seen = new Set();
|
|
54
74
|
const visited = new Set();
|
|
@@ -69,7 +89,7 @@ cli({
|
|
|
69
89
|
throw new CliError('FETCH_ERROR', status ? `Zhihu question answers request failed (HTTP ${status})` : 'Zhihu question answers request failed', 'Try again later or rerun with -v for more detail');
|
|
70
90
|
}
|
|
71
91
|
for (const item of data.data || []) {
|
|
72
|
-
const key =
|
|
92
|
+
const key = answerDedupeKey(item);
|
|
73
93
|
if (seen.has(key)) continue;
|
|
74
94
|
seen.add(key);
|
|
75
95
|
answers.push(item);
|
|
@@ -78,11 +98,16 @@ cli({
|
|
|
78
98
|
if (data.paging?.is_end) break;
|
|
79
99
|
url = typeof data.paging?.next === 'string' ? data.paging.next : '';
|
|
80
100
|
}
|
|
81
|
-
return answers.map((item, i) =>
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
101
|
+
return answers.map((item, i) => {
|
|
102
|
+
const id = answerId(item);
|
|
103
|
+
return {
|
|
104
|
+
rank: i + 1,
|
|
105
|
+
id,
|
|
106
|
+
author: item.author?.name || 'anonymous',
|
|
107
|
+
votes: item.voteup_count || 0,
|
|
108
|
+
url: id ? `https://www.zhihu.com/question/${questionId}/answer/${id}` : '',
|
|
109
|
+
content: stripHtml(item.content || '').substring(0, 200),
|
|
110
|
+
};
|
|
111
|
+
});
|
|
87
112
|
},
|
|
88
113
|
});
|