forked from ondras/cyp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (113 loc) · 3.45 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
const static = require("node-static");
const app = new static.Server("./app");
const port = Number(process.argv[2]) || process.env.PORT || 8080;
let tickets = [];
const cmd = "youtube-dl";
function escape(arg) {
return `'${arg.replace(/'/g, `'\\''`)}'`;
}
function searchYoutube(q, limit, response) {
response.setHeader("Content-Type", "text/plain"); // necessary for firefox to read by chunks
console.log("YouTube searching", q, limit);
q = escape(`ytsearch${limit}:${q}`);
const command = `set -o pipefail; ${cmd} -j ${q} | jq "{id,title}" | jq -s .`;
require("child_process").exec(command, {shell:"bash"}, (error, stdout, stderr) => {
if (error) {
console.log("error", error);
response.writeHead(500);
response.end(error.message);
} else {
response.end(stdout);
}
});
}
function downloadYoutube(id, response) {
response.setHeader("Content-Type", "text/plain"); // necessary for firefox to read by chunks
console.log("YouTube downloading", id);
let args = [
"-f", "bestaudio",
"-o", `${__dirname}/_youtube/%(title)s-%(id)s.%(ext)s`,
"--",
id
]
let child = require("child_process").spawn(cmd, args);
child.stdout.setEncoding("utf8").on("data", chunk => response.write(chunk));
child.stderr.setEncoding("utf8").on("data", chunk => response.write(chunk));
child.on("error", error => {
console.log(error);
response.writeHead(500);
response.end(error.message);
});
child.on("close", code => {
if (code != 0) { // fixme
}
response.end();
});
}
function handleYoutubeSearch(url, response) {
let q = url.searchParams.get("q");
let limit = url.searchParams.get("limit") || "";
if (q) {
searchYoutube(q, limit, response);
} else {
response.writeHead(404);
response.end();
}
}
function handleYoutubeDownload(request, response) {
let str = "";
request.setEncoding("utf8");
request.on("data", chunk => str += chunk);
request.on("end", () => {
let id = require("querystring").parse(str)["id"];
if (id) {
downloadYoutube(id, response);
} else {
response.writeHead(404);
response.end();
}
});
}
function handleTicket(request, response) {
request.resume().on("end", () => {
let ticket = require("crypto").randomBytes(16).toString("hex");
tickets.push(ticket);
if (tickets.length > 10) { tickets.shift(); }
let data = {ticket};
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify(data));
});
}
function onRequest(request, response) {
const url = new URL(request.url, "http://localhost");
switch (true) {
case request.method == "GET" && url.pathname == "/youtube":
return handleYoutubeSearch(url, response);
case request.method == "POST" && url.pathname == "/youtube":
return handleYoutubeDownload(request, response);
case request.method == "POST" && url.pathname == "/ticket":
return handleTicket(request, response);
default:
return request.on("end", () => app.serve(request, response)).resume();
}
}
function requestValidator(request) {
let ticket = request.resourceURL.query["ticket"];
let index = tickets.indexOf(ticket);
if (index > -1) {
tickets.splice(index, 1);
return true;
} else {
return false;
}
}
let httpServer = require("http").createServer(onRequest).listen(port);
let passwords = {};
try {
passwords = require("./passwords.json");
console.log("loaded passwords.json file");
} catch (e) {
console.log("no passwords.json found");
}
require("ws2mpd").ws2mpd(httpServer, requestValidator, passwords);
require("ws2mpd").logging(false);