From 5d7b5388dc5e7e4cbe18de2ac21d3c2cc4e97fd9 Mon Sep 17 00:00:00 2001 From: TobyG74 Date: Sat, 30 Mar 2024 00:14:51 +0700 Subject: [PATCH] feat: added page as a replacement for the cursor --- README.md | 1 + src/constants/params.ts | 40 ++++++++++++++++++++++++++ src/index.ts | 10 +++---- src/utils/search/tiktok_user_search.ts | 40 +++----------------------- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index ef8b265..22091e0 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ const username = "tobz2k19" Tiktok.Search(username, { type: "user", + page: 1, cookie: process.env.COOKIE || "Your Cookie" }).then((result) => { console.log(result) diff --git a/src/constants/params.ts b/src/constants/params.ts index c85b9b6..fef4ea9 100644 --- a/src/constants/params.ts +++ b/src/constants/params.ts @@ -35,6 +35,46 @@ export const _userPostsParams = () => { ) } +export const _userSearchParams = (keyword: any, page: number = 1) => { + let cursor = 0 + for (let i = 1; i < page; i++) { + cursor += 10 + } + + return qs.stringify({ + WebIdLastTime: Date.now(), + 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/122.0.0.0 Safari/537.36 Edg/122.0.0.0", + channel: "tiktok_web", + cookie_enabled: true, + cursor: cursor, + device_id: "7340508178566366722", + device_platform: "web_pc", + focus_state: false, + from_page: "search", + history_len: 5, + is_fullscreen: false, + is_page_visible: true, + keyword: keyword, + os: "windows", + priority_region: "ID", + referer: "", + region: "ID", + screen_height: 768, + screen_width: 1366, + search_id: "20240329123238075BE0FECBA0FE11C76B", + tz_name: "Asia/Jakarta", + web_search_code: { tiktok: { client_params_x: { search_engine: { ies_mt_user_live_video_card_use_libra: 1, mt_search_general_user_live_card: 1 } }, search_server: {} } }, + webcast_language: "en" + }) +} + export const _tiktokApiParams = (args: any) => { return { ...args, diff --git a/src/index.ts b/src/index.ts index 4f08084..2a969ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,10 +38,10 @@ export const Tiktok = { } } }, - Search: async (query: string, options: { type: T; cookie: string }): Promise> => { + Search: async (query: string, options: { type: T; cookie?: string; page?: number }): Promise> => { switch (options?.type) { case "user": { - const response = await SearchUser(query, options.cookie) + const response = await SearchUser(query, options?.cookie, options?.page) return response as TiktokSearchResponse } // case "video": { @@ -49,13 +49,13 @@ export const Tiktok = { // return response as TiktokSearchResponse // } default: { - const response = await SearchUser(query, options?.cookie) + const response = await SearchUser(query, options?.cookie, options?.page) return response as TiktokSearchResponse } } }, - StalkUser: async (username: string, options: { cookie: string }): Promise => { - const response = await StalkUser(username, options.cookie) + StalkUser: async (username: string, options?: { cookie?: string }): Promise => { + const response = await StalkUser(username, options?.cookie) return response } } diff --git a/src/utils/search/tiktok_user_search.ts b/src/utils/search/tiktok_user_search.ts index c4dea0d..898f863 100644 --- a/src/utils/search/tiktok_user_search.ts +++ b/src/utils/search/tiktok_user_search.ts @@ -1,51 +1,19 @@ import Axios from "axios" import { _tiktokSearchUserFull, _tiktokurl } from "../../constants/api" import { TiktokUserSearchResponse } from "../../types/search/userSearch" - -const params = (keyword: string) => { - return { - WebIdLastTime: Date.now(), - 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/122.0.0.0 Safari/537.36 Edg/122.0.0.0", - channel: "tiktok_web", - cookie_enabled: true, - cursor: 0, - device_id: "7340508178566366722", - device_platform: "web_pc", - focus_state: false, - from_page: "search", - history_len: 5, - is_fullscreen: false, - is_page_visible: true, - keyword: keyword, - os: "windows", - priority_region: "ID", - referer: "", - region: "ID", - screen_height: 768, - screen_width: 1366, - tz_name: "Asia/Jakarta", - web_search_code: { tiktok: { client_params_x: { search_engine: { ies_mt_user_live_video_card_use_libra: 1, mt_search_general_user_live_card: 1 } }, search_server: {} } }, - webcast_language: "en" - } -} +import { _userSearchParams } from "../../constants/params" /** * Tiktok Search User * @param {string} username - The username you want to search * @param {object|string} cookie - Your Tiktok cookie (optional) + * @param {number} page - The page you want to search (optional) * @returns {Promise} */ -export const SearchUser = (username: string, cookie?: any): Promise => +export const SearchUser = (username: string, cookie?: any, page?: number): Promise => new Promise(async (resolve, reject) => { - Axios(_tiktokSearchUserFull(params(username)), { + Axios(_tiktokSearchUserFull(_userSearchParams(username, 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",