SEARCH CACHING!!!
This commit is contained in:
parent
a6bcbc1c7b
commit
b6b6e44992
2 changed files with 57 additions and 15 deletions
49
index.js
49
index.js
|
@ -13,8 +13,8 @@ const cssPath = path.join(staticPath, 'mainStyle.css')
|
||||||
|
|
||||||
const resources = path.join(__dirname, 'resources')
|
const resources = path.join(__dirname, 'resources')
|
||||||
|
|
||||||
const cachePath = path.join(__dirname, 'cache')
|
const cachePath = path.join(__dirname, 'cache/videos')
|
||||||
// const cachePath = "/tmp/cache/SimpleTube"
|
const searchCacheDur = (process.env.SEARCH_DUR || 24) * 3600000
|
||||||
|
|
||||||
const playerPath = path.join(resources, 'player.html')
|
const playerPath = path.join(resources, 'player.html')
|
||||||
const searchPath = path.join(resources, 'searchPage.html')
|
const searchPath = path.join(resources, 'searchPage.html')
|
||||||
|
@ -31,6 +31,7 @@ fs.mkdirSync(cachePath)
|
||||||
|
|
||||||
|
|
||||||
var videoCache = {}
|
var videoCache = {}
|
||||||
|
var searchCache = {}
|
||||||
|
|
||||||
var app = express()
|
var app = express()
|
||||||
|
|
||||||
|
@ -42,13 +43,13 @@ app.listen(PORT, () => {
|
||||||
console.log("Simpletube is now listening on port: " + PORT)
|
console.log("Simpletube is now listening on port: " + PORT)
|
||||||
})
|
})
|
||||||
|
|
||||||
function cacher(id, ready) {
|
async function cacher(id, ready) {
|
||||||
console.log(id)
|
|
||||||
vidpath = path.join(__dirname, `cache/${id}.mp4`)
|
vidpath = path.join(__dirname, `cache/${id}.mp4`)
|
||||||
|
|
||||||
var debounce = true
|
var debounce = true
|
||||||
|
|
||||||
var dp = 0
|
var dp = 0
|
||||||
|
var vidInfo = await ytdl.getBasicInfo(id)
|
||||||
var video = ytdl(id, { filter: 'videoandaudio', quality: "highest", format: 'mp4' })
|
var video = ytdl(id, { filter: 'videoandaudio', quality: "highest", format: 'mp4' })
|
||||||
.on("progress", (chunk, ct, et) => {
|
.on("progress", (chunk, ct, et) => {
|
||||||
|
|
||||||
|
@ -59,7 +60,8 @@ function cacher(id, ready) {
|
||||||
"size": et,
|
"size": et,
|
||||||
"downloaded": false,
|
"downloaded": false,
|
||||||
"download%": 0,
|
"download%": 0,
|
||||||
"lastUsed": new Date().getTime()
|
"lastUsed": new Date().getTime(),
|
||||||
|
"duration": (vidInfo.videoDetails.lengthSeconds + 1) * 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
ready(vidpath, fs.readFileSync(vidpath))
|
ready(vidpath, fs.readFileSync(vidpath))
|
||||||
|
@ -81,7 +83,8 @@ function cacher(id, ready) {
|
||||||
app.get("/search", async (req, res) => {
|
app.get("/search", async (req, res) => {
|
||||||
var search = req.query.q || "How to search on SimpleTube"
|
var search = req.query.q || "How to search on SimpleTube"
|
||||||
res.setHeader("Content-Type", "text/html")
|
res.setHeader("Content-Type", "text/html")
|
||||||
youtube.search(search, { type: "all" }).then((results) => {
|
|
||||||
|
function searchReturn(results) {
|
||||||
var videos = results.videos
|
var videos = results.videos
|
||||||
|
|
||||||
var html = fs.readFileSync(searchPath).toString()
|
var html = fs.readFileSync(searchPath).toString()
|
||||||
|
@ -125,20 +128,20 @@ app.get("/search", async (req, res) => {
|
||||||
addedHTML += `
|
addedHTML += `
|
||||||
<div class="col-xxl-4 col-sm-6 resultContainer">
|
<div class="col-xxl-4 col-sm-6 resultContainer">
|
||||||
<div class="videoResult container-fluid row">
|
<div class="videoResult container-fluid row">
|
||||||
<div class=" col-lg-6 thumbparent">
|
<div class="col-lg-6 thumbparent">
|
||||||
<a class="videoLink" href="/watch?v=${result.id}">
|
<a class="videoLink" href="/watch?v=${result.id}">
|
||||||
<img class="thumbnail" src="${result.thumbnail}">
|
<img class="thumbnail" src="${result.thumbnail}">
|
||||||
<p style="display: block; text-align: left;">${result.durationString}</p>
|
<p style="display: block; text-align: left;">${result.durationString}</p>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div class=" col-lg-6">
|
<div class="col-lg-6">
|
||||||
<a class="videoLink" href="/watch?v=${result.id}">
|
<a class="videoLink" href="/watch?v=${result.id}">
|
||||||
<p style="font-size: 1.25rem;">${result.title || "No Title Found"}</p>
|
<p style="font-size: 1.25rem;">${result.title || "No Title Found"}</p>
|
||||||
<p class="resultDescription">${result.description.substring(0, 75) + "..." || "No Description"}</p>
|
<p class="resultDescription">${result.description.substring(0, 75) + "..." || "No Description"}</p>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="display: inline-block; width: 100%; ">
|
<div style="display: inline-block; width: 100%;">
|
||||||
<a style="color: white; margin: 10px; display: inline-block;" href="${result.channel.link}">
|
<a style="color: white; margin: 10px; display: inline-block;" href="${result.channel.link}">
|
||||||
<img src="${result.channel.thumbnail}" class="minipfp">
|
<img src="${result.channel.thumbnail}" class="minipfp">
|
||||||
${result.channel.name}
|
${result.channel.name}
|
||||||
|
@ -150,7 +153,31 @@ app.get("/search", async (req, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send(html.replace("{RESULTS}", addedHTML))
|
res.send(html.replace("{RESULTS}", addedHTML))
|
||||||
})
|
}
|
||||||
|
|
||||||
|
var tA = Object.keys(searchCache)
|
||||||
|
|
||||||
|
|
||||||
|
for (let index = 0; index < tA.length; index++) {
|
||||||
|
itemName = tA[index]
|
||||||
|
const item = searchCache[itemName];
|
||||||
|
|
||||||
|
if (item[1] < Date.now()) {
|
||||||
|
console.log("Deleted!")
|
||||||
|
delete searchCache[search]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search in searchCache) {
|
||||||
|
searchReturn(searchCache[search][0])
|
||||||
|
searchCache[search][1] = Date.now() + searchCacheDur
|
||||||
|
} else {
|
||||||
|
youtube.search(search, { type: "all" })
|
||||||
|
.then((result)=> {
|
||||||
|
searchReturn(result)
|
||||||
|
searchCache[search] = [result, Date.now() + searchCacheDur]
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/video", async (req, res) => {
|
app.get("/video", async (req, res) => {
|
||||||
|
@ -212,7 +239,7 @@ app.get("/video", async (req, res) => {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
videoCache[id] = []
|
videoCache[id] = []
|
||||||
var video = cacher(id, ready)
|
var video = await cacher(id, ready)
|
||||||
video.pipe(fs.createWriteStream(vidpath))
|
video.pipe(fs.createWriteStream(vidpath))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -117,25 +117,39 @@ p {
|
||||||
background-color: black;
|
background-color: black;
|
||||||
border: 2px gray solid;
|
border: 2px gray solid;
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
padding: 3px;
|
padding: 2px;
|
||||||
margin: auto;
|
margin: auto;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
@media (max-width: 720px) {
|
||||||
.resultContainer {
|
.resultContainer {
|
||||||
max-width: 450px;
|
max-width: 450px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (min-width: 720px) and (max-width: 992px) {
|
||||||
|
.resultContainer {
|
||||||
|
max-width: 450px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (min-width: 992px) {
|
||||||
|
.resultContainer {
|
||||||
|
max-width: 550px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.resultContainer {
|
.resultContainer {
|
||||||
margin: 5px auto;
|
margin: 0 auto;
|
||||||
|
padding: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.thumbnail {
|
.thumbnail {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
max-width: 350px;
|
||||||
margin: auto;
|
margin: auto;
|
||||||
border-radius: 20px;
|
border-radius: 20px;
|
||||||
aspect-ratio: 16/9;
|
aspect-ratio: 16/9;
|
||||||
|
@ -167,11 +181,12 @@ p {
|
||||||
}
|
}
|
||||||
|
|
||||||
main {
|
main {
|
||||||
padding: 10px;
|
padding: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.pfp {
|
.pfp {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
max-width: 150px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
border: white 2px solid;
|
border: white 2px solid;
|
||||||
margin: 15px auto;
|
margin: 15px auto;
|
||||||
|
|
Loading…
Reference in a new issue