fix: request errors and returning undefined

This commit is contained in:
Tobi Saputra 2025-05-13 18:26:55 +07:00
parent cb64f0585a
commit 80b5cbdc1d
3 changed files with 216 additions and 155 deletions

View File

@ -15,6 +15,7 @@ import {
StatisticsAuthorLiked,
ImagesLiked
} from "../../types/get/getUserLiked"
import retry from "async-retry"
export const getUserLiked = (
username: string,
@ -215,7 +216,10 @@ const requestUserLiked = async (
url.searchParams.append("X-Bogus", xbogus)
const xttparams = Tiktok.generateXTTParams(url.searchParams.toString())
const { data } = await Axios.get(`${url.toString()}`, {
return await retry(
async (bail, attempt) => {
try {
const { data } = await Axios.get(url.toString(), {
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",
@ -231,6 +235,19 @@ const requestUserLiked = async (
: undefined)) ||
undefined
})
return data
} catch (error) {
if (attempt === 3) {
bail(error)
}
throw error
}
},
{
retries: 3,
minTimeout: 1000,
maxTimeout: 5000,
factor: 2
}
)
}

View File

@ -10,6 +10,7 @@ import { HttpsProxyAgent } from "https-proxy-agent"
import { SocksProxyAgent } from "socks-proxy-agent"
import { TiktokService } from "../../services/tiktokService"
import { StalkUser } from "../get/getProfile"
import retry from "async-retry"
export const getUserPosts = (
username: string,
@ -58,14 +59,17 @@ const parseUserPosts = async (
let cursor = 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
// Prevent missing response posts
for (let i = 0; i < 30; i++) {
result = await requestUserPosts(secUid, cursor, postLimit, proxy)
if (result !== "") break
}
result = await requestUserPosts(proxy, xttparams)
// Validate
if (result === "") {
@ -161,22 +165,19 @@ const parseUserPosts = async (
}
const requestUserPosts = async (
secUid: string,
cursor: number = 0,
count: number = 30,
proxy?: string
proxy?: string,
xttparams: string = ""
): Promise<any> => {
const Tiktok = new TiktokService()
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": Tiktok.generateXTTParams(
_xttParams(secUid, cursor, count)
)
"x-tt-params": xttparams
},
httpsAgent:
(proxy &&
@ -188,6 +189,27 @@ const requestUserPosts = async (
undefined
}
)
console.log(data)
return data
} catch (error) {
if (
error.response?.status === 400 ||
error.response?.data?.statusCode === 10201
) {
bail(new Error("Video not found!"))
return
}
throw error
}
},
{
retries: 3,
minTimeout: 1000,
maxTimeout: 5000,
factor: 2,
onRetry: (error, attempt) => {
console.log(`Retry attempt ${attempt} due to: ${error}`)
}
}
)
}

View File

@ -4,6 +4,7 @@ import { _liveSearchParams, _videoSearchParams } from "../../constants/params"
import { SocksProxyAgent } from "socks-proxy-agent"
import { HttpsProxyAgent } from "https-proxy-agent"
import { TiktokService } from "../../services/tiktokService"
import retry from "async-retry"
import {
TiktokVideoSearchResponse,
AuthorVideoSearch,
@ -36,35 +37,9 @@ export const SearchVideo = async (
})
}
const Tiktok = new TiktokService()
const url = new URL(
_tiktokSearchVideoFull(_videoSearchParams(keyword, page))
)
const signature = Tiktok.generateSignature(url)
url.searchParams.append("_signature", signature)
const xbogus = Tiktok.generateXBogus(url, signature)
url.searchParams.append("X-Bogus", xbogus)
try {
const data = await requestVideoSearch(keyword, page, cookie, proxy)
Axios(_tiktokSearchVideoFull(_videoSearchParams(keyword, page)), {
method: "GET",
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
cookie:
typeof cookie === "object"
? cookie.map((v: any) => `${v.name}=${v.value}`).join("; ")
: cookie
},
httpsAgent:
(proxy &&
(proxy.startsWith("http") || proxy.startsWith("https")
? new HttpsProxyAgent(proxy)
: proxy.startsWith("socks")
? new SocksProxyAgent(proxy)
: undefined)) ||
undefined
})
.then(({ data }) => {
// Cookie Invalid
if (data.status_code === 2483)
return resolve({ status: "error", message: "Invalid cookie!" })
@ -93,7 +68,7 @@ export const SearchVideo = async (
}
const stats: StatisticsVideoSearch = {
diggCount: v.stats.diggCount,
likeCount: v.stats.diggCount,
shareCount: v.stats.shareCount,
commentCount: v.stats.commentCount,
playCount: v.stats.playCount,
@ -147,8 +122,55 @@ export const SearchVideo = async (
page,
totalResults: result.length
})
})
.catch((e) => {
} catch (e) {
resolve({ status: "error", message: e.message })
}
})
})
const requestVideoSearch = async (
keyword: string,
page: number,
cookie: string | any[],
proxy?: string
): Promise<any> => {
return retry(
async (bail, attempt) => {
try {
const { data } = await Axios(
_tiktokSearchVideoFull(_videoSearchParams(keyword, page)),
{
method: "GET",
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
cookie:
typeof cookie === "object"
? cookie.map((v: any) => `${v.name}=${v.value}`).join("; ")
: cookie
},
httpsAgent:
(proxy &&
(proxy.startsWith("http") || proxy.startsWith("https")
? new HttpsProxyAgent(proxy)
: proxy.startsWith("socks")
? new SocksProxyAgent(proxy)
: undefined)) ||
undefined
}
)
return data
} catch (error) {
throw error
}
},
{
retries: 3,
minTimeout: 1000,
maxTimeout: 5000,
factor: 2,
onRetry: (error, attempt) => {
console.log(`Retry attempt ${attempt} due to: ${error}`)
}
}
)
}