diff --git a/src/utils/get/getComments.ts b/src/utils/get/getComments.ts index 36cd846..b68eeb9 100644 --- a/src/utils/get/getComments.ts +++ b/src/utils/get/getComments.ts @@ -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 diff --git a/src/utils/get/getProfile.ts b/src/utils/get/getProfile.ts index c6e3b0a..52e5333 100644 --- a/src/utils/get/getProfile.ts +++ b/src/utils/get/getProfile.ts @@ -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} @@ -24,7 +23,6 @@ import { SocksProxyAgent } from "socks-proxy-agent" export const StalkUser = ( username: string, - cookie?: any, postLimit?: number, proxy?: string ): Promise => @@ -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 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