-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·233 lines (208 loc) · 6.2 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const m3u8stream = require('m3u8stream');
const ytdl = require("ytdl-core");
const ytsr = require("ytsr");
const ytpl = require("ytpl");
const miniget = require("miniget");
const express = require("express");
const ejs = require("ejs");
const { FFmpeg } = require("prism-media");
const app = express();
let args = [
"-i", "-",
"-loglevel", "0",
"-f", "mp4",
"-"
];
// CONFIGURATION //
// Result Limit
// By default, ytsr & ytpl result limit is 100.
// For ytmous, The search result default is 50.
// Change it as many as you want. 0 for all result without limit.
// The smaller, The faster.
const limit = process.env.LIMIT || 0;
// User Agent
// This is where we fake our request to youtube.
const user_agent = process.env.USER_AGENT || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36";
// END OF CONFIGURATION //
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
// Home page
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
// Search page
app.get("/s", async (req, res) => {
let query = req.query.q;
let page = Number(req.query.p || 1);
if (!query) return res.redirect("/");
try {
res.render("search.ejs", {
res: await ytsr(query, { limit, pages: page }),
query: query,
page
});
} catch (error) {
console.error(error);
try {
res.status(500).render("error.ejs", {
title: "ytsr Error",
content: error
});
} catch (error) {
console.error(error);
}
}
});
// Watch Page
app.get("/w/:id", async (req, res) => {
if (!req.params.id) return res.redirect("/");
try {
let info = await ytdl.getInfo(req.params.id);
if (!info.formats.filter(format => format.hasVideo && format.hasAudio).length) {
return res.status(500).render("error.ejs", {
title: "Region Lock",
content: "Sorry. This video is not available for this server country."
});
}
res.render("watch.ejs", {
id: req.params.id, info
});
} catch (error) {
console.error(error);
res.status(500).render("error.ejs", {
title: "ytdl Error",
content: error
});
}
});
// Embed Page
app.get("/e/:id", async (req, res) => {
if (!req.params.id) return res.redirect("/");
try {
let info = await ytdl.getInfo(req.params.id);
if (!info.formats.filter(format => format.hasVideo && format.hasAudio).length) {
return res.status(500).send("This Video is not Available for this Server Region.");
}
res.render('embed.ejs', {
id: req.params.id, info
});
} catch (error) {
console.error(error);
res.status(500).send(error.toString());
}
});
// Playlist page
app.get("/p/:id", async (req, res) => {
if (!req.params.id) return res.redirect("/");
let page = Number(req.query.p || 1);
try {
res.render("playlist.ejs", {
playlist: await ytpl(req.params.id, { limit, pages: page }),
page
});
} catch (error) {
console.error(error);
res.status(500).render("error.ejs", {
title: "ytpl Error",
content: error
});
}
});
// Channel page
app.get("/c/:id", async (req, res) => {
if (!req.params.id) return res.redirect("/");
let page = Number(req.query.p || 1);
try {
res.render("channel.ejs", {
channel: await ytpl(req.params.id, { limit, pages: page }),
page
});
} catch (error) {
console.error(error);
res.status(500).render("error.ejs", {
title: "ytpl Error",
content: error
});
}
});
// Proxy Area
// This is where we make everything became anonymous
// Video Streaming
app.get("/s/:id", async (req, res) => {
if (!req.params.id) return res.redirect("/");
try {
let info = await ytdl.getInfo(req.params.id);
info.formats = info.formats.filter(format => format.hasVideo && format.hasAudio);
if (!info.formats.length) {
return res.status(500).send("This Video is not Available for this Server Region.");
}
let headers = {
'user-agent': user_agent
}
// If user is seeking a video
if (req.headers.range) {
headers.range = req.headers.range;
}
res.setHeader("content-type", "video/mp4");
if (info.videoDetails.isLiveContent && info.formats[0].type == "video/ts") {
return m3u8stream(info.formats[0].url).on('error', (err) => {
res.status(500).send(err.toString());
console.error(err);
}).pipe(new FFmpeg({ args })).pipe(res);
}
let stream = miniget(info.formats[0].url, {
headers
}).on('response', resp => {
if (resp.headers['accept-ranges']) res.setHeader('accept-ranges', resp.headers['accept-ranges']);
if (resp.headers['content-length']) res.setHeader('content-length', resp.headers['content-length']);
if (resp.headers['content-type']) res.setHeader('content-type', resp.headers['content-type']);
if (resp.headers['content-range']) res.setHeader('content-range', resp.headers['content-range']);
if (resp.headers['connection']) res.setHeader('connection', resp.headers['connection']);
if (resp.headers['cache-control']) res.setHeader('cache-control', resp.headers['cache-control']);
stream.pipe(res.status(resp.statusCode));
}).on('error', err => {
res.status(500).send(err.toString());
});
} catch (error) {
res.status(500).send(error.toString());
}
});
// Proxy to i.ytimg.com, Where Video Thumbnail is stored here.
app.get("/vi*", (req, res) => {
let stream = miniget(`https://i.ytimg.com/${req.url.split("?")[0]}`, {
headers: {
"user-agent": user_agent
}
});
stream.on('error', err => {
console.log(err);
res.status(500).send(err.toString());
});
stream.pipe(res);
});
// Proxy to yt3.ggpht.com, Where User avatar is being stored on that host.
app.get("/ytc/*", (req, res) => {
let stream = miniget(`https://yt3.ggpht.com/${req.url}`, {
headers: {
"user-agent": user_agent
}
});
stream.on('error', err => {
console.log(err);
res.status(500).send(err.toString());
});
stream.pipe(res);
});
// 404 Handler
app.use((req, res) => {
res.status(404).render("error.ejs", {
title: "404 Not found",
content: "A resource that you tried to get is not found or deleted."
});
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log("Your app is now listening on port", listener.address().port);
});
// Handle any unhandled promise rejection.
process.on("unhandledRejection", console.error);