UNPKG

apithreads

Version:

Unofficial, Reverse-Engineered Node.js/TypeScript client for Meta's [Threads](https://threads.net).

1 lines 13.5 kB
"use strict";!function(e,t){for(var n in t)Object.defineProperty(e,n,{enumerable:!0,get:t[n]})}(exports,{DEFAULT_DEVICE:function(){return l},ThreadsAPI:function(){return d}});var e=require("axios"),t=require("fs"),n=require("mime-types"),r=require("uuid"),s=require("./constants"),a=require("./dynamic-data");function o(e,t,n,r,s,a,o){try{var i=e[a](o),u=i.value}catch(e){n(e);return}i.done?t(u):Promise.resolve(u).then(r,s)}function i(e){return function(){var t=this,n=arguments;return new Promise(function(r,s){var a=e.apply(t,n);function i(e){o(a,r,s,i,u,"next",e)}function u(e){o(a,r,s,i,u,"throw",e)}i(void 0)})}}function u(){return(u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function c(e,t){var n,r,s,a,o={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){return function(a){if(n)throw TypeError("Generator is already executing.");for(;o;)try{if(n=1,r&&(s=2&a[0]?r.return:a[0]?r.throw||((s=r.return)&&s.call(r),0):r.next)&&!(s=s.call(r,a[1])).done)return s;switch(r=0,s&&(a=[2&a[0],s.value]),a[0]){case 0:case 1:s=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,r=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(s=(s=o.trys).length>0&&s[s.length-1])&&(6===a[0]||2===a[0])){o=0;continue}if(3===a[0]&&(!s||a[1]>s[0]&&a[1]<s[3])){o.label=a[1];break}if(6===a[0]&&o.label<s[1]){o.label=s[1],s=a;break}if(s&&o.label<s[2]){o.label=s[2],o.ops.push(a);break}s[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],r=0}finally{n=s=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,i])}}}var l={manufacturer:"OnePlus",model:"ONEPLUS+A3010",os_version:25,os_release:"7.1.1"},d=function(o){"use strict";var d=this;this.verbose=!1,this.token=void 0,this.fbLSDToken=s.DEFAULT_LSD_TOKEN,this.noUpdateToken=!1,this.noUpdateLSD=!1,this.deviceID=s.DEFAULT_DEVICE_ID,this.device=l,this.userID=void 0,this._getAppHeaders=function(){return u({"User-Agent":"Barcelona "+a.LATEST_ANDROID_APP_VERSION+" Android","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},d.token?{Authorization:"Bearer IGT:2:"+d.token}:void 0)},this._getDefaultHeaders=function(e){return u({},d._getAppHeaders(),{authority:"www.threads.net",accept:"*/*","accept-language":"ko","cache-control":"no-cache",origin:"https://www.threads.net",pragma:"no-cache","Sec-Fetch-Site":"same-origin","x-asbd-id":"129477","x-fb-lsd":d.fbLSDToken,"x-ig-app-id":"238260118697367"},e?{referer:"https://www.threads.net/@"+e}:void 0)};var h=this;this.getUserIDfromUsername=i(function(t,n){var r,s,a,o,i;return c(this,function(c){switch(c.label){case 0:return[4,e.default.get("https://www.instagram.com/"+t,u({},n,{httpAgent:h.httpAgent,httpsAgent:h.httpsAgent,headers:u({},h._getDefaultHeaders(t),{accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","accept-language":"ko,en;q=0.9,ko-KR;q=0.8,ja;q=0.7",Authorization:void 0,referer:"https://www.instagram.com/","sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"cross-site","sec-fetch-user":"?1","upgrade-insecure-requests":"1","x-asbd-id":void 0,"x-fb-lsd":void 0,"x-ig-app-id":void 0})}))];case 1:return o=null==(r=(a=(a=(a=c.sent().data).replace(/\s/g,"")).replace(/\n/g,"")).match(/"user_id":"(\d+)",/))?void 0:r[1],i=null==(s=a.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/))?void 0:s[1],!h.noUpdateLSD&&i&&(h.fbLSDToken=i,h.verbose&&console.debug("[fbLSDToken] UPDATED",h.fbLSDToken)),[2,o]}})});var p=this;this.getCurrentUserID=i(function(e){return c(this,function(t){switch(t.label){case 0:if(p.userID)return p.verbose&&console.debug("[userID] USING",p.userID),[2,p.userID];if(!p.username)throw Error("username is not defined");return[4,p.getUserIDfromUsername(p.username,e)];case 1:return p.userID=t.sent(),p.verbose&&console.debug("[userID] UPDATED",p.userID),[2,p.userID]}})});var f=this;this.getUserProfile=i(function(t,n,r){return c(this,function(s){switch(s.label){case 0:return f.verbose&&console.debug("[fbLSDToken] USING",f.fbLSDToken),[4,e.default.post("https://www.threads.net/api/graphql",new URLSearchParams({lsd:f.fbLSDToken,variables:'{"userID":"'+n+'"}',doc_id:"23996318473300828"}),u({},r,{httpAgent:f.httpAgent,httpsAgent:f.httpsAgent,headers:u({},f._getDefaultHeaders(t),{"x-fb-friendly-name":"BarcelonaProfileRootQuery"})}))];case 1:return[2,s.sent().data.data.userData.user]}})});var g=this;this.getUserProfileThreads=i(function(t,n,r){var s,a;return c(this,function(o){switch(o.label){case 0:return g.verbose&&console.debug("[fbLSDToken] USING",g.fbLSDToken),[4,e.default.post("https://www.threads.net/api/graphql",new URLSearchParams({lsd:g.fbLSDToken,variables:'{"userID":"'+n+'"}',doc_id:"6232751443445612"}),u({},r,{httpAgent:g.httpAgent,httpsAgent:g.httpsAgent,headers:u({},g._getDefaultHeaders(t),{"x-fb-friendly-name":"BarcelonaProfileThreadsTabQuery"})}))];case 1:return[2,(null==(a=o.sent().data.data)?void 0:null==(s=a.mediaData)?void 0:s.threads)||[]]}})});var b=this;this.getUserProfileReplies=i(function(t,n,r){var s,a;return c(this,function(o){switch(o.label){case 0:return b.verbose&&console.debug("[fbLSDToken] USING",b.fbLSDToken),[4,e.default.post("https://www.threads.net/api/graphql",new URLSearchParams({lsd:b.fbLSDToken,variables:'{"userID":"'+n+'"}',doc_id:"6684830921547925"}),u({},r,{httpAgent:b.httpAgent,httpsAgent:b.httpsAgent,headers:u({},b._getDefaultHeaders(t),{"x-fb-friendly-name":"BarcelonaProfileRepliesTabQuery"})}))];case 1:return[2,(null==(a=o.sent().data.data)?void 0:null==(s=a.mediaData)?void 0:s.threads)||[]]}})});var v=this;this.getPostIDfromThreadID=i(function(e,t){var n;return c(this,function(r){return n="https://www.threads.net/t/"+(e=(e=(e=e.split("?")[0]).replace(/\s/g,"")).replace(/\//g,"")),[2,v.getPostIDfromURL(n,t)]})});var _=this;this.getPostIDfromURL=i(function(t,n){var r,s,a,o,i;return c(this,function(c){switch(c.label){case 0:return[4,e.default.get(t,u({},n,{httpAgent:_.httpAgent,httpsAgent:_.httpsAgent}))];case 1:return o=null==(r=(a=(a=(a=c.sent().data).replace(/\s/g,"")).replace(/\n/g,"")).match(/{"post_id":"(.*?)"}/))?void 0:r[1],i=null==(s=a.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/))?void 0:s[1],!_.noUpdateLSD&&i&&(_.fbLSDToken=i,_.verbose&&console.debug("[fbLSDToken] UPDATED",_.fbLSDToken)),[2,o]}})});var m=this;this.getThreads=i(function(t,n){return c(this,function(r){switch(r.label){case 0:return m.verbose&&console.debug("[fbLSDToken] USING",m.fbLSDToken),[4,e.default.post("https://www.threads.net/api/graphql",new URLSearchParams({lsd:m.fbLSDToken,variables:'{"postID":"'+t+'"}',doc_id:"5587632691339264"}),u({},n,{httpAgent:m.httpAgent,httpsAgent:m.httpsAgent,headers:u({},m._getDefaultHeaders(),{"x-fb-friendly-name":"BarcelonaPostPageQuery"})}))];case 1:return[2,r.sent().data.data.data]}})});var D=this;this.getThreadLikers=i(function(t,n){return c(this,function(r){switch(r.label){case 0:return D.verbose&&console.debug("[fbLSDToken] USING",D.fbLSDToken),[4,e.default.post("https://www.threads.net/api/graphql",new URLSearchParams({lsd:D.fbLSDToken,variables:'{"mediaID":"'+t+'"}',doc_id:"9360915773983802"}),u({},n,{httpAgent:D.httpAgent,httpsAgent:D.httpsAgent,headers:D._getDefaultHeaders()}))];case 1:return[2,r.sent().data.data.likers]}})});var w=this;this._toggleAuthPostRequest=i(function(t,n){return c(this,function(r){switch(r.label){case 0:return[4,w.getToken()];case 1:if(!r.sent())throw Error("Token not found");return[4,e.default.post(t,void 0,u({},n,{httpAgent:w.httpAgent,httpsAgent:w.httpsAgent,headers:w._getDefaultHeaders()}))];case 2:return[2,r.sent()]}})});var A=this;this.like=i(function(e,t){var n;return c(this,function(r){switch(r.label){case 0:return[4,A.getCurrentUserID()];case 1:return n=r.sent(),[4,A._toggleAuthPostRequest(s.BASE_API_URL+"/media/"+e+"_"+n+"/like/",t)];case 2:return[2,"ok"===r.sent().data.status]}})});var S=this;this.unlike=i(function(e,t){var n;return c(this,function(r){switch(r.label){case 0:return[4,S.getCurrentUserID()];case 1:return n=r.sent(),[4,S._toggleAuthPostRequest(s.BASE_API_URL+"/media/"+e+"_"+n+"/unlike/",t)];case 2:return[2,"ok"===r.sent().data.status]}})});var k=this;this.follow=i(function(e,t){var n;return c(this,function(r){switch(r.label){case 0:return[4,k._toggleAuthPostRequest(s.BASE_API_URL+"/friendships/create/"+e+"/",t)];case 1:return n=r.sent(),k.verbose&&console.debug("[FOLLOW]",n.data),[2,n.data]}})});var I=this;this.unfollow=i(function(e,t){var n;return c(this,function(r){switch(r.label){case 0:return[4,I._toggleAuthPostRequest(s.BASE_API_URL+"/friendships/destroy/"+e+"/",t)];case 1:return n=r.sent(),I.verbose&&console.debug("[UNFOLLOW]",n.data),[2,n.data]}})});var T=this;this.getToken=i(function(){var t,n,r,a,o;return c(this,function(i){switch(i.label){case 0:if(T.token)return T.verbose&&console.debug("[token] USING",T.token),[2,T.token];if(!T.username||!T.password)throw Error("Username and password are required");return t=encodeURIComponent(JSON.stringify({client_input_params:{password:T.password,contact_point:T.username,device_id:""+T.deviceID},server_params:{credential_type:"password",device_id:""+T.deviceID}})),r=encodeURIComponent(JSON.stringify({bloks_version:n="5f56efad68e1edec7801f630b5c122704ec5378adbee6609a448f105f34a9c73",styles_id:"instagram"})),a={method:"POST",headers:T._getAppHeaders(),responseType:"text",data:"params="+t+"&bk_client_context="+r+"&bloks_versioning_id="+n},[4,(0,e.default)(s.LOGIN_URL,a)];case 1:return o=i.sent().data.split("Bearer IGT:2:")[1].split('"')[0].replaceAll("\\",""),T.noUpdateToken||(T.verbose&&console.debug("[token] UPDATED",o),T.token=o),[2,o]}})});var L=this;this.publish=i(function(t){var n,r,a,o,i,u,l,d;return c(this,function(c){switch(c.label){case 0:if(n="string"==typeof t?{text:t}:t,!L.username||!L.password)throw Error("Username or password not set");return[4,L.getUserIDfromUsername(L.username)];case 1:if(!(r=c.sent()))throw Error("User ID not found");return[4,L.getToken()];case 2:if(!c.sent())throw Error("Token not found");if(o={text_post_app_info:{reply_control:0},timezone_offset:(-(60*(a=new Date).getTimezoneOffset())).toString(),source_type:"4",_uid:r,device_id:L.deviceID,caption:n.text||"",upload_id:a.getTime(),device:L.device},i=s.POST_URL,!("image"in n&&n.image))return[3,4];return i=s.POST_WITH_IMAGE_URL,[4,L.uploadImage(n.image)];case 3:return u=c.sent().upload_id,o.upload_id=u,o.scene_capture_type="",[3,5];case 4:"url"in n&&n.url&&(o.text_post_app_info.link_attachment_url=n.url),c.label=5;case 5:return n.parentPostID&&(o.text_post_app_info.reply_id=n.parentPostID),n.image||(o.publish_mode="text_post"),l="signed_body=SIGNATURE."+encodeURIComponent(JSON.stringify(o)),[4,e.default.post(i,l,{httpAgent:L.httpAgent,httpsAgent:L.httpsAgent,headers:L._getAppHeaders(),timeout:6e4})];case 6:if(d=c.sent(),L.verbose&&console.debug("[PUBLISH]",d.data),"ok"===d.data.status)return[2,!0];return[2,!1]}})});var U=this;this.publishWithImage=i(function(e,t){return c(this,function(n){return[2,U.publish({text:e,image:t})]})});var y=this;this.uploadImage=i(function(s){var a,o,i,l,d,h,p,f,g,b,v;return c(this,function(c){switch(c.label){case 0:if(i="https://www.instagram.com/rupload_igphoto/"+(o=(a=Date.now().toString())+"_0_"+Math.floor(9e9*Math.random()+1e9)),s.startsWith("http"))return[3,2];return[4,t.promises.readFile(s)];case 1:return l=c.sent(),d=n.default.lookup(s)||"application/octet-stream",[3,4];case 2:return[4,e.default.get(s,{responseType:"arraybuffer"})];case 3:h=c.sent(),l=Buffer.from(h.data,"binary"),d=h.headers["content-type"],c.label=4;case 4:p={upload_id:a,media_type:"1",sticker_burnin_params:JSON.stringify([]),image_compression:JSON.stringify({lib_name:"moz",lib_version:"3.1.m",quality:"80"}),xsharing_user_ids:JSON.stringify([]),retry_context:JSON.stringify({num_step_auto_retry:"0",num_reupload:"0",num_step_manual_retry:"0"}),"IG-FB-Xpost-entry-point-v2":"feed"},f=l.length,g=u({},y._getDefaultHeaders(),{"Content-Type":"application/octet-stream",X_FB_PHOTO_WATERFALL_ID:(0,r.v4)(),"X-Entity-Type":void 0!==d?"image/"+d:"image/jpeg",Offset:"0","X-Instagram-Rupload-Params":JSON.stringify(p),"X-Entity-Name":o,"X-Entity-Length":f.toString(),"Content-Length":f.toString(),"Accept-Encoding":"gzip"}),y.verbose&&console.log("[UPLOAD_IMAGE] Uploading "+f.toLocaleString()+"b as "+a+"..."),c.label=5;case 5:return c.trys.push([5,7,,8]),[4,e.default.post(i,l,{httpAgent:y.httpAgent,headers:g,timeout:6e4})];case 6:return b=c.sent().data,y.verbose&&console.log("[UPLOAD_IMAGE] SUCCESS",b),[2,b];case 7:throw v=c.sent(),y.verbose&&console.log("[UPLOAD_IMAGE] FAILED",v.response.data),v;case 8:return[2]}})}),(null==o?void 0:o.token)&&(this.token=o.token),(null==o?void 0:o.fbLSDToken)&&(this.fbLSDToken=o.fbLSDToken),this.noUpdateToken=!!(null==o?void 0:o.noUpdateToken),this.noUpdateLSD=!!(null==o?void 0:o.noUpdateLSD),this.verbose=(null==o?void 0:o.verbose)||!1,this.httpAgent=null==o?void 0:o.httpAgent,this.httpsAgent=null==o?void 0:o.httpsAgent,this.username=null==o?void 0:o.username,this.password=null==o?void 0:o.password,(null==o?void 0:o.deviceID)&&(this.deviceID=o.deviceID),this.device=null==o?void 0:o.device};