-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
99 lines (88 loc) · 2.76 KB
/
server.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
const express = require("express"); //step 01 : import express module
const router = express.Router(); //step 02 : create router object
const app = express(); //step 03 : create express app
const expressHandlebars = require("express-handlebars");
const Handlebars = require("handlebars"); // view engine
const bodyParser = require("body-parser");
const youtubedl = require("youtube-dl");
app.use("/", router);
router.use(
bodyParser.urlencoded({
extended: true,
})
);
router.use(
bodyParser.json({
extended: true,
})
);
//Set Handlebars as default view engine
app.engine("handlebars", expressHandlebars({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
Handlebars.registerHelper("toLowerCase", function (str) {
return str.toLowerCase().replace(/\ /g, "-");
});
//CSS
router.use("/cssfiles", express.static(__dirname + "/css"));
//JS
router.use("/jsfiles", express.static(__dirname + "/js"));
//IMG
router.use("/imgfiles", express.static(__dirname + "/img"));
router.get("/", function (req, res) {
//res.sendFile("index.html", { root: __dirname });
res.render("home", { result: {} });
});
router.post("/", function (req, res) {
const videoURL = req.body.ytvurl;
const options = ["--username=user", "--password=hunter2"];
const getVidInfo = async () => {
console.log("Awaiting response : youtube...");
try {
const vidInfo = await youtubedl.getInfo(videoURL, options, function (
err,
info
) {
if (err) throw err;
const datsObj = {
id: info.id,
title: info.title,
thumbnail: info.thumbnail,
url: info.url,
description: info.description,
resolution: info.height,
duration: info.duration,
formats: info.formats,
};
let filesArray = [];
for (let i = 0; i < info.formats.length - 1; i++) {
if (info.formats[i].ext === "webm") {
//console.log("yes!");
filesArray[i] = {
resolution: info.formats[i].format,
type: info.formats[i].ext,
url: info.formats[i].url,
size:
Math.ceil(
Number(info.formats[i].filesize / 1024 / 1024) * 100
) / 100,
};
}
}
// let outArray = [];
// for (const iterator of filesArray) {
// outArray += iterator.resolution;
// }
console.log(filesArray);
res.render("download", { result: { one: datsObj, two: filesArray } });
//res.end(JSON.stringify(info));
});
} catch (error) {
console.log(error);
res.end("NO INTERNET!");
}
};
getVidInfo();
});
app.listen(8080, function () {
console.log("express server is up! : http://localhost:8080/");
});