-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
359 lines (325 loc) · 10 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env node
const inquirer = require("inquirer")
const fs = require("fs")
const path = require("path")
const chalk = require("chalk")
const figlet = require("figlet")
const Spinner = require("cli-spinner").Spinner
const axios = require("axios")
const Vimeo = require("vimeo").Vimeo
const spinner = new Spinner("%s Checking for previous download info... ")
spinner.setSpinnerDelay(100)
var LOGPATH
var ID
var SECRET
var TOKEN
var FILEPATH
var TOTALSIZE = 0
var TOTALBYTESDOWNLOADED = 0
var TOTALFILESDOWNLOADED = 0
var TOTALFILES
var TOTALPAGES
var FILES = []
var TOTAL_LOADING_DONE = 0
//what the fuck are global variables. what do you mean this is a bad
const run = async () => {
init()
spinner.start()
try {
spinner.stop()
if(fs.existsSync('files.json')) {
console.log("")
const answers = await askContinue()
if(answers.RESTART.toLowerCase()=="y"||answers.RESTART.toLowerCase()=="yes") {
const files = require(path.resolve(__dirname,"files.json"))
ID = files.ID
SECRET = files.SECRET
TOKEN = files.TOKEN
FILEPATH = files.FILEPATH
TOTALFILES = files.TOTALFILES
TOTALFILESDOWNLOADED = files.TOTALFILESDOWNLOADED
TOTALBYTESDOWNLOADED = files.TOTALBYTESDOWNLOADED
FILES = files.FILES
fs.writeFile(path.resolve(__dirname,FILEPATH,'LOG'),"",(err) => {
if(err) throw err
LOGPATH = path.resolve(__dirname,FILEPATH,'LOG')
downloadFiles()
})
} else {
console.log("")
runTraditional()
}
} else {
console.log("")
runTraditional()
}
} catch(err) {
console.error(err)
}
}
const init = () => {
console.log(
chalk.white.bold.bgCyan(
figlet.textSync("Vimeo DL", {
font: "Alligator2",
horizontalLayout: "default",
verticalLayout: "default"
})
)
)
console.log(" ")
console.log(" ")
}
const exitCantCreateClient = () => {
console.log("Something went wrong... we can't create the vimeo client!")
process.exit()
}
const askQuestions = () => {
const questions = [
{
name: "TOKEN",
type: "input",
message: "Input Vimeo app access token (make sure that it has the video_file scope):"
},
{
name: "ID",
type: "input",
message: "Input client ID:"
},
{
name: "SECRET",
type: "input",
message: "Input client secret:"
},
{
name: "FILEPATH",
type: "input",
message: "Specify filepath to save videos to:"
}
]
return inquirer.prompt(questions)
}
const askContinue = () => {
const filesJson = require(path.resolve(__dirname,"files.json"))
const questions = [
{
name: "RESTART",
type: "input",
message: `Would you like to continue your download? ${filesJson.TOTALFILESDOWNLOADED}/${filesJson.TOTALFILES} videos downloaded (y/n)`
}
]
return inquirer.prompt(questions)
}
const downloadFiles = () => {
spinner.setSpinnerTitle("%s Preparing files for download...")
FILES.forEach(e => {
spinner.isSpinning() ? null : spinner.start()
let uri = (e.uri.split("/"))[2]
if(e.files.length) {
let dlLink = false
let fileList = e.files.filter(e => e.quality=='hd'||e.quality=='sd')
let fileToUse
if(fileList.length) {
fileList.sort((a,b) => b.width-a.width)
fileToUse = fileList[0]
dlLink = fileToUse.link
}
if(dlLink) {
let type = (fileToUse.type.split("/"))[1]
let filename = uri.concat(".",type)
e.trueURI = uri
e.filename = filename
e.link = dlLink
TOTALSIZE += fileToUse.size
} else {
fs.open(LOGPATH,'a',777,(e,id) => {
fs.write(id, `Error getting download link for uri ${uri}\n`,null,'utf8',() => {
fs.close(id, () => {
console.log("")
console.log(`Error getting download link for uri ${uri}`)
})
})
})
}
} else {
fs.open(LOGPATH,'a',777,(e,id) => {
fs.write(id, `Files corrupted or missing for uri ${uri}\n`,null,'utf8',() => {
fs.close(id, () => {
console.log("")
console.log(`Files corrupted or missing for uri ${uri}`)
})
})
})
}
})
console.log("")
spinner.setSpinnerTitle("%s Downloading files...")
dl(FILES[TOTALFILESDOWNLOADED].link,FILEPATH,FILES[TOTALFILESDOWNLOADED].filename)
}
const dl = async (link,filepath,filename) => {
let filesJson = require(path.resolve(__dirname,"files.json"))
if(link) {
const writer = fs.createWriteStream(path.resolve(__dirname,filepath,filename))
try {
const response = await axios(
{
url:link,
method:'GET',
responseType:'stream'
}
)
spinner.isSpinning() ? null : spinner.start()
response.data.on('data', (data) => {
TOTALBYTESDOWNLOADED += Buffer.byteLength(data)
const progress = 3*Math.round(100*(TOTALBYTESDOWNLOADED/TOTALSIZE))/10
spinner.setSpinnerTitle(`%s Downloaded ${TOTALFILESDOWNLOADED}/${TOTALFILES} FILES | ${Math.round(10*TOTALBYTESDOWNLOADED/(1024*1024*1024))/10}GB/${Math.round(10*TOTALSIZE/(1024*1024*1024))/10}GB |${"=".repeat(Math.round(progress))}${"-".repeat(Math.round(30-progress))}|`)
})
response.data.on('end', () => {
TOTALFILESDOWNLOADED++
filesJson.TOTALFILESDOWNLOADED = TOTALFILESDOWNLOADED
filesJson.TOTALBYTESDOWNLOADED = TOTALBYTESDOWNLOADED
filesJson = JSON.stringify(filesJson, null, 2)
fs.writeFile('files.json',filesJson, (err) => {
if(err) throw err
if(TOTALFILESDOWNLOADED==TOTALFILES) {
finish()
} else {
dl(FILES[TOTALFILESDOWNLOADED].link,FILEPATH,FILES[TOTALFILESDOWNLOADED].filename)
}
})
})
response.data.on('error',(error) => {
console.log(error)
})
response.data.pipe(writer)
} catch(error) {
fs.open(LOGPATH,'a',777,(e,id) => {
fs.write(id, `Error downloading file with uri ${FILES[TOTALFILESDOWNLOADED].uri}, http request returned with status code ${error.response.status}\n`,null,'utf8',() => {
fs.close(id, () => {
console.log("")
console.log(`Error downloading file with uri ${FILES[TOTALFILESDOWNLOADED].uri}, http request returned with status code ${error.response.status}`)
})
})
})
TOTALFILESDOWNLOADED++
filesJson.TOTALFILESDOWNLOADED = TOTALFILESDOWNLOADED
filesJson = JSON.stringify(filesJson, null, 2)
fs.writeFile('files.json',filesJson, (err) => {
if(err) throw err
dl(FILES[TOTALFILESDOWNLOADED].link,FILEPATH,FILES[TOTALFILESDOWNLOADED].filename)
})
}
return new Promise((resolve,reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
} else {
fs.open(LOGPATH,'a',777,(e,id) => {
fs.write(id, `Did not download file ${FILES[TOTALFILESDOWNLOADED].uri}\n`,null,'utf8',() => {
fs.close(id, () => {
console.log("")
console.log(`Did not download file ${FILES[TOTALFILESDOWNLOADED].uri}`)
})
})
})
TOTALFILESDOWNLOADED++
filesJson.TOTALFILESDOWNLOADED = TOTALFILESDOWNLOADED
filesJson = JSON.stringify(filesJson, null, 2)
fs.writeFile('files.json',filesJson, (err) => {
if(err) throw err
dl(FILES[TOTALFILESDOWNLOADED].link,FILEPATH,FILES[TOTALFILESDOWNLOADED].filename)
})
}
}
const finish = () => {
spinner.stop()
if(TOTALFILES > 200) {
console.log("Oh thank goodness. It's over. It's finally over.")
} else {
console.log("Done! Thanks for using dl-vimeo!")
}
process.exit()
}
const recordFiles = (files) => {
let filesToRecord = {
ID: ID,
SECRET: SECRET,
FILEPATH: FILEPATH,
TOTALFILES: TOTALFILES,
TOTALFILESDOWNLOADED: TOTALFILESDOWNLOADED,
TOTALBYTESDOWNLOADED: TOTALBYTESDOWNLOADED,
FILES: files
}
filesToRecord = JSON.stringify(filesToRecord, null, 2)
fs.writeFile('files.json', filesToRecord, (err) => {
if(err) throw err
console.log("")
console.log("File data written to files.json")
downloadFiles()
})
}
const loadVideoData = (pages) => {
const vimeoClient = ID ? new Vimeo(ID, SECRET, TOKEN) : exitCantCreateClient()
for(let i=1; i<=pages; i++) {
vimeoClient.request({
method:'GET',
path:`/me/videos?per_page=100&page=${i}`,
query: {
per_page:100,
fields:"uri,files"
}
},(error,body,status_code,headers) => {
if(error) {
console.log(error)
console.log(`HTTP STATUS CODE: ${status_code}`)
process.exit()
} else {
body.data.forEach(e => {
FILES.push(e)
TOTAL_LOADING_DONE++
spinner.setSpinnerTitle(`%s Loading all video data from Vimeo API... ${TOTAL_LOADING_DONE}/${TOTALFILES}`)
})
if(TOTAL_LOADING_DONE==TOTALFILES) {
spinner.setSpinnerTitle("%s Loading complete! Making sure everything is in order... ")
recordFiles(FILES)
}
}
})
}
}
const vimeoInitialTest = (error, body, status_code, headers) => {
spinner.isSpinning() ? spinner.stop() : null
if(error) {
console.log(error)
process.exit()
} else {
console.log(`total videos found: ${body.total}`)
TOTALFILES = body.total
TOTALPAGES = Math.ceil(body.total/100)
spinner.setSpinnerTitle("%s Loading all video data from Vimeo API... ")
spinner.start()
loadVideoData(TOTALPAGES)
}
}
const runTraditional = async () => {
const answers = await askQuestions()
ID = answers.ID
SECRET = answers.SECRET
TOKEN = answers.TOKEN
FILEPATH = answers.FILEPATH
FILEPATH = FILEPATH=="" ? "videos/" : FILEPATH
fs.writeFile(path.resolve(__dirname,FILEPATH,'LOG'),"",(err) => {
if(err) throw err
LOGPATH = path.resolve(__dirname,FILEPATH,'LOG')
})
const vimeoClient = new Vimeo(ID, SECRET, TOKEN)
vimeoClient.request({
method: 'GET',
path: '/me/videos',
query: {
per_page: 1,
fields: "uri"
}
}, vimeoInitialTest)
}
run()