UNPKG

tiktermux

Version:

Scraper for downloading media in the form of videos, images and audio from Tiktok. Also for stalking Tiktok Users

153 lines (152 loc) 5.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUserPosts = void 0; const axios_1 = __importDefault(require("axios")); const api_1 = require("../../constants/api"); const params_1 = require("../../constants/params"); const https_proxy_agent_1 = require("https-proxy-agent"); const socks_proxy_agent_1 = require("socks-proxy-agent"); const tiktokService_1 = require("../../services/tiktokService"); const getProfile_1 = require("../get/getProfile"); const getUserPosts = (username, proxy, postLimit) => new Promise((resolve) => { try { (0, getProfile_1.StalkUser)(username).then(async (res) => { if (res.status === "error") { return resolve({ status: "error", message: res.message }); } const secUid = res.result.user.secUid; const data = await parseUserPosts(secUid, postLimit, proxy); resolve({ status: "success", result: data, totalPosts: data.length }); }); } catch (err) { if (err.status == 400 || (err.response.data && err.response.data.statusCode == 10201)) { return resolve({ status: "error", message: "Video not found!" }); } } }); exports.getUserPosts = getUserPosts; const parseUserPosts = async (secUid, postLimit, proxy) => { let hasMore = true; let cursor = 0; const posts = []; let counter = 0; while (hasMore) { let result = null; for (let i = 0; i < 30; i++) { result = await requestUserPosts(secUid, cursor, postLimit, proxy); if (result !== "") break; } if (result === "") { hasMore = false; break; } result?.itemList?.forEach((v) => { const author = { id: v.author.id, username: v.author.uniqueId, nickname: v.author.nickname, avatarLarger: v.author.avatarLarger, avatarThumb: v.author.avatarThumb, avatarMedium: v.author.avatarMedium, signature: v.author.signature, verified: v.author.verified, openFavorite: v.author.openFavorite, privateAccount: v.author.privateAccount, isADVirtual: v.author.isADVirtual, isEmbedBanned: v.author.isEmbedBanned }; if (v.imagePost) { const imagePost = v.imagePost.images.map((img) => img.imageURL.urlList[0]); posts.push({ id: v.id, desc: v.desc, createTime: v.createTime, digged: v.digged, duetEnabled: v.duetEnabled, forFriend: v.forFriend, officalItem: v.officalItem, originalItem: v.originalItem, privateItem: v.privateItem, shareEnabled: v.shareEnabled, stitchEnabled: v.stitchEnabled, stats: v.stats, music: v.music, author, imagePost }); } else { const video = { id: v.video.id, duration: v.video.duration, format: v.video.format, bitrate: v.video.bitrate, ratio: v.video.ratio, playAddr: v.video.playAddr, cover: v.video.cover, originCover: v.video.originCover, dynamicCover: v.video.dynamicCover, downloadAddr: v.video.downloadAddr }; posts.push({ id: v.id, desc: v.desc, createTime: v.createTime, digged: v.digged, duetEnabled: v.duetEnabled, forFriend: v.forFriend, officalItem: v.officalItem, originalItem: v.originalItem, privateItem: v.privateItem, shareEnabled: v.shareEnabled, stitchEnabled: v.stitchEnabled, stats: v.stats, music: v.music, author, video }); } }); hasMore = result.hasMore; cursor = hasMore ? result.cursor : null; counter++; if (postLimit && posts.length >= postLimit) { hasMore = false; break; } } return postLimit ? posts.slice(0, postLimit) : posts; }; const requestUserPosts = async (secUid, cursor = 0, count = 30, proxy) => { const Tiktok = new tiktokService_1.TiktokService(); const { data } = await axios_1.default.get(`${(0, api_1._tiktokGetPosts)((0, params_1._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((0, params_1._xttParams)(secUid, cursor, count)) }, httpsAgent: (proxy && (proxy.startsWith("http") || proxy.startsWith("https") ? new https_proxy_agent_1.HttpsProxyAgent(proxy) : proxy.startsWith("socks") ? new socks_proxy_agent_1.SocksProxyAgent(proxy) : undefined)) || undefined }); return data; };