-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
94 lines (82 loc) · 2.75 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
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
const express = require("express");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");
const path = require("path");
const chalk = require("chalk");
const fs = require("fs");
const ejs = require("ejs");
const contentDisposition = require("content-disposition");
const ytdl = require("ytdl-core");
const moment = require("moment");
moment.locale("tr");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "./web"));
app.use(express.static("web"));
app.use(
express.static(path.join(__dirname, "/"), {
dotfiles: "allow",
})
);
app.use("/assets", express.static(path.join(__dirname, "./assets/*")));
app.get("/", (req, res) => {
res.render("index");
});
app.post("/download", async (req, res) => {
try {
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
const { type, url, quality } = req.body;
if (!type || !url || !quality) {
return res.status(400).json({
status: 400,
message: "Bad Request",
});
}
if (!ytdl.validateURL(url)) {
return res.status(400).json({
status: 400,
message: "Bad Request",
});
}
const info = await ytdl.getInfo(url);
let { title } = info.videoDetails;
title = title.replace(/[^\w\s]/gi, "");
if (type == "mp3") {
const stream = ytdl(url, {
quality: quality == 1 ? "highestaudio" : "lowestaudio",
filter: "audioonly",
});
//Title ı kısalat ve sonuna kalite ve site adını ekle
res.setHeader(
"Content-Disposition",
contentDisposition(`${title.slice(0, 15)}-${quality == 1 ? 'Yüksek_Kalite' : 'Düşük_Kalite'}-Youtube.mp3`)
);
stream.pipe(res);
} else if (type == "mp4") {
const stream = ytdl(url, {
quality: quality == 1 ? "highest" : "lowest",
filter: "videoandaudio",
});
res.setHeader(
"Content-Disposition",
contentDisposition(`${title.slice(0, 15)}-${quality == 1 ? 'Yüksek_Kalite' : 'Düşük_Kalite'}-Youtube.mp4`)
);
stream.pipe(res);
} else {
res.status(400).json({
status: 400,
message: "Bad Request",
});
}
console.log(chalk.green(`[SERVER]`) + chalk.gray(` [${moment().format("HH:mm:ss")}]`) + chalk.yellow(` ${ip} ip adresli bir kişi yükleme yapıyor;`) + chalk.green(` Format: [.${type}], Kalite: [${quality == 1 ? 'Yüksek' : 'Düşük'}] | ${url}`));
} catch (e) {
console.log(e);
}
});
app.listen(port, () => {
console.log(
chalk.green(`[SERVER]`) + chalk.yellow(` Server started on port ${port}`)
);
});