-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (49 loc) · 1.42 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
const Q = require("bluebird")
const colors = require("colors")
const fs = require("fs")
const path = require("path")
const Downloader = require("youtube-manifest-downloader")
const GOOGLE = require("google-cloudstorage-commands")
const uploadFile = (p, BUCKET) => {
return GOOGLE.upload(p, BUCKET, true)
.then(() => GOOGLE.publicRead(BUCKET))
.catch(err => {
console.log(err)
})
}
module.exports = (IDS, BUCKET, options = {}) => {
const save = options.save || __dirname
try {
fs.mkdirSync(save)
} catch (e) {}
return Downloader(IDS, {
save: save,
wgetDownload: true,
itags: options.itags || [],
}).then(r => {
return Q.map(
r,
itagGroup => {
const videoPath = itagGroup[0]
const { name } = path.parse(videoPath)
const jsonPath = path.join(save,`${name}.json`)
console.log(colors.green(`videoPath: ${videoPath}`))
const realItag = videoPath.substring(
videoPath.length - 3,
videoPath.length
)
console.log(colors.green(`ITAG: ${realItag}`))
const sidx = itagGroup[1]
sidx.itag = realItag
fs.writeFileSync(jsonPath, JSON.stringify(sidx))
return uploadFile(videoPath, BUCKET, true).then(() => {
fs.unlinkSync(videoPath)
return uploadFile(jsonPath, BUCKET, true).then(
r => itagGroup
)
})
},
{ concurrency: 1 }
)
})
}