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, StatisticsAuthorLiked,
ImagesLiked ImagesLiked
} from "../../types/get/getUserLiked" } from "../../types/get/getUserLiked"
import retry from "async-retry"
export const getUserLiked = ( export const getUserLiked = (
username: string, username: string,
@ -215,7 +216,10 @@ const requestUserLiked = async (
url.searchParams.append("X-Bogus", xbogus) url.searchParams.append("X-Bogus", xbogus)
const xttparams = Tiktok.generateXTTParams(url.searchParams.toString()) 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: { 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",
@ -231,6 +235,19 @@ const requestUserLiked = async (
: undefined)) || : undefined)) ||
undefined undefined
}) })
return data 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 { SocksProxyAgent } from "socks-proxy-agent"
import { TiktokService } from "../../services/tiktokService" import { TiktokService } from "../../services/tiktokService"
import { StalkUser } from "../get/getProfile" import { StalkUser } from "../get/getProfile"
import retry from "async-retry"
export const getUserPosts = ( export const getUserPosts = (
username: string, username: string,
@ -58,14 +59,17 @@ const parseUserPosts = async (
let cursor = 0 let cursor = 0
const posts: Posts[] = [] const posts: Posts[] = []
let counter = 0 let counter = 0
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
// Prevent missing response posts // Prevent missing response posts
for (let i = 0; i < 30; i++) { result = await requestUserPosts(proxy, xttparams)
result = await requestUserPosts(secUid, cursor, postLimit, proxy)
if (result !== "") break
}
// Validate // Validate
if (result === "") { if (result === "") {
@ -161,22 +165,19 @@ const parseUserPosts = async (
} }
const requestUserPosts = async ( const requestUserPosts = async (
secUid: string, proxy?: string,
cursor: number = 0, xttparams: string = ""
count: number = 30,
proxy?: string
): Promise<any> => { ): Promise<any> => {
const Tiktok = new TiktokService() return retry(
async (bail, attempt) => {
try {
const { data } = await Axios.get( const { data } = await Axios.get(
`${_tiktokGetPosts(_getUserPostsParams())}`, `${_tiktokGetPosts(_getUserPostsParams())}`,
{ {
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": Tiktok.generateXTTParams( "x-tt-params": xttparams
_xttParams(secUid, cursor, count)
)
}, },
httpsAgent: httpsAgent:
(proxy && (proxy &&
@ -188,6 +189,27 @@ const requestUserPosts = async (
undefined undefined
} }
) )
console.log(data)
return 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 { SocksProxyAgent } from "socks-proxy-agent"
import { HttpsProxyAgent } from "https-proxy-agent" import { HttpsProxyAgent } from "https-proxy-agent"
import { TiktokService } from "../../services/tiktokService" import { TiktokService } from "../../services/tiktokService"
import retry from "async-retry"
import { import {
TiktokVideoSearchResponse, TiktokVideoSearchResponse,
AuthorVideoSearch, AuthorVideoSearch,
@ -36,35 +37,9 @@ export const SearchVideo = async (
}) })
} }
const Tiktok = new TiktokService() try {
const url = new URL( const data = await requestVideoSearch(keyword, page, cookie, proxy)
_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)
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 // Cookie Invalid
if (data.status_code === 2483) if (data.status_code === 2483)
return resolve({ status: "error", message: "Invalid cookie!" }) return resolve({ status: "error", message: "Invalid cookie!" })
@ -93,7 +68,7 @@ export const SearchVideo = async (
} }
const stats: StatisticsVideoSearch = { const stats: StatisticsVideoSearch = {
diggCount: v.stats.diggCount, likeCount: v.stats.diggCount,
shareCount: v.stats.shareCount, shareCount: v.stats.shareCount,
commentCount: v.stats.commentCount, commentCount: v.stats.commentCount,
playCount: v.stats.playCount, playCount: v.stats.playCount,
@ -147,8 +122,55 @@ export const SearchVideo = async (
page, page,
totalResults: result.length totalResults: result.length
}) })
}) } catch (e) {
.catch((e) => {
resolve({ status: "error", message: e.message }) 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}`)
}
}
)
}