-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (67 loc) · 2.34 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
const express = require('express');
require('dotenv').config();
const app = express();
const bodyParser = require('body-parser');
const { getTime } = require('./src/getTime')
const { getData } = require('./src/getData')
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
var error = "";
app.get('/', (req, res) => {
res.render('index', { error: error, totalVideos: "", videoLen: "" });
})
app.post('/', (req, res) => {
var url = req.body.url;
if (url.length < 38 && url.slice(0, 38) != "https://www.youtube.com/playlist?list=") {
error = "Enter correct URL";
res.render('index', { error: error, totalVideos: "", videoLen: "" });
}
else {
const ID = url.slice(38, url.length);
// console.log(url);
var start, end;
if (req.body.start && req.body.end) {
if (req.body.start) {
start = req.body.start;
}
if (req.body.end) {
end = req.body.end;
}
}
else if (req.body.start) {
error = "Please enter 'To' Field";
res.render('index', { error: error, totalVideos: "", videoLen: "" });
}
else if (req.body.end) {
error = "Please enter 'From' Field";
res.render('index', { error: error, totalVideos: "", videoLen: "" });
}
(async () => {
let [totalV, Videos] = await getData(ID);
if (!(start || end)) {
start = 0;
end = Videos.length - 1;
}
else {
start = start - 1;
if (end == totalV) {
end = end - 1;
}
}
if(start<=end){
let Time = await getTime(Videos, start, end);
if (!Time) {
res.render('index', { error: "Something went Wrong!!!", totalVideos: "", videoLen: "" });
}
res.render('index', { error: error, totalVideos: totalV, videoLen: Time });
}
else{
res.render('index', { error: "Incorrect Input for Range", totalVideos:"", videoLen: "" });
}
})();
}
})
app.listen(process.env.PORT, (req, res) => {
console.log("listening on port " + process.env.PORT);
})