-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
executable file
·49 lines (41 loc) · 1.18 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
#!/usr/bin/env node
const open = require('open-pip')
const ytdl = require('ytdl-core')
const ora = require('ora')
const getStdin = require('get-stdin')
const spinner = ora().start()
const help = () => spinner.info('Usage: open-pip <path | youtube-url | twitch-url | other-video-url>')
const youtubeUrls = [
new RegExp('www.youtube'),
new RegExp('youtu.be')
]
const processStdin = stdin => {
if (stdin) return stdin.trim()
const input = process.argv[2]
if (!input) {
help()
throw new Error('No url supplied')
}
return input
}
const processInput = async input => {
let file = input
for (const url of youtubeUrls) {
if (url.test(input)) {
const result = await ytdl.getInfo(input, {})
file = result.formats[0].url
spinner.stopAndPersist({ symbol: '🚀', text: 'Found youtube link' })
}
}
return file
}
getStdin()
.then(processStdin)
.then(async input => open(await processInput(input)))
.then(() => spinner.stopAndPersist({ symbol: '🌟', text: 'Running!' }))
.catch((err) => {
spinner.fail('Something went wrong')
if (err) console.log(err)
})
module.exports.processStdin = processStdin
module.exports.processInput = processInput