-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·214 lines (172 loc) · 5.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env node
let puppeteer = require("puppeteer");
const fs = require('fs');
const { program } = require('commander');
const path = require('path');
const os = require('os');
const axios = require('axios');
const AsyncRetry = require("async-retry");
const { setTimeout } = require("timers/promises");
//initialize CMD arg structure
program
.argument("<source>", "the URL to the tweet or the ID of the tweet.")
.option("-v --verbose", "output extra information", false)
.option("-s --silent", "removes all stdout output, overriding other flags", false)
.option("-h --with-head", "run xvidrip with a head", false)
.argument("[destination]", "the path and filename of the output file")
.parse(process.argv);
let programArgs = program.args;
let programOptions = program.opts();
function verbose_print(strToPrint) {
if(programOptions.silent) return;
if (programOptions.verbose) {
console.log(strToPrint);
}
}
function print(strToPrint)
{
if(programOptions.silent) return;
console.log(strToPrint);
}
let sourceInput = programArgs[0];
let userDestination = programArgs[1];
let userId = sourceInput.match(/\d+$/);
verbose_print(`User id is: ${userId}`);
let embedUrl = `https://platform.twitter.com/embed/Tweet.html?id=${userId}`;
verbose_print(sourceInput);
(async () => {
let fetchResponse;
await AsyncRetry(async(bail) => {
fetchResponse = await getTweetResult();
},
{retries: 5, onRetry: () => {print("Retrying...")}}
);
print("Converting tweet result to JSON...");
let body = await fetchResponse;
verbose_print(body);
let variantArray = body.data.video.variants;
let mp4Url = identifyBestVariant(variantArray);
//todo: find right source to download in the variant_array
let mp4AsBuffer = await downloadMp4(mp4Url);
let fullPath = resolveDownloadPath(userDestination);
if (!fs.existsSync(path.dirname(fullPath))) {
{
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
}
}
fs.writeFileSync(fullPath, mp4AsBuffer, (err) => { console.error(err) });
print(`Downloaded to ${fullPath}`);
process.exit(0);
})();
//We only need puppeteer for this part.
async function getTweetResult()
{
print("Opening puppeteer...");
// Launch the browser and open a new blank page
let config = {
// Whether chrome should simulate
// the absence of connectivity
'offline': false,
// Simulated download speed (bytes/s)
'downloadThroughput': 0,
// Simulated upload speed (bytes/s)
'uploadThroughput': 0,
// Simulated latency (ms)
'latency': 22,
args: ['--disable-cache'],
headless: !programOptions.withHead,
}
const browser = await puppeteer.launch(config);
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36');
let myTweetResult = null;
await page.setRequestInterception(true);
page.on("request", (interceptedRequest) => {
verbose_print(`Intercepted request: ${interceptedRequest.method()} ${interceptedRequest.url()}`);
if (interceptedRequest.url().includes("https://cdn.syndication.twimg.com/tweet-result")) {
myTweetResult = axios.get(interceptedRequest.url());
}
interceptedRequest.continue();
});
page.on("response", (response) => {
let url = response.url();
verbose_print(`Intercepted response: ${url}`);
});
let error_object = null;
const timer = setTimeout(() => {
}, 3000);
timer.then(() => {error_object = new Error("Timed out!");})
//start the process by going to the embed page.
await page.goto(embedUrl);
while (myTweetResult == null) {
if(error_object)
{
browser.close();
throw error_object;
}
}
clearTimeout(timer);
browser.close();
return myTweetResult;
}
function resolveDownloadPath(userDestination) {
print("Resolving download path...");
let dirName = __dirname;
let fileName = "video";
let extName = ".mp4";
if (userDestination) {
dirName = path.dirname(userDestination);
extName = path.extname(userDestination);
fileName = path.basename(userDestination, extName);
if (extName != ".mp4") {
console.warn("Warning: Specified an output file that's not an mp4. xvidrip can only download mp4 files. Please change the output file to an mp4 afterwards.");
}
}
dirName = untildify(dirName);
let baseFilename = fileName;
//check if file exist, if it does add a number to end until one doesn't
let int_modifier = 0;
let break_loop = false;
do {
fullPath = path.join(dirName, fileName + extName);
let doesFileExist = fs.existsSync(fullPath);
if (!doesFileExist) {
fullPath = path.join(dirName, fileName + extName);
break_loop = true;
} else {
int_modifier++;
fileName = baseFilename + "_" + int_modifier;
if (programOptions.verbose) {
console.warn(`Checking if ${fileName} exists already...`);
}
}
} while (!break_loop);
return fullPath;
}
function identifyBestVariant(variantArray) {
verbose_print("Identifying best variant...");
let filtered_variant_array = new Array();
for (let x of variantArray) {
if (x.src.includes("mp4") && x.type == "video/mp4") {
filtered_variant_array.push(x);
}
}
let mp4Url = filtered_variant_array[0].src;
verbose_print(`Best variant identified: ${mp4Url}`);
return mp4Url;
}
async function downloadMp4(mp4Url)
{
print(`Downloading mp4 from '${mp4Url}'...`);
let arrayBuffer = await axios(mp4Url, {
responseType: 'arraybuffer'
});
return Buffer.from(arrayBuffer.data);
}
const homeDirectory = os.homedir();
function untildify(pathWithTilde) {
if (typeof pathWithTilde !== 'string') {
throw new TypeError(`Expected a string, got ${typeof pathWithTilde}`);
}
return homeDirectory ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory) : pathWithTilde;
}