Skip to content

Commit a69d1fa

Browse files
committed
fix: setup page limit
1 parent 04eb997 commit a69d1fa

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/api.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { CommentDetailsResponse, CommentSort, CommentType } from './types/api'
22
import { Reply } from './types/reply'
33

4+
// for performance and rate limit, we only fetch the first 20 pages
5+
const PAGE_LIMIT = 20
46
const replies: Reply[] = []
57

68
export async function fetchComments(videoId: string) {
7-
if (replies.length > 0) {
9+
if (replies.length > 0) {
810
return replies
911
}
1012

11-
const pageSize = 40
13+
const pageSize = 49
1214
let page = 1
1315

1416
while (true) {
@@ -36,7 +38,10 @@ export async function fetchComments(videoId: string) {
3638

3739
replies.push(...(data.data.replies ?? []))
3840

39-
if (page * pageSize >= data.data.page.count) {
41+
if (
42+
page * pageSize >=
43+
Math.min(PAGE_LIMIT * pageSize, data.data.page.count)
44+
) {
4045
break
4146
}
4247

0 commit comments

Comments
 (0)