-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
217 lines (141 loc) · 6.71 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const request = require('request');
const axios = require('axios')
const session = require('express-session')
const fetch = require('node-fetch');
const json = require('body-parser/lib/types/json');
app = express();
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
//API KEY
let API_KEY = 'AIzaSyBAjSUYf2Op90nijPE7vQnckgFokRUrC-k'
//get video id from url
var finalresult
app.get('/', (req, res) => {
res.render('index',{data:finalresult})
});
app.get('/about',(req,res)=>{
res.render('about');
})
app.get('/contact-us',(req,res)=>{
res.render('contactUs');
})
app.get('/privacy-policy',(req,res)=>{
res.render('privacyPolicy');
})
var link
var count = 0
var incount = 0
app.post('/', async (req, res) => {
link = req.body.link
link = req.body.playlist_link
link = link.slice(38);
var token = ''
try {
if (link) {
async function DurationCalculate(token) {
let url1 = `https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=${link}&key=${API_KEY}&pageToken=${token}`
const response = await fetch(url1, {
method: 'GET'
})
const details = await response.json()
token = details.nextPageToken;
count = details.pageInfo.totalResults;
// console.log(incount + " :incount");
const videoIds = []
details.items.forEach(item => {
videoIds.push(item.snippet.resourceId.videoId)
})
var minSum = 0;
var hourSum = 0;
var secSum = 0;
let url2 = `https://youtube.googleapis.com/youtube/v3/videos?part=contentDetails&id=${videoIds.join(',')}&key=${API_KEY}`
const response2 = await fetch(url2, {
method: 'GET'
})
var durations = await response2.json()
// duration = duration.items[0].contentDetails.duration;
// Assuming the array is stored in a variable named "duration"
// const durations = duration.map(video => video.contentDetails.duration);
durations = durations.items;
durations.forEach((video) => {
const duration = video.contentDetails.duration;
// let minute = duration.substring(0, duration.indexOf('M'));
// let minute = duration
// minute = duration.replaceAll("PT", "");
let nduration = duration.replaceAll("PT", "");
// console.log(nduration);
const hoursRegex = /^(\d+)H/;
let hour = nduration.match(hoursRegex);
hour = hour ? parseInt(hour[1]) : 0;
// console.log(hour);
hourSum = hourSum+hour;
const minRegEx = /(\d+)M/;//minute regular expression
let minute = nduration.match(minRegEx);
minute = minute ? parseInt(minute[1]):0;
// console.log(minute);
minSum = minSum+ minute;
const secRegEx = /(\d+)S/;//second regular expression
let sec = nduration.match(secRegEx);
sec = sec ? parseInt(sec[1]):0;
//console.log(sec);
secSum = secSum+sec;
// console.log(minute);
// var hour = minute.split(/[HM]/);
// minute = minute.split(/[HM]/);
// console.log(minute);
// if (minute.length == 3) {
// var minute1 = minute[1];
// min1 = parseInt(minute1);
// minSum = minSum + min1;
// //couting hours
// var hour1 = parseInt(hour[0])
// hourSum = hourSum+ hour1
// // counting second
// // console.log(hour);
// var sec1 = parseInt(hour[2]);
// // console.log(sec1);
// } else if (minute.length == 2) {
// var min0 = parseInt(minute[0]);
// minSum = minSum + min0;
// var sec0 = parseInt(minute[1]);
// secSum = secSum+sec0;
// }
});
// console.log("1");
let subTotal = 0
// console.log("token: "+ token);
if (token) {
subTotal = await DurationCalculate(token);
incount++
// console.log("2 : "+subTotal);
}
// console.log("3: printing total");
// console.log("4: printing Subtotal: "+ subTotal);
total = minSum + hourSum*60 + secSum/60 + subTotal;
// console.log("total :"+total);
return total
}
// console.log("0: ");
const result = await DurationCalculate(token);
// console.log("count: "+incount);
// console.log(parseInt(result/60) +" hours : "+ parseInt(result%60)+" min : "+ (parseInt(result%60))%60+" second" );
// finalresult = parseInt(result/60) +" hours : "+ parseInt(result%60)+" min : "+ (parseInt(result%60))%60+" second"
finalresult =
{
minute:parseInt(result) ,
totalVideo:count
}
res.redirect('/');
}else{
finalresult = 0;
res.redirect('/')
}
} catch (error) {
res.send(error)
}
});
app.listen(process.env.PORT || 3000);