fix: get videos & get comments limit

This commit is contained in:
Tobi Saputra 2025-03-03 20:55:35 +07:00
parent c7af6991c3
commit fcc542f462
2 changed files with 69 additions and 82 deletions

View File

@ -95,83 +95,75 @@ const parseComments = async (
) => {
const comments: Comments[] = []
let cursor: number = 0
let counter: number = 0
let count: number = 50
let total: number = 0
let hasMore: boolean = true
while (hasMore) {
for (let i = 0; i < count; i++) {}
const result = await requestComments(id, cursor, proxy)
// Check if the result has more comments &
if (result.has_more === 0) hasMore = false
// Check if the result has more comments
hasMore = result.has_more === 1
cursor = hasMore ? result.cursor : 0
result.comments?.forEach((v: any) => {
const comment = {
cid: v.cid,
text: v.text,
commentLanguage: v.comment_language,
createTime: v.create_time,
likeCount: v.digg_count,
isAuthorLiked: v.is_author_digged,
isCommentTranslatable: v.is_comment_translatable,
replyCommentTotal: v.reply_comment_total,
user: {
uid: v.user.uid,
avatarThumb: v.user.avatar_thumb.url_list,
nickname: v.user.nickname,
username: v.user.unique_id,
isVerified: v.user.custom_verify !== ""
} as User,
url: v.share_info?.url || "",
replyComment: []
}
if (result.comments) {
result.comments.forEach((v: any) => {
const comment = {
cid: v.cid,
text: v.text,
commentLanguage: v.comment_language,
createTime: v.create_time,
likeCount: v.digg_count,
isAuthorLiked: v.is_author_digged,
isCommentTranslatable: v.is_comment_translatable,
replyCommentTotal: v.reply_comment_total,
user: {
uid: v.user.uid,
avatarThumb: v.user.avatar_thumb.url_list,
nickname: v.user.nickname,
username: v.user.unique_id,
isVerified: v.user.custom_verify !== ""
} as User,
url: v.share_info?.url || "",
replyComment: []
}
if (v.reply_comment !== null) {
v.reply_comment.forEach((v: any) => {
comment.replyComment.push({
cid: v.cid,
text: v.text,
commentLanguage: v.comment_language,
createTime: v.create_time,
likeCount: v.digg_count,
isAuthorLiked: v.is_author_digged,
isCommentTranslatable: v.is_comment_translatable,
replyCommentTotal: v.reply_comment_total,
user: {
uid: v.user.uid,
avatarThumb: v.user.avatar_thumb.url_list,
nickname: v.user.nickname,
username: v.user.unique_id,
isVerified: v.user.custom_verify !== ""
} as User,
url: v.share_info?.url || "",
replyComment: []
if (v.reply_comment !== null) {
v.reply_comment.forEach((v: any) => {
comment.replyComment.push({
cid: v.cid,
text: v.text,
commentLanguage: v.comment_language,
createTime: v.create_time,
likeCount: v.digg_count,
isAuthorLiked: v.is_author_digged,
isCommentTranslatable: v.is_comment_translatable,
replyCommentTotal: v.reply_comment_total,
user: {
uid: v.user.uid,
avatarThumb: v.user.avatar_thumb.url_list,
nickname: v.user.nickname,
username: v.user.unique_id,
isVerified: v.user.custom_verify !== ""
} as User,
url: v.share_info?.url || "",
replyComment: []
})
total++
})
total++
})
}
total++
comments.push(comment)
})
// Check if the comments length is equal to the comment limit
if (commentLimit) {
let loopCount = Math.floor(commentLimit / 50)
if (counter >= loopCount) hasMore = false
break
}
total++
comments.push(comment)
})
}
hasMore = result.has_more === 1
cursor = result.has_more === 1 ? result.cursor : 0
counter++
// Check if we've reached the comment limit
if (commentLimit && comments.length >= commentLimit) {
hasMore = false
break
}
}
const response =
total > commentLimit ? comments.slice(0, commentLimit) : comments
const response = commentLimit ? comments.slice(0, commentLimit) : comments
return {
total: response.length,
comments: response

View File

@ -16,7 +16,6 @@ import { SocksProxyAgent } from "socks-proxy-agent"
/**
* Tiktok Stalk User
* @param {string} username - The username you want to stalk
* @param {object|string} cookie - Your Tiktok Cookie (optional)
* @param {number} postLimit - The limit of post you want to get (optional)
* @param {string} proxy - Your Proxy (optional)
* @returns {Promise<StalkResult>}
@ -24,7 +23,6 @@ import { SocksProxyAgent } from "socks-proxy-agent"
export const StalkUser = (
username: string,
cookie?: any,
postLimit?: number,
proxy?: string
): Promise<StalkResult> =>
@ -34,11 +32,7 @@ export const StalkUser = (
method: "GET",
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
cookie:
typeof cookie === "object"
? cookie.map((v: any) => `${v.name}=${v.value}`).join("; ")
: cookie
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
},
httpsAgent:
(proxy &&
@ -156,7 +150,7 @@ const parsePosts = async (
): Promise<Posts[]> => {
// Posts Result
let hasMore = true
let cursor: number | null = null
let cursor = 0
const posts: Posts[] = []
let counter = 0
while (hasMore) {
@ -169,7 +163,10 @@ const parsePosts = async (
}
// Validate
if (result === "") hasMore = false // No More Post
if (result === "") {
hasMore = false
break
}
result?.itemList?.forEach((v: any) => {
const author: AuthorPost = {
@ -243,18 +240,16 @@ const parsePosts = async (
}
})
// Restrict too many data requests
if (postLimit !== 0) {
let loopCount = Math.floor(postLimit / 30)
if (counter >= loopCount) {
hasMore = false
break
}
}
// Update hasMore and cursor for next iteration
hasMore = result.hasMore
cursor = hasMore ? result.cursor : null
counter++
// Check post limit if specified
if (postLimit && posts.length >= postLimit) {
hasMore = false
break
}
}
return postLimit ? posts.slice(0, postLimit) : posts