-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
103 lines (86 loc) · 2.99 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
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const app = express();
const figlet = require("figlet");
const Innertube = require('youtubei.js');
app.use(morgan("common"));
app.use(cors());
app.use(express.json());
app.get("/", async (req, res) => {
res.send("hello welcome");
})
app.get("/get", async (req, res) => {
const youtube = await new Innertube();
let playlistregex = /\/playlist\?list=/;
let videos = [];
let url = req.query.url;
const info = await youtube.getDetails(url);
console.log(info)
res.json({
id: info.title,
title: info.title,
description: info.description,
view: info.view_count,
date: info.publish_date_text,
thumbnail: info.thumbnail.url,
video: info.formats,
})
});
app.get("/audio", async (req, res, next) => {
const info = await ytdl.getInfo(req.query.id);
const youtube = await new Innertube();
try {
var url = req.query.id;
res.header("Content-Disposition", `attachment; filename="${info.videoDetails.title}-fdciabdul.mp3"`);
youtube.download(url, {
format: 'mp4a', // Optional, defaults to mp4 and I recommend to leave it as it is unless you know what you're doing
quality: '360p', // if a video doesn't have a specific quality it'll fall back to 360p, also ignored when type is set to audio
type: 'audio' // can be “video”, “audio” and “videoandaudio”
}).pipe(res);
} catch (err) {
res.statusMessage = err;
res.sendStatus(400);
}
});
app.get("/search", async (req, res, next) => {
let search = [];
const youtube = await new Innertube();
const videos = await youtube.search(req.query.q);
for (var i = 0; i < videos.length; i++) {
search.push({
id: videos[i].id,
title: videos[i].title,
viewcount: videos[i].viewCount,
});
}
res.json(videos); // 20
});
app.get("/video", async (req, res, next) => {
const info = await ytdl.getInfo(req.query.id);
const youtube = await new Innertube();
try {
var url = req.query.id;
res.header("Content-Disposition", `attachment; filename="${info.videoDetails.title}-fdciabdul.mp3"`);
youtube.download(url, {
format: 'mp4', // Optional, defaults to mp4 and I recommend to leave it as it is unless you know what you're doing
quality: '360p', // if a video doesn't have a specific quality it'll fall back to 360p, also ignored when type is set to audio
type: 'video' // can be “video”, “audio” and “videoandaudio”
}).pipe(res);
} catch (err) {
res.statusMessage = err;
res.sendStatus(400);
}
});
let PORT = process.env.PORT || 8000;
app.listen(PORT, () => {
console.log(figlet.textSync('Youtube Grabber ', {
font: '',
horizontalLayout: 'default',
verticalLayout: true,
width: 60,
whitespaceBreak: true
}));
console.log(`Made By : fdciabdul`);
console.log(`Listening at port :${PORT}`);
});