-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·60 lines (53 loc) · 1.95 KB
/
server.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
#! /usr/bin/env node
const {spawn} = require('child_process')
const path = require('path')
const express = require('express')
const app = express()
const port = 9311
const videoFormat = 'bestvideo[height<2400][width<2400]+bestaudio/best'
const audioFormat = 'bestaudio/best'
const notifierPath = path.join(__dirname, 'notifier.js')
const notify = msg => spawn('node', [notifierPath, msg])
let key
let posArgs = process.argv.slice(2).filter(arg => !arg.startsWith('--key=') ||
((key = arg.slice(6)) && false))
let videoPath = posArgs[0] || process.cwd()
let audioPath = posArgs[1] || videoPath
app.post('/', (req, res) => {
let url = req.query.v
let audioonly = false
let downloadPath = videoPath
if (req.query.a) {
url = req.query.a
audioonly = true
downloadPath = audioPath
}
let keyOk = !key || key == req.query.k
let urlOk = url && url.startsWith('http')
if (keyOk && urlOk) {
let yt
if (audioonly) {
yt = spawn('youtube-dl', ['-f', audioFormat, '--extract-audio',
'--audio-format', 'mp3', '--no-mtime', '-o',
`${path.join(downloadPath, '%(title)s.part')}`, '--exec',
`node ${notifierPath}`, '--add-metadata', '--embed-thumbnail',
'--no-post-overwrites', '--', url],
{stdio: 'ignore'})
} else {
yt = spawn('youtube-dl', ['-f', videoFormat, '--no-mtime', '-o',
`${path.join(downloadPath, '%(title)s.%(ext)s')}`, '--exec',
`node ${notifierPath}`, '--add-metadata', '--embed-subs',
'--all-subs', '--', url],
{stdio: 'ignore'})
}
yt.on('close', code => {
if (code != 0) notify('Something went wrong')
})
} else {
if (!keyOk) notify('Security key is wrong')
else notify('Url not supported')
}
res.set('Access-Control-Allow-Origin', '*')
res.end()
})
app.listen(port)