-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
243 lines (176 loc) · 5.32 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const express = require('express')
const usetube = require('usetube')
const bodyParser = require('body-parser')
const path = require('path')
const multer = require('multer')
const { equal } = require('assert')
const { Parser } = require('json2csv');
const fs = require('fs');
const ytdl = require('ytdl-core')
const cors= require('cors')
const { text } = require('body-parser')
// import userController from './controllers/userController';
const app = express();
app.use(express.json())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'))
app.set('views', path.join(__dirname+'/views'))
app.set('view engine', 'hbs')
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/home.html')
// let data = await getChannel('carryminati')
// console.log(data)
})
// app.get('/download', userController.download);
app.post('/', async function (req, res) {
let query = req.body.search;
let option = req.body.drop;
if (option == "Search Video") {
console.log('Search Video')
let data = await getVideo(query);
// res.send(data)
// const json2csvParser = new Parser();
// const csv = json2csvParser.parse(data);
// const downloadResource = (res, fileName, fields, data) => {
// const json2csv = new Parser({ fields });
// const csv = json2csv.parse(data);
// res.header('Content-Type', 'text/csv');
// res.attachment(fileName);
// return res.send(csv);
// }
// downloadResource(res,fileName,fields,data);
res.render('videos', {
video: data.videos,
// message: 'Greetings from geekforgeeks'
})
}
else if (option == "Search Channel") {
let data = await getChannel(query);
console.log('Search Channel')
if(data=='') res.send('Data Not Found')
// res.send(data)
res.render('channels', {
video: data.channels,
// message: 'Greetings from geekforgeeks'
})
// res.send(data);
}
else if( option =="Download Video")
{
// Download the Video
// res.json({url:query});
// let query = getVideoId(query);
let is_utube = parseVideoUrl(query);
console.log(is_utube)
// if(is_utube==false)
// {
// // res.redirect('/')
// res.send('Input Data Unsupported. Kindly Enter Correct Link in the Field');
// }
// else
// {
res.header('Content-Disposition','attachment ; filename="download.mp4')
ytdl
( query,{
format: 'mp4'
}).pipe(res);
// }
// res.redirect(__dirname+'../public/thankyou.html')
}
else if( option =="Video Desc")
{
let id = getVideoId(query);
console.log(query)
console.log(id)
let data = await videoDesc(id);
res.send(data);
}
else if( option =="Channel Desc")
{
let id= getChannelId(query);
console.log(query)
console.log(id)
let data = await channelDesc(id);
res.send(data);
}
})
app.get('/auto',function(req,res)
{
res.send('hu')
})
app.listen(5000, function (req, res) {
console.log(`Server has started on port https://localhost:5000/`);
})
async function getChannel(query) {
let data = await usetube.searchChannel(query)
return data;
}
async function getVideo(query) {
let data = await usetube.searchVideo(query)
return data;
}
async function searchChannel(query) {
let data = await usetube.searchChannel(query)
return data;
}
async function videoDesc(query)
{
let data= await usetube.getVideoDesc(query);
return data;
}
async function channelDesc(query)
{
let data= await usetube.getChannelDesc(query);
return data;
}
function getChannelId(query)
{
// let id = query;
// var a="";
// let i=0;
// while(id[i]!='?')
// {
// i++;
// }
// for(let j=i+1;j<id.length;j++)
// {
// a+=id[j];
// }
// let a = slice(, end)
let a="";
for(let i=32;i<query.length;i++)
{
a+=query[i];
}
return a;
}
function getVideoId(query)
{
let id = query;
var a="";
let i=0;
while(id[i]!='?')
{
i++;
}
for(let j=i+3;j<id.length;j++)
{
a+=id[j];
}
return a;
}
function download_data(query)
{
const json2csvParser = new Parser();
const csv = json2csvParser.parse(query);
fs.writeFileSync('text.csv', csv, (err) => {
if (err) throw err;
console.log('The lyrics were updated!');
});
res.download(__dirname+'/text.csv')
}
function parseVideoUrl(url) {
const regExp ='^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$';
const match = url.match(regExp);
return match;
}