UNPKG

twt-dl-cli

Version:

The easiest way to download any Twitter video from CLI

2 lines (1 loc) 1.52 kB
var u=Object.defineProperty;var i=(a,r)=>u(a,"name",{value:r,configurable:!0});import{load as d}from"cheerio";import p from"cross-fetch";import h from"download";import v from"ora";import{createWriteStream as U}from"node:fs";import{join as g}from"node:path";import f from"node:process";var w=i((a,r,o)=>new Promise((t,e)=>{var l=i(n=>{try{c(o.next(n))}catch(m){e(m)}},"fulfilled"),s=i(n=>{try{c(o.throw(n))}catch(m){e(m)}},"rejected"),c=i(n=>n.done?t(n.value):Promise.resolve(n.value).then(l,s),"step");c((o=o.apply(a,r)).next())}),"__async");function $(a){return w(this,null,function*(){if(!a)throw new Error("Missing URL");const r=a.replace("x.com","twitter.com");if(new URL(r).hostname!=="twitter.com")throw new Error("Not a Twitter URL");return p(r.replace("twitter.com","vxtwitter.com"),{headers:{"User-Agent":"TelegramBot (like TwitterBot)"}}).then(t=>w(this,null,function*(){if(!t.ok)throw new Error(`Failed to fetch tweet: ${t.status}`);return t.text()})).then(t=>{const e=d(t);return i(s=>{var c;return(c=e(`meta[name="twitter:${s}"]`).attr("content"))!=null?c:e(`meta[property="og:${s}"]`).attr("content")},"getMetaContent")("video")})})}i($,"downloadVideo");function y(a){return w(this,arguments,function*(r,o=g((t=>(t=f.env.PWD)!=null?t:f.cwd())(),`${Date.now()}.mp4`)){const t=v("Downloading file...").start(),e=U(o);return e.on("finish",()=>{t.succeed(`File saved as ${o}`)}),e.on("error",l=>{t.fail(l.message),console.error(l)}),h(r).pipe(e)})}i(y,"downloadFile");export{y as downloadFile,$ as downloadVideo};