`
}
html = html.replace("{RESULTS}", addedHTML)
html = html.replace("{CHANNEL_NAME}", info.name)
html = html.replace("{CHANNEL_DESC}", info.description)
for (let index = 0; index < 3; index++) {
html = html.replace("{CHANNEL_ID}", info.id)
}
html = html.replace("{CHANNEL_PFP}", info.thumbnails[0].url)
html = html.replace("{SUB_COUNT}", info.subscribers.pretty)
res.send(html)
})
app.get("/video", async (req, res) => {
var id = req.query.q || req.query.v
var quality = req.query.quality
var range = req.headers.range
res.setHeader("X-Accel-Buffering", "no")
res.setHeader("Content-Type", "video/mp4")
if (ytdl.validateURL(id)) {
id = ytdl.getVideoID(id)
}
if (!ytdl.validateID(id)) {
res.setHeader("Content-Type", "text/html")
res.write("Not a valid video id or url!")
res.end()
return
}
if (range) {
function ready(vidpath) {
if (fs.existsSync(vidpath)) {
const fileSize = videoCache[id].size
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10)
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize - 1
if (start >= fs.statSync(vidpath).size + 1) {
return
}
const chunksize = (end - start) + 1
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
}
res.writeHead(206, head)
if (fs.existsSync(vidpath)) {
fs.createReadStream(vidpath, { start: start }).pipe(res)
}
}
}
if ((id + quality) in videoCache) {
id = id + quality
if ("path" in videoCache[id]) {
ready(videoCache[id].path)
videoCache[id].lastUsed = Date.now()
}
} else {
videoCache[id] = []
cacher(id, quality, ready)
}
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
}
res.writeHead(200, head)
fs.createReadStream(path).pipe(res)
}
var tA = Object.keys(videoCache)
for (let index = 0; index < tA.length; index++) {
const itemName = tA[index];
const item = videoCache[itemName]
const itemPath = item.path
if ("lastUsed" in item && item.lastUsed + (item.duration * 2.5) < Date.now()) {
delete videoCache[itemName]
if (fs.existsSync(itemPath)) {
fs.unlinkSync(itemPath)
}
}
}
})
app.get("/watch", async (req, res) => {
var id = req.query.q || req.query.v || "ubFq-wV3Eic"
var quality = req.query.quality || "sd"
res.setHeader("Content-Type", "text/html")
if (ytdl.validateURL(id)) {
id = ytdl.getVideoID(id)
}
if (!ytdl.validateID(id)) {
res.setHeader("Content-Type", "text/html")
res.write("Not a valid video id or url!")
res.end()
return
}
var vidInfo = (await ytdl.getBasicInfo(id)).videoDetails
var html = fs.readFileSync(playerPath).toString()
if (quality == "sd") {
html = html.replace(`href="?quality=sd&q={VIDEOID}"`, `style="color: white;"`)
} else {
html = html.replace(`href="?quality=hd&q={VIDEOID}"`, `style="color: white;"`)
}
for (let index = 0; index < 2; index++) {
html = html.replace("{VIDEOID}", id)
}
html = html.replace("{VIDEO_QUALITY}", quality)
html = html.replace("{DURATION}", msth(vidInfo.lengthSeconds * 1000))
html = html.replace("{CSS_HEADER}", cssHeader)
for (let index = 0; index < 2; index++) {
html = html.replace("{VIDEO_TITLE}", vidInfo.title)
}
html = html.replace("{VIDEO_DESCRIPTION}", vidInfo.description || "No Description.")
if (!(id in videoCache && videoCache[id]["downloaded"] == true)) {
html = html.replace("{CACHE_WARNING}", `
Please note that this video has not been fully cached, and may have trouble loading!
{DOWNLOAD_PERCENT}% cached as of page load. If content fails to load after a minute, reload the page!
`)
if (id in videoCache && "download%" in videoCache[id]) {
html = html.replace("{DOWNLOAD_PERCENT}", videoCache[id]["download%"])
} else {
html = html.replace("{DOWNLOAD_PERCENT}", "0")
}
} else {
html = html.replace("{CACHE_WARNING}", "