UNPKG

tiktok-scraper-ts-updated

Version:
3 lines (2 loc) 11.4 kB
var O=Object.defineProperty;var a=(s,r)=>O(s,"name",{value:r,configurable:!0}),N=(s=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(s,{get:(r,e)=>(typeof require!="undefined"?require:r)[e]}):s)(function(s){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+s+'" is not supported')});import{fileURLToPath as $}from"url";import D from"path";var j=a(()=>$(import.meta.url),"getFilename"),P=a(()=>D.dirname(j()),"getDirname"),m=P();import*as M from"cheerio";import x from"miniget";import y from"node-fetch";import{createWriteStream as T,existsSync as U,mkdirSync as z,unlinkSync as G}from"node:fs";import*as E from"puppeteer";import H from"node:http";import K from"node:https";import{exit as B}from"node:process";var w=class{constructor(r,e,t,o,i,u,c,d,n,g,v,_,I,k){this.id=r,this.uniqueId=e,this.nickname=t,this.avatar=o,this.signature=i,this.createdAt=u,this.verified=c,this.secretUID=d,this.bioLink=n,this.privateAccount=g,this.followers=v,this.following=_,this.hearts=I,this.videos=k}};a(w,"User");var b=class{constructor(r,e,t,o,i,u,c,d,n,g){this.author=r,this.video=e,this.audio=t,this.shareCount=o,this.likesCount=i,this.commentCount=u,this.playCount=c,this.createdAt=d,this.tiktokLink=n,this.thumbnail=g}};a(b,"TikTokResult");var p=class{constructor(r,e,t,o,i,u,c,d,n,g,v,_,I,k,S,R,L,V){this.id=r,this.description=e,this.createdAt=t,this.height=o,this.width=i,this.duration=u,this.resolution=c,this.shareCount=d,this.likesCount=n,this.commentCount=g,this.playCount=v,this.downloadURL=_,this.cover=I,this.dynamicCover=k,this.playURL=S,this.format=R,this.author=L,this.directVideoUrl=V}};a(p,"Video");var f=class{constructor(r,e,t,o,i,u,c,d,n){this.id=r,this.title=e,this.playURL=t,this.coverLarge=o,this.coverThumb=i,this.author=u,this.duration=c,this.original=d,this.album=n}};a(f,"Music");import{CookieJar as X}from"netscape-cookies-parser";import W from"node-fetch";var q=N("tiktok-signature"),F="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.56",J="https://www.tiktok.com/api/post/item_list/?WebIdLastTime=1725387734&aid=1988&app_language=en&app_name=tiktok_web&browser_language=en-US&browser_name=Mozilla&browser_online=true&browser_platform=MacIntel&browser_version=5.0 (Macintosh)&channel=tiktok_web&cookie_enabled=true&count=35&coverFormat=2&cursor=0&data_collection_enabled=true&device_id=7410483808526698016&device_platform=web_pc&focus_state=true&from_page=user&history_len=5&is_fullscreen=false&is_page_visible=true&language=en&odinId=7410483842936669216&os=mac&priority_region=&referer=https://www.google.com/&region=PL&root_referer=https://www.google.com/&screen_height=1117&screen_width=1728&secUid=MS4wLjABAAAAGu-Kg2l1tIS7B6HJ1skdamgWbjdobxERVTR2NJ0MHXg0X7E8b_Pt1E7KUp8-bSzc&tz_name=Europe/Warsaw&user_is_login=false&webcast_language=en&msToken=4plSj1K-DnpCGX4S4YB8UcoEs6Jh-jrEyjH9Ot8W0CRZUk0gSkPxIzhSLctxAGwlwMwKbVNFYO7fO6Eze0YIqYjK57eT2_wxETNqXjU197SxS3zSOx7YXkc6iGULBc0vd1vzsrPVMB5S3zM-2eF3jg==&X-Bogus=DFSzsIVOSz2AN9fbtI/AvSMK1-7s&_signature=_02B4Z6wo00001k5X6aQAAIDCNwU9tBpuJepOVe0AAPVReb",A={aid:"1988",count:35,secUid:"",cursor:"",cookie_enabled:!0,screen_width:0,screen_height:0,browser_language:"",browser_platform:"",browser_name:"",browser_version:"",browser_online:"",timezone_name:"Europe/London"};async function C(s,r){A.secUid=s,A.cursor=r;let e=new q(null,F);await e.init();let i=`https://m.tiktok.com/api/post/item_list/?${new URLSearchParams(A).toString()}`,u=await e.sign(i),c=await e.navigator();await e.close();let{"x-tt-params":d}=u,{user_agent:n}=c,g=await Y(n,d);if(g.status!==200)throw new Error("A request to get the user's videos was not successful!");return await g.json()}a(C,"getUserVideos");async function Y(s,r){return await W(J,{method:"GET",headers:{"user-agent":s,"x-tt-params":r}})}a(Y,"fetchVideos");var h=class{constructor(r){this._cookiesJar=new X;r?this._cookies=this._cookiesJar.load(r):this._cookies=""}async requestWebsite(r,e){let t=new H.Agent({keepAlive:!0,maxSockets:20}),o=new K.Agent({keepAlive:!0,maxSockets:20}),i={agent:n=>n.protocol=="http:"?t:o,headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36",Accept:"application/json, text/plain, */*","Accept-Encoding":"gzip, deflate, br","Cache-Control":"no-cache",Connection:"keep-alive",Cookie:`${this._cookies?this._cookiesJar.toString():""}`}},c=await(await y(`${r}`,e||i)).text();return M.load(c,{xmlMode:!0})}extractJSONObject(r){let e=r.split('<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" type="application/json">')[1].indexOf("<\/script>");return r.split('<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" type="application/json">')[1].slice(0,e)}checkJSONExisting(r){try{return!!JSON.parse(r)}catch{}}async requestWithPuppeteer(r){let e=await E.launch({headless:!0,args:["--no-sandbox","--disable-setuid-sandbox"]}),o=await(await e.newPage()).goto(r,{waitUntil:"domcontentloaded"});if(o==null)throw await e.close(),new Error("Could not load the desired Page!");let i=await o.text();return await e.close(),this.extractJSONObject(i)}handleHTMLContent(r){let e=r,t=e.split("window['SIGI_STATE']=")[1].indexOf(";window['SIGI_RETRY']=");return JSON.parse(e.split("window['SIGI_STATE']=")[1].slice(0,t))}async TryFetch(r){let e=await this.requestWebsite(r);if(this.checkJSONExisting(e("#__UNIVERSAL_DATA_FOR_REHYDRATION__").text()))return JSON.parse(e("#__UNIVERSAL_DATA_FOR_REHYDRATION__").text());{let t=await this.requestWithPuppeteer(r);return JSON.parse(t)}}async video(r){if(!r)throw new Error("A video URL must be provided");let t=(await this.TryFetch(r)).__DEFAULT_SCOPE__["webapp.video-detail"].itemInfo.itemStruct,o=t.id;if(o==0)return console.log("Could not find the Video on Tiktok!");let i=await this.noWaterMark(r);return new p(o,t.desc,new Date(Number(t.createTime)*1e3).toLocaleDateString(),Number(t.video.height),Number(t.video.width),Number(t.video.duration),t.video.ratio,t.stats.shareCount,t.stats.diggCount,t.stats.commentCount,t.stats.playCount,i,t.video.cover,t.video.dynamicCover,i,t.video.format,t.author,`https://www.tiktok.com/@${t.author.uniqueId}/video/${o}`)}async user(r){if(!r)throw new Error("Please enter a username");let t=(await this.TryFetch(`https://www.tiktok.com/@${r}`)).__DEFAULT_SCOPE__["webapp.user-detail"].userInfo;return new w(t.user.id,t.user.uniqueId,t.user.nickname,t.user.avatarLarger,t.user.signature.trim(),new Date(t.createTime*1e3).toLocaleDateString(),t.user.verified,t.user.secUid,t?.bioLink?.link??"none",t.user.privateAccount,t.stats.followerCount,t.stats.followingCount,t.stats.heart,t.stats.videoCount)}async getAllVideosFromUser(r,e){if(!r)throw new Error("You must provide a username!");let{secretUID:t}=await this.user(`${r}`);if(!t)throw new Error("Couuld not find user UID!");let o="",i=[],u=[],c=await C(t,o);if(c?.itemList&&(u.push(c.itemList),o=c.cursor),c?.hasMore===!0)for(;;){let d=await C(t,o);if(u.push(d.itemList),o=d.cursor,d.hasMore==!1)break}for(let d of u)for(let n of d)i.push(new p(n.id,n.desc,new Date(Number(n.createTime)*1e3).toLocaleDateString(),Number(n.video?.height),Number(n.video?.width),Number(n.video?.duration),n.video?.ratio,n?.stats?.shareCount,n?.stats?.diggCount,n?.stats?.commentCount,n?.stats?.playCount,e?await this.noWaterMark(`https://www.tiktok.com/@${n.author.uniqueId}/video/${n.id}`):n.video?.downloadAddr.trim(),n?.video?.cover,n?.video?.dynamicCover,e?await this.noWaterMark(`https://www.tiktok.com/@${n.author.uniqueId}/video/${n.id}`):n.video?.downloadAddr.trim(),n?.video?.format,n.author,`https://www.tiktok.com/@${n.author.uniqueId}/video/${n.id}`));return i}async getMusic(r){if(!r)throw new Error("You must provide a link!");let t=(await this.TryFetch(r)).__DEFAULT_SCOPE__["webapp.video-detail"].itemInfo.itemStruct;return new f(t.music.id,t.music.title,t.music.playUrl,t.music.coverLarge,t.music.coverThumb,t.music.authorName,Number(t.music.duration),t.music.original,t.music.album)}async downloadAllVideosFromUser(r,e){if(!r)throw new Error("Please enter a username!");let t=await this.getAllVideosFromUser(r);if(!t)throw new Error("No Videos were found for this username. Either the videos are private or the user has not videos");if(!e.path){if(e.path=`${m}/../${r}`,U(e.path)){console.log("A folder with this username exists, that is unusual!");try{G(e.path)}catch(o){console.log(`[ERROR] Could not remove ${e.path} Error Message: ${o.message}`),B(1)}}U(e.path)||z(e.path)}if(e.watermark){for(let[o,i]of t.entries()){console.log(`Downloading Video: ${i.description?i.description:i.id}, [${o+1}/${t.length}]`);let u=await this.noWaterMark(i.id);if(!u){console.log(`Could not fetch ${i.description?i.description:i.id} with no watermark`);continue}x(u).pipe(T(`${e.path}/${i.id}_${i.resolution}.${i.format}`))}return}for(let[o,i]of t.entries())console.log(`Downloading Video: ${i.description?i.description:i.id}, [${o+1}/${t.length}]`),x(i.downloadURL).pipe(T(`${e.path}/${i.id}_${i.resolution}.${i.format}`))}async noWaterMark(r){let e={url:r,count:"12",cursor:"0",web:"1",hd:"1"},t=await y("https://www.tikwm.com/api/",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams(e).toString()});if(!t.ok)throw new Error("There was an Error retrieveing this video without watermark!");let o=await t.json();if(o.code===-1)throw new Error("API Limit for nowatermark, please wait 1 second and try again!");return"https://www.tikwm.com"+o.data.hdplay}async hashTag(r){if(!r)throw new Error("You must provide a tag name to complete the search!");let e=await this.TryFetch(`https://www.tiktok.com/tag/${r}`);console.log(e);let{ItemList:t}=e,o=[];for(let i of t.challenge.list)o.push(new p(e.ItemModule[i].video.id,e.ItemModule[i].desc,new Date(Number(e.ItemModule[i].createTime)*1e3).toLocaleDateString(),Number(e.ItemModule[i].video.height),Number(e.ItemModule[i].video.width),Number(e.ItemModule[i].video.duration),e.ItemModule[i].video.ratio,e.ItemModule[i].stats.shareCount,e.ItemModule[i].stats.diggCount,e.ItemModule[i].stats.commentCount,e.ItemModule[i].stats.playCount,e.ItemModule[i].video.downloadAddr.trim(),e.ItemModule[i].video.cover,e.ItemModule[i].video.dynamicCover,e.ItemModule[i].video.playAddr.trim(),e.ItemModule[i].video.format,e.ItemModule[i].author));return o}};a(h,"TTScraper");async function jt(s){if(!s)throw new Error("You must provide a Tiktok video url!");return await new h().video(s)}a(jt,"fetchVideo");async function Pt(s){if(!s)throw new Error("You must provide a username!");return await new h().user(s)}a(Pt,"fetchUser");async function qt(s,r){if(!s)throw new Error("You must provide a username!");return await new h().getAllVideosFromUser(s,r)}a(qt,"fetchAllVideosFromUser");async function Wt(s){if(!s)throw new Error("You must provide a Tiktok video url!");return await new h().getMusic(s)}a(Wt,"fetchMusic");async function Ft(s){if(!s)throw new Error("You must provide a Tiktok video url!");return await new h().noWaterMark(s)}a(Ft,"fetchVideoNoWaterMark");async function Jt(s){if(!s)throw new Error("You must provide a tiktok hashtag");return await new h().hashTag(s)}a(Jt,"hashtag");export{f as Music,h as TTScraper,b as TikTokResult,w as User,p as Video,qt as fetchAllVideosFromUser,Wt as fetchMusic,Pt as fetchUser,jt as fetchVideo,Ft as fetchVideoNoWaterMark,Jt as hashtag};