forked from ytmous/ytmous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (122 loc) · 4.08 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
const cluster = require("cluster");
const os = require("os");
if (!process.env.NO_CLUSTERS && cluster.isPrimary) {
const numClusters = process.env.CLUSTERS || (os.availableParallelism ? os.availableParallelism() : (os.cpus().length || 2))
console.log(`Primary ${process.pid} is running. Will fork ${numClusters} clusters.`);
// Fork workers.
for (let i = 0; i < numClusters; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Forking another one....`);
cluster.fork();
});
return true;
}
const express = require("express");
const compression = require("compression");
const YouTubeJS = require("youtubei.js");
const proxyHandler = require("./etc/proxy");
const util = require("./etc/util");
let app = express();
let client;
app.use(compression());
// https://github.com/ytmous/ytmous/pull/25
app.use(function (req, res, next) {
res.setHeader('X-Frame-Options', "SAMEORIGIN");
res.setHeader('X-Content-Type-Options', "nosniff");
next();
});
app.use(express.static(__dirname + "/local/public"));
app.use(express.static(__dirname + "/public"));
app.set("views", [__dirname + "/local/views", __dirname + "/views"]);
app.set("view engine", "ejs");
// Search page
app.get("/s", async (req, res) => {
let query = req.query.q;
let page = parseInt(req.query.p || 1);
if (!query) return res.redirect("/");
try {
res.render("search.ejs", {
res: await client.search(query),
query: query,
page,
});
} catch (error) {
util.sendError(res, error);
}
});
// Watch Page
app.get(["/w/:id", "/embed/:id", "/live/:id", "/shorts/:id", "/watch"], async (req, res) => {
if (!util.validateID(req.params.id) && !util.validateID(req.query.v)) return util.sendInvalidIDError(res);
try {
let id = req.params.id || req.query.v;
let info = await client.getInfo(id);
res.render("watch.ejs", {
id: id, info,
comments: await util.getComments(client, id),
captions: util.getCaptions(info)
});
} catch (error) {
util.sendError(res, error);
}
});
// Playlist page
app.get("/p/:id", async (req, res) => {
res.status(500).render("error.ejs", {
title: "Sorry. Not implemented yet",
content: "Viewing playlist is not available in ytmous nightly yet."
});
});
// Channel page
app.get("/c/:id", async (req, res) => {
try {
const channel = await client.getChannel(req.params.id);
const about = await channel.getAbout();
const videos = await channel.getVideos();
res.render("channel.ejs", { channel, about, videos });
} catch (err) {
console.error("Failed to fetch channel", req.params.id, err);
res.status(500).render("error.ejs", {
title: "Sorry. Something went wrong",
content: "Failed to fetch channel information:\n" + err.toString()
});
}
});
// Comments
app.get("/cm/:id", async (req, res) => {
if (!util.validateID(req.params.id)) return util.sendInvalidIDError(res);
try {
res.render("comments.ejs", {
id: req.params.id,
comments: await client.getComments(req.params.id)
});
} catch (error) {
util.sendError(res, error, "Failed to fetch comments");
}
})
proxyHandler(app);
// 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 available.",
});
});
app.on("error", console.error);
async function initInnerTube() {
try {
client = await YouTubeJS.Innertube.create({ location: process.env.GEOLOCATION || "US", cache: new YouTubeJS.UniversalCache(true, process.env.CACHE_DIR || "./.cache") });
proxyHandler.setClient(client);
const listener = app.listen(process.env.PORT || 3000, () => {
console.log(process.pid, "-- Ready. ytmous is now listening on port", listener.address().port);
});
} catch (e) {
console.error(process.pid, "--- Failed to initialize InnerTube. Trying again in 10 seconds....");
console.error(e);
setTimeout(initInnerTube, 10000);
};
};
// Handle any unhandled promise rejection.
process.on("unhandledRejection", console.error);
initInnerTube();