naseif / tiktok-scraper

Scraper for TikTok. Download videos, music, fetch users info and more.
https://naseif.github.io/tiktok-scraper/
MIT License
90 stars 25 forks source link

Getting error when get user by username #5

Closed pelldam closed 2 years ago

pelldam commented 2 years ago

/Users/peldam/Documents/tiktok-api/nodemodules/tiktok-scraper-ts/dist/main.js:1 var W=Object.create;var f=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var =Object.getOwnPropertyNames;var q=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var U=o=>f(o,"esModule",{value:!0}),a=(o,r)=>f(o,"name",{value:r,configurable:!0});var j=(o,r)=>{for(var n in r)f(o,n,{get:r[n],enumerable:!0})},$=(o,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of _(r))!D.call(o,e)&&(n||e!=="default")&&f(o,e,{get:()=>r[e],enumerable:!(s=O(r,e))||s.enumerable});return o},v=(o,r)=>$(U(f(o!=null?W(q(o)):{},"default",!r&&o&&o.__esModule?{get:()=>o.default,enumerable:!0}:{value:o,enumerable:!0})),o),J=(o=>(r,n)=>o&&o.get(r)||(n=$(U({}),r,1),o&&o.set(r,n),n))(typeof WeakMap!="undefined"?new WeakMap:0);var z={};j(z,{Music:()=>p,TTScraper:()=>m,TikTokResult:()=>b,User:()=>I,Video:()=>g,fetchAllVideosFromUser:()=>P,fetchMusic:()=>F,fetchUser:()=>G,fetchVideo:()=>Y,fetchVideoNoWaterMark:()=>H,hashtag:()=>K});var L=v(require("cheerio")),x=v(require("miniget")),T=v(require("node-fetch")),l=require("fs"),S=v(require("http")),E=v(require("https")),R=require("process");var I=class{constructor(r,n,s,e,t,u,i,h,c,w,M,k,A,C,y){this.id=r,this.uniqueId=n,this.nickname=s,this.avatar=e,this.signature=t,this.createdAt=u,this.verified=i,this.secretUID=h,this.bioLink=c,this.privateAccount=w,this.isUnderAge18=M,this.followers=k,this.following=A,this.hearts=C,this.videos=y}};a(I,"User");var b=class{constructor(r,n,s,e,t,u,i,h,c,w){this.author=r,this.video=n,this.audio=s,this.shareCount=e,this.likesCount=t,this.commentCount=u,this.playCount=i,this.createdAt=h,this.tiktokLink=c,this.thumbnail=w}};a(b,"TikTokResult");var g=class{constructor(r,n,s,e,t,u,i,h,c,w,M,k,A,C,y,V,N){this.id=r,this.description=n,this.createdAt=s,this.height=e,this.width=t,this.duration=u,this.resolution=i,this.shareCount=h,this.likesCount=c,this.commentCount=w,this.playCount=M,this.downloadURL=k,this.cover=A,this.dynamicCover=C,this.playURL=y,this.format=V,this.author=N}};a(g,"Video");var p=class{constructor(r,n,s,e,t,u,i,h,c){this.id=r,this.title=n,this.playURL=s,this.coverLarge=e,this.coverThumb=t,this.author=u,this.duration=i,this.original=h,this.album=c}};a(p,"Music");var m=class{async requestWebsite(r,n){let s=new S.default.Agent({keepAlive:!0,maxSockets:20}),e=new E.default.Agent({keepAlive:!0,maxSockets:20}),t={agent:c=>c.protocol=="http:"?s:e,headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"}},i=await(await(0,T.default)(${r},n||t)).text();return L.load(i,{xmlMode:!0})}handleHTMLContent(r){let n=r,s=n.split("window['SIGI_STATE']=")[1].indexOf(";window['SIGI_RETRY']=");return JSON.parse(n.split("window['SIGI_STATE']=")[1].slice(0,s))}async video(r){if(!r)throw new Error("A video URL must be provided");let s=(await this.requestWebsite(r))("#SIGI_STATE").text(),e=JSON.parse(s),t=e.ItemList.video.list[0];return new g(e.ItemModule[t].video.id,e.ItemModule[t].desc,new Date(Number(e.ItemModule[t].createTime)1e3).toLocaleDateString(),Number(e.ItemModule[t].video.height),Number(e.ItemModule[t].video.width),Number(e.ItemModule[t].video.duration),e.ItemModule[t].video.ratio,e.ItemModule[t].stats.shareCount,e.ItemModule[t].stats.diggCount,e.ItemModule[t].stats.commentCount,e.ItemModule[t].stats.playCount,e.ItemModule[t].video.downloadAddr.trim(),e.ItemModule[t].video.cover,e.ItemModule[t].video.dynamicCover,e.ItemModule[t].video.playAddr.trim(),e.ItemModule[t].video.format,e.ItemModule[t].nickname)}async user(r){if(!r)throw new Error("Please enter a username");let s=(await this.requestWebsite(https://www.tiktok.com/@${r}))("#SIGI_STATE").text(),e=JSON.parse(s),t=e.UserModule.users[r];return new I(t.id,t.uniqueId,t.nickname,t.avatarLarger,t.signature.trim(),new Date(t.createTime1e3).toLocaleDateString(),t.verified,t.secUid,t?.bioLink?.link,t.privateAccount,t.isUnderAge18,e.UserModule.stats[r].followerCount,e.UserModule.stats[r].followingCount,e.UserModule.stats[r].heart,e.UserModule.stats[r].videoCount)}async getAllVideosFromUser(r){if(!r)throw new Error("You must provide a username!");let s=(await this.requestWebsite(https://www.tiktok.com/@${r}))("#SIGI_STATE").text(),e=JSON.parse(s),t=[],{ItemList:u}=e;return u["user-post"].list.forEach(i=>{t.push(new g(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))}),t}async getMusic(r){if(!r)throw new Error("You must provide a link!");let s=(await this.requestWebsite(r))("#SIGI_STATE").text(),e=JSON.parse(s),t=e.ItemList.video.list[0];return new p(e.ItemModule[t].music.id,e.ItemModule[t].music.title,e.ItemModule[t].music.playUrl,e.ItemModule[t].music.coverLarge,e.ItemModule[t].music.coverThumb,e.ItemModule[t].music.authorName,Number(e.ItemModule[t].music.duration),e.ItemModule[t].music.original,e.ItemModule[t].music.album)}async downloadAllVideosFromUser(r,n){if(!r)throw new Error("Please enter a username!");let s=await this.getAllVideosFromUser(r);if(!s)throw new Error("No Videos were found for this username. Either the videos are private or the user has not videos");if(!n.path){if(n.path=`${dirname}/../${r},(0,l.existsSync)(n.path)){console.log("A folder with this username exists, that is unusual!");try{(0,l.unlinkSync)(n.path)}catch(e){console.log([ERROR] Could not remove ${n.path} ^ SyntaxError: Unexpected end of JSON input at JSON.parse () at m.user (/Users/peldam/Documents/tiktok-api/node_modules/tiktok-scraper-ts/dist/main.js:1:3703) at processTicksAndRejections (node:internal/process/task_queues:95:5) at Object.fetchUser (/Users/peldam/Documents/tiktok-api/node_modules/tiktok-scraper-ts/dist/main.js:2:2253)

Spawnrad commented 2 years ago

I think is because the line :

    const videoObject = $("#SIGI_STATE").text();
    const videoJson = JSON.parse(videoObject);

Sometimes on the description you have : "example of "description" text" so you have an error on the json because of this quotes inside quotes.

Hope that can help resolve the issue.

naseif commented 2 years ago

I think is because the line :

    const videoObject = $("#SIGI_STATE").text();
    const videoJson = JSON.parse(videoObject);

Sometimes on the description you have : "example of "description" text" so you have an error on the json because of this quotes inside quotes.

Hope that can help resolve the issue.

I do not think this is a parsing issue. I still did not look into it but I guess this has something with Tiktok itself because I noticed they have been adding Captcha challenges when visiting specific links. I will look into it and I hope I do not have to make cookies mandatory to be able to use this method. Thanks for the Feedback

roycirkin commented 2 years ago

I think is because the line :

    const videoObject = $("#SIGI_STATE").text();
    const videoJson = JSON.parse(videoObject);

Sometimes on the description you have : "example of "description" text" so you have an error on the json because of this quotes inside quotes. Hope that can help resolve the issue.

I do not think this is a parsing issue. I still did not look into it but I guess this has something with Tiktok itself because I noticed they have been adding Captcha challenges when visiting specific links. I will look into it and I hope I do not have to make cookies mandatory to be able to use this method. Thanks for the Feedback

Hi, any idea when will it be fixed? Thanks!

vanb commented 2 years ago

This does indeed return HTML with a Captcha challenge so $("#SIGI_STATE").text(); returns an empty string and JSON.parse fails.

Are cookies the solution? Is there already some cookie support that can be leveraged?

naseif commented 2 years ago

This does indeed return HTML with a Captcha challenge so $("#SIGI_STATE").text(); returns an empty string and JSON.parse fails.

Are cookies the solution? Is there already some cookie support that can be leveraged?

The answer is yes. Cookies are actually the solution but it would be a pain in the ass to update the cookies every 1 hour especially if this library is being used in bots. I found a workaround for this right now but it is a bit slower. After some testing the fix I applied is slowing the requests by 2 seconds. But at the end its better than nothing, I am going to apply this fix for now while looking for another alternative

naseif commented 2 years ago

@roycirkin, @vanb, @Spawnrad I would appreciate a feedback!

vanb commented 2 years ago

Agree something is better than nothing!