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

View File

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