-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (53 loc) · 1.84 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
const express = require('express')
const dotenv = require('dotenv')
dotenv.config()
const axios = require('axios')
const app = express()
const port = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
// function that return a bytes string from a number
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
const value = bytes / 1024 ** i;
return value.toFixed(2) + ' ' + sizes[i];
}
app.get('/', (req, res) => {
res.render('pages/index');
})
app.post('/', async (req, res) => {
const { videoLink } = req.body;
const videoLinkUrlObj = new URL(videoLink);
const videoId = videoLinkUrlObj.searchParams.get('v') || videoLinkUrlObj.pathname.split('/')[1];
if (videoId.length != 11) return res.render('pages/index', { error: 'Invalid video id' });
const response = await axios.post('https://youtubei.googleapis.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8', {
"context": {
"client": {
"hl": "en",
"clientName": "WEB",
"clientVersion": "2.20210721.00.00"
}
},
"videoId": videoId,
})
var downloadLinks = [];
for (var i = 0; i < response.data.streamingData.adaptiveFormats.length; i++) {
var format = response.data.streamingData.adaptiveFormats[i];
downloadLinks.push({
link: format.url,
quality: format.qualityLabel,
size: bytesToSize(format.contentLength),
type: format.mimeType.split(';')[0],
});
}
res.render('pages/index', {
downloadLinks,
title: response.data.videoDetails.title,
thumbnail: response.data.videoDetails.thumbnail.thumbnails[0].url,
});
})
app.listen(port, () => {
console.log(`Youtube Downloader started on port ${port}`)
})