From 259c9a178996aee9bcd3d10a5f84dabbaf6f3dfb Mon Sep 17 00:00:00 2001 From: Tobi Saputra Date: Sat, 12 Jul 2025 18:06:32 +0700 Subject: [PATCH] fix: the getUserPost error when the data exceeds 35 --- src/constants/params.ts | 43 +++++++++++++++++++++ src/utils/get/getUserPosts.ts | 70 +++++++++++++++++++++++------------ test/userposts-test.ts | 3 +- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/constants/params.ts b/src/constants/params.ts index 35f0174..8e1257f 100644 --- a/src/constants/params.ts +++ b/src/constants/params.ts @@ -1,3 +1,4 @@ +import { count } from "console" import qs from "qs" /** Get Params */ @@ -38,6 +39,48 @@ export const _getUserPostsParams = () => { ) } +export const _getUserRepostsParams = ( + secUid: string, + cursor: number, + count: number +) => { + return qs.stringify({ + WebIdLastTime: 1743386313, + aid: 1988, + app_language: "en", + app_name: "tiktok_web", + browser_language: "en-US", + browser_name: "Mozilla", + browser_online: true, + browser_platform: "Win32", + browser_version: + "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0", + channel: "tiktok_web", + clientABVersions: "", + ...(count ? { count } : { count: 16 }), + coverFormat: 2, + ...(cursor ? { cursor } : { cursor: 0 }), + data_collection_enabled: true, + device_id: "7002566096994190854", + device_platform: "web_pc", + focus_state: true, + from_page: "user", + history_len: 12, + is_fullscreen: false, + is_page_visible: true, + language: "en", + os: "windows", + post_item_list_request_type: 0, + priority_region: "ID", + region: "ID", + screen_height: 1080, + screen_width: 1920, + secUid, + tz_name: "Asia/Jakarta", + webcast_language: "en" + }) +} + export const _getUserLikedParams = ( id: string, secUid: string, diff --git a/src/utils/get/getUserPosts.ts b/src/utils/get/getUserPosts.ts index 1e7d485..d5d56f3 100644 --- a/src/utils/get/getUserPosts.ts +++ b/src/utils/get/getUserPosts.ts @@ -61,18 +61,41 @@ const parseUserPosts = async ( proxy?: string ): Promise => { // Posts Result + let page = 1 let hasMore = true - let cursor = 0 + let responseCursor = 0 const posts: Posts[] = [] let counter = 0 const Tiktok = new TiktokService() - const xttparams = Tiktok.generateXTTParams( - _xttParams(secUid, cursor, postLimit) - ) while (hasMore) { let result: any | null = null + let xttparams = "" + let urlCursor = 0 + let urlCount = 0 + + if (page === 1) { + urlCount = 0 + urlCursor = 0 + xttparams = Tiktok.generateXTTParams(_xttParams(secUid, 0, 35)) + } else if (page === 2) { + urlCount = 35 + urlCursor = 0 + xttparams = Tiktok.generateXTTParams(_xttParams(secUid, 0, 30)) + } else if (page === 3) { + urlCount = 30 + urlCursor = 0 + xttparams = Tiktok.generateXTTParams( + _xttParams(secUid, responseCursor, 16) + ) + } else { + urlCount = 16 + urlCursor = responseCursor + xttparams = Tiktok.generateXTTParams( + _xttParams(secUid, responseCursor, 16) + ) + } // Prevent missing response posts result = await requestUserPosts(proxy, xttparams) @@ -149,9 +172,9 @@ const parseUserPosts = async ( } }) - // Update hasMore and cursor for next iteration hasMore = result.hasMore - cursor = hasMore ? result.cursor : null + responseCursor = hasMore ? result.cursor : 0 + page++ counter++ // Check post limit if specified @@ -171,24 +194,23 @@ const requestUserPosts = async ( return retry( async (bail, attempt) => { try { - const { data } = await Axios.get( - `${_tiktokGetPosts(_getUserPostsParams())}`, - { - headers: { - "user-agent": - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35", - "x-tt-params": xttparams - }, - httpsAgent: - (proxy && - (proxy.startsWith("http") || proxy.startsWith("https") - ? new HttpsProxyAgent(proxy) - : proxy.startsWith("socks") - ? new SocksProxyAgent(proxy) - : undefined)) || - undefined - } - ) + let urlParams = _getUserPostsParams() + + const { data } = await Axios.get(`${_tiktokGetPosts(urlParams)}`, { + headers: { + "user-agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35", + "x-tt-params": xttparams + }, + httpsAgent: + (proxy && + (proxy.startsWith("http") || proxy.startsWith("https") + ? new HttpsProxyAgent(proxy) + : proxy.startsWith("socks") + ? new SocksProxyAgent(proxy) + : undefined)) || + undefined + }) if (data === "") { throw new Error("Empty response") diff --git a/test/userposts-test.ts b/test/userposts-test.ts index 0083f2b..df6081f 100644 --- a/test/userposts-test.ts +++ b/test/userposts-test.ts @@ -5,7 +5,7 @@ async function testUserPosts() { try { const username = "Tobz2k19" // Change to a valid TikTok username const result = await Tiktok.GetUserPosts(username, { - postLimit: 5, + postLimit: 30, proxy: undefined }) if (result.status === "success" && result.result) { @@ -14,6 +14,7 @@ async function testUserPosts() { console.log("Posts Overview:") console.log("========================") console.log(`Total posts fetched: ${result.result.length}`) + result.result.forEach((post, index) => { console.log(`\nPost ${index + 1}:`) console.log("-------------------")