fix: the getUserPost error when the data exceeds 35
This commit is contained in:
parent
9bab4ad652
commit
259c9a1789
@ -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,
|
||||
|
||||
@ -61,18 +61,41 @@ const parseUserPosts = async (
|
||||
proxy?: string
|
||||
): Promise<Posts[]> => {
|
||||
// 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,9 +194,9 @@ const requestUserPosts = async (
|
||||
return retry(
|
||||
async (bail, attempt) => {
|
||||
try {
|
||||
const { data } = await Axios.get(
|
||||
`${_tiktokGetPosts(_getUserPostsParams())}`,
|
||||
{
|
||||
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",
|
||||
@ -187,8 +210,7 @@ const requestUserPosts = async (
|
||||
? new SocksProxyAgent(proxy)
|
||||
: undefined)) ||
|
||||
undefined
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
if (data === "") {
|
||||
throw new Error("Empty response")
|
||||
|
||||
@ -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("-------------------")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user