-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEngine.js
111 lines (104 loc) · 3.44 KB
/
Engine.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
const express = require('express');
const cors = require('cors');
const puppeteer = require('puppeteer');
const app = express();
app.use(cors());
const chromeOptions = {
headless: true,
defaultViewport: null,
args: [
"--no-sandbox",
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
],
};
const getVideoInfo = async (videoURL) => {
const browser = await puppeteer.launch(chromeOptions)
try{
const page = await browser.newPage();
await page.goto('https://yt5s.com',{
waitUntil:'networkidle2'
});
await page.$eval('input[name=q]', (el,value) => el.value = value,videoURL);
await (await page.$('button.btn-red')).click();
await page.waitForSelector("div.thumbnail > img[src]",{timeout:5000});
const info = [];
const thumbnail = await page.$eval('.thumbnail img[src]', imgs => imgs.getAttribute('src'));
const title = await page.$eval('.clearfix h3',e => e.innerText);
const channel = await page.$eval('.clearfix p',el => el.innerText);
const length = await page.$eval('.clearfix p.mag0',el => el.innerText);
info.push({thumbnail,title,channel,length});
const formats = await page.$$eval('select#formatSelect option',(options) => options.map(option => {
const format = option.parentElement.label;
const textContent = option.textContent.trim();
let arr = textContent.split(" ");
return {
"value":option.value,
"format":format,
"size":`${arr[1]} ${arr[2]}`
}
}));
info.push(formats);
console.log("[VIDEO FOUND]")
console.log(info[0]);
return info;
}catch(err){
console.log("[VIDEO INFO] ",err);
return {'error':404};
}finally{
await browser.close();
}
}
const getVideoLink = async (videoURL,value,format) => {
const browser = await puppeteer.launch(chromeOptions);
try{
const page = await browser.newPage();
await page.goto('https://yt5s.com',{
waitUntil:'networkidle2'
});
await page.$eval('input[name=q]', (el,value) => el.value = value,videoURL);
await (await page.$('button.btn-red')).click();
await page.waitForSelector('div.thumbnail');
await page.$eval('select#formatSelect optgroup[label="'+format+'"] option[value="'+value+'"]',(option) => {option.selected=true;});
await (await page.$('button#btn-action')).click();
await page.waitForSelector('a.form-control.mesg-convert.success',{visible: true});
const videoLink = await page.$eval('a.form-control.mesg-convert.success',el => el.href);
console.log(`Downloading youtube video ${videoURL} with quality ${value} and ${format}`);
await browser.close();
return videoLink;
}catch(err){
console.log("[DOWNLOAD] ",err);
}finally{
await browser.close();
}
}
app.get('/', (req,res) => {
res.send("Server started");
})
app.get('/download',async (req,res) => {
try{
const videoURL = req.query.url;
const value = req.query.v;
const format = req.query.f;
const videoLink = await getVideoLink(videoURL,value,format);
if(videoLink){
res.redirect(videoLink);
}else res.send({code:404});
}catch(err){
console.log("[GET /download]",err);
}
})
app.get('/getVideo',async (req,res) => {
try{
const videoURL = req.query.url;
const videoData = await getVideoInfo(videoURL);
res.send(videoData);
}catch(err){
console.log("[GET /getVideo] ",err);
}
})
const port = process.env.PORT || 5000;
app.listen(port,async () => {
console.log(`%cGreen Lights! Server is up and running on port ${port}`,
'color:green;');
})