@tcortega/twitter-scraper
Version:
Twitter Scraper & Media Downloader. Scrape info from any tweet.
3 lines (2 loc) • 12.7 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("node-fetch"))&&"object"==typeof t&&"default"in t?t.default:t;function r(t,e,r,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=t.apply(e,n);function u(t){r(a,o,i,u,c,"next",t)}function c(t){r(a,o,i,u,c,"throw",t)}u(void 0)}))}}function o(){return(o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var a,u=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var o=Object.create((e&&e.prototype instanceof l?e:l).prototype),i=new A(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=_(a,r);if(u){if(u===f)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=s(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===f)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f={};function l(){}function d(){}function h(){}var p={};p[o]=function(){return this};var v=Object.getPrototypeOf,y=v&&v(v(L([])));y&&y!==e&&r.call(y,o)&&(p=y);var m=h.prototype=l.prototype=Object.create(p);function g(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,u){var c=s(t[o],t,i);if("throw"!==c.type){var f=c.arg,l=f.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):e.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,u)}))}u(c.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function _(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,_(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function x(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function b(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function L(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:j}}function j(){return{value:void 0,done:!0}}return d.prototype=m.constructor=h,h.constructor=d,d.displayName=u(h,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===d||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,u(t,a,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},g(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(c(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},g(m),u(m,a,"Generator"),m[o]=function(){return this},m.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=L,A.prototype={constructor:A,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(b),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(u&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,f):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),b(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;b(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:L(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(a={exports:{}}),a.exports),c=function(){function t(){}return t.mapMediaObject=function(e){for(var r,n=[],o=function(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return i(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(t,void 0):void 0}}(t))){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e);!(r=o()).done;){var a=r.value;if("video"===a.type)return t.mapMediaObjectForVideos(a);n.push(t.mapMediaObjectForImages(a))}return{isImage:!0,media_url:n}},t.mapMediaObjectForVideos=function(t){var e=t.video_info.variants.filter((function(t){return t.hasOwnProperty("bitrate")}));return{isVideo:!0,media_url:e=e.sort((function(t,e){return e.bitrate-t.bitrate}))}},t.mapMediaObjectForImages=function(t){return{url:t.media_url_https,content_type:"img/jpg"}},t}(),s=function(){function t(){}return t.get=function(){var t=n(u.mark((function t(r,n){var i;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=this.defaultHeaders,n&&(i=o({},this.defaultHeaders,n)),t.next=4,e(r,{headers:i});case 4:return t.abrupt("return",t.sent);case 5:case"end":return t.stop()}}),t,this)})));return function(e,r){return t.apply(this,arguments)}}(),t.post=function(){var t=n(u.mark((function t(r,n){var i;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=this.defaultHeaders,n&&(i=o({},this.defaultHeaders,n)),t.next=4,e(r,{method:"POST",headers:i});case 4:return t.abrupt("return",t.sent);case 5:case"end":return t.stop()}}),t,this)})));return function(e,r){return t.apply(this,arguments)}}(),t}();s.defaultHeaders={authorization:"Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"};var f=function(){function t(){}return t.isValidTweetUrl=function(t){return/(?:http)?(?:s:\/\/)?(?:www\.)?twitter\.com\/([a-zA-Z0-9_]+)\/status\/[0-9]{19}/.test(t)},t.getTweetId=function(t){return t.match(/(?<=status\/\s*)\d+/)[0]},t.getRandomGuestID=function(){var t=n(u.mark((function t(){return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,s.post("https://api.twitter.com/1.1/guest/activate.json");case 2:return t.next=4,t.sent.json();case 4:return t.abrupt("return",t.sent.guest_token);case 6:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),t.filterTweetData=function(t){var e={id:t.id_str,created_at:t.created_at,description:t.text?t.text:t.full_text,isMedia:t.hasOwnProperty("extended_entities"),favorite_count:t.favorite_count,retweet_count:t.retweet_count,reply_count:t.reply_count,quote_count:t.quote_count};return e.isMedia&&(e=o({},e,c.mapMediaObject(t.extended_entities.media))),e},t}(),l=function(t,e){this.message=t,this.errorType=e};(exports.ErrorEnum||(exports.ErrorEnum={})).INVALID_URL="The url provided is not a valid tweet url",exports.TwitterScraper=function(){function t(t){this.guestId=t}t.create=function(){var e=n(u.mark((function e(){return u.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,f.getRandomGuestID();case 2:return e.abrupt("return",new t(e.sent));case 4:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();var e=t.prototype;return e.getTweetMeta=function(){var t=n(u.mark((function t(e){var r,n;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!f.isValidTweetUrl(e)){t.next=13;break}return r=f.getTweetId(e),t.next=4,s.get("https://twitter.com/i/api/2/timeline/conversation/"+r+".json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweet=true&count=20&include_ext_has_birdwatch_notes=false&ext=mediaStats%2ChighlightedLabel",this.buildGuestIdHeader());case 4:if(401!==(n=t.sent).status){t.next=8;break}return this.reloadGuestId(),t.abrupt("return",this.getTweetMeta(e));case 8:return t.next=10,n.json();case 10:return t.abrupt("return",f.filterTweetData(t.sent.globalObjects.tweets[r]));case 13:throw new l(exports.ErrorEnum.INVALID_URL,"INVALID_URL");case 14:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),e.reloadGuestId=function(){var t=n(u.mark((function t(){return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,f.getRandomGuestID();case 2:this.guestId=t.sent;case 3:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}(),e.buildGuestIdHeader=function(){return{"x-guest-token":this.guestId}},t}(),exports.TwitterScraperError=l;
//# sourceMappingURL=twitter-scraper.cjs.production.min.js.map