2019-03-20 03:45:23 +08:00
|
|
|
const static = require("node-static");
|
|
|
|
const app = new static.Server("./app");
|
2019-04-03 01:19:46 +08:00
|
|
|
const port = Number(process.argv[2]) || 8080;
|
2019-03-20 03:45:23 +08:00
|
|
|
|
2019-04-15 00:09:42 +08:00
|
|
|
const cmd = "youtube-dl";
|
|
|
|
//const cmd = "./test.sh";
|
|
|
|
|
2019-04-15 03:02:21 +08:00
|
|
|
function downloadYoutube(q, response) {
|
2019-04-15 00:09:42 +08:00
|
|
|
response.setHeader("Content-Type", "text/plain"); // necessary for firefox to read by chunks
|
|
|
|
// response.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
|
|
|
2019-03-29 23:28:26 +08:00
|
|
|
// FIXME create directory
|
2019-04-15 03:02:21 +08:00
|
|
|
console.log("YouTube downloading", q);
|
2019-03-29 04:43:18 +08:00
|
|
|
let args = [
|
|
|
|
"-f", "bestaudio",
|
|
|
|
"-o", `${__dirname}/_youtube/%(title)s-%(id)s.%(ext)s`,
|
2019-04-15 03:02:21 +08:00
|
|
|
q
|
2019-03-29 04:43:18 +08:00
|
|
|
]
|
2019-04-15 00:09:42 +08:00
|
|
|
let child = require("child_process").spawn(cmd, args);
|
2019-03-20 03:45:23 +08:00
|
|
|
|
2019-04-15 00:09:42 +08:00
|
|
|
child.stdout.setEncoding("utf8").on("data", chunk => response.write(chunk));
|
|
|
|
child.stderr.setEncoding("utf8").on("data", chunk => response.write(chunk));
|
2019-03-29 04:43:18 +08:00
|
|
|
|
|
|
|
child.on("error", error => {
|
|
|
|
console.log(error);
|
|
|
|
response.writeHead(500);
|
|
|
|
response.end(error.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on("close", code => {
|
2019-04-15 00:09:42 +08:00
|
|
|
if (code != 0) { // fixme
|
2019-03-29 04:43:18 +08:00
|
|
|
}
|
2019-04-15 00:09:42 +08:00
|
|
|
response.end();
|
2019-03-29 04:43:18 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleYoutube(request, response) {
|
|
|
|
let str = "";
|
|
|
|
request.setEncoding("utf8");
|
|
|
|
request.on("data", chunk => str += chunk);
|
|
|
|
request.on("end", () => {
|
2019-04-15 03:02:21 +08:00
|
|
|
let q = require("querystring").parse(str)["q"];
|
|
|
|
if (q) {
|
|
|
|
downloadYoutube(q, response);
|
2019-03-29 04:43:18 +08:00
|
|
|
} else {
|
|
|
|
response.writeHead(404);
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onRequest(request, response) {
|
|
|
|
if (request.method == "POST" && request.url == "/youtube") {
|
|
|
|
return handleYoutube(request, response);
|
|
|
|
} else {
|
|
|
|
request.on("end", () => app.serve(request, response)).resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let httpServer = require("http").createServer(onRequest).listen(port);
|
2019-03-20 03:45:23 +08:00
|
|
|
require("ws2mpd").ws2mpd(httpServer, `http://localhost:${port}`);
|
2019-04-15 00:09:42 +08:00
|
|
|
//require("ws2mpd").logging(false);
|