fix: the getUserPost error when the data exceeds 35

This commit is contained in:
Tobi Saputra 2025-07-12 18:06:32 +07:00
parent 9bab4ad652
commit 259c9a1789
3 changed files with 91 additions and 25 deletions

View File

@ -1,3 +1,4 @@
import { count } from "console"
import qs from "qs" import qs from "qs"
/** Get Params */ /** 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 = ( export const _getUserLikedParams = (
id: string, id: string,
secUid: string, secUid: string,

View File

@ -61,18 +61,41 @@ const parseUserPosts = async (
proxy?: string proxy?: string
): Promise<Posts[]> => { ): Promise<Posts[]> => {
// Posts Result // Posts Result
let page = 1
let hasMore = true let hasMore = true
let cursor = 0 let responseCursor = 0
const posts: Posts[] = [] const posts: Posts[] = []
let counter = 0 let counter = 0
const Tiktok = new TiktokService() const Tiktok = new TiktokService()
const xttparams = Tiktok.generateXTTParams(
_xttParams(secUid, cursor, postLimit)
)
while (hasMore) { while (hasMore) {
let result: any | null = null 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 // Prevent missing response posts
result = await requestUserPosts(proxy, xttparams) result = await requestUserPosts(proxy, xttparams)
@ -149,9 +172,9 @@ const parseUserPosts = async (
} }
}) })
// Update hasMore and cursor for next iteration
hasMore = result.hasMore hasMore = result.hasMore
cursor = hasMore ? result.cursor : null responseCursor = hasMore ? result.cursor : 0
page++
counter++ counter++
// Check post limit if specified // Check post limit if specified
@ -171,24 +194,23 @@ const requestUserPosts = async (
return retry( return retry(
async (bail, attempt) => { async (bail, attempt) => {
try { try {
const { data } = await Axios.get( let urlParams = _getUserPostsParams()
`${_tiktokGetPosts(_getUserPostsParams())}`,
{ const { data } = await Axios.get(`${_tiktokGetPosts(urlParams)}`, {
headers: { headers: {
"user-agent": "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", "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 "x-tt-params": xttparams
}, },
httpsAgent: httpsAgent:
(proxy && (proxy &&
(proxy.startsWith("http") || proxy.startsWith("https") (proxy.startsWith("http") || proxy.startsWith("https")
? new HttpsProxyAgent(proxy) ? new HttpsProxyAgent(proxy)
: proxy.startsWith("socks") : proxy.startsWith("socks")
? new SocksProxyAgent(proxy) ? new SocksProxyAgent(proxy)
: undefined)) || : undefined)) ||
undefined undefined
} })
)
if (data === "") { if (data === "") {
throw new Error("Empty response") throw new Error("Empty response")

View File

@ -5,7 +5,7 @@ async function testUserPosts() {
try { try {
const username = "Tobz2k19" // Change to a valid TikTok username const username = "Tobz2k19" // Change to a valid TikTok username
const result = await Tiktok.GetUserPosts(username, { const result = await Tiktok.GetUserPosts(username, {
postLimit: 5, postLimit: 30,
proxy: undefined proxy: undefined
}) })
if (result.status === "success" && result.result) { if (result.status === "success" && result.result) {
@ -14,6 +14,7 @@ async function testUserPosts() {
console.log("Posts Overview:") console.log("Posts Overview:")
console.log("========================") console.log("========================")
console.log(`Total posts fetched: ${result.result.length}`) console.log(`Total posts fetched: ${result.result.length}`)
result.result.forEach((post, index) => { result.result.forEach((post, index) => {
console.log(`\nPost ${index + 1}:`) console.log(`\nPost ${index + 1}:`)
console.log("-------------------") console.log("-------------------")