-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreel.js
139 lines (116 loc) · 3.66 KB
/
reel.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
const express = require('express');
const {readdirSync, createWriteStream, unlink} = require("fs");
const ffmpeg = require("ffmpeg");
const instagramGetUrl = require("instagram-url-direct");
const {join} = require("path");
const https = require("https");
const reelRouter = express.Router();
let isRunning = false;
const attempts = new Map();
let validIds = {};
reelRouter.get('/', function (req, res) {
return res.sendFile(join(__dirname, 'reel.html'));
});
reelRouter.get('/index.css', function (req, res) {
return res.sendFile(join(__dirname, 'index.css'));
});
reelRouter.get('/video/:id', (req, res) => {
const { id } = req.params;
if (!id) return res.sendStatus(400);
if (!validIds[id]) return res.sendStatus(404);
isRunning = false;
return res.download(`./reels/${id}.mp4`);
});
reelRouter.get('/audio/:id', (req, res) => {
const { id } = req.params;
if (!id) return res.sendStatus(400);
if (!validIds[id]) return res.sendStatus(404);
isRunning = false;
return res.download(`./reels/${id}.mp3`);
});
reelRouter.post('/audio/:id', async (req, res) => {
const { id } = req.params;
let video = readdirSync('./reels').filter(e => e === `${id}.mp4`);
if (!video.length) return res.sendStatus(404);
video = video[0];
try {
const process = new ffmpeg(`./reels/${video}`);
process.then(function (video) {
video.fnExtractSoundToMP3(`./reels/${id}.mp3`, function (error) {
if (error) {
console.log(error);
return res.sendStatus(500);
}
else {
validIds[id] = true;
return res.sendStatus(200);
}
});
}, function (err) {
console.log(err);
return res.sendStatus(500);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
});
reelRouter.post('/video', async (req, res) => {
const { link } = req.body;
if (!link) return res.status(400).send('Link is required');
if (attempts.get(req.ip) === 10) return res.status(429).send('Too many attempts');
if (attempts.has(req.ip))
attempts.set(req.ip, attempts.get(req.ip) + 1);
else
attempts.set(req.ip, 1);
const fileId = Date.now().toString();
try {
let links = await instagramGetUrl(link);
if (!links || links['results_number'] === 0) return res.status(404).send('No video found');
const downloadLink = links['url_list'][0];
const filePath = join(__dirname, 'reels', `${fileId}.mp4`);
const file = createWriteStream(filePath);
https.get(downloadLink, (response) => {
if (response.statusCode !== 200) {
console.error('Failed to download the video, status code:', response.statusCode);
return res.status(500).send('Failed to download video');
}
response.pipe(file);
file.on('finish', () => {
file.close(() => {
isRunning = true;
const attemptClear = setInterval(() => {
if (!attempts.has(req.ip) || attempts.get(req.ip) === 0) {
return clearInterval(attemptClear);
}
attempts.set(req.ip, attempts.get(req.ip) - 1);
}, 1000 * 60);
validIds[fileId] = true;
return res.json({
id: fileId,
});
});
});
file.on('error', (fileErr) => {
console.error('Error writing file:', fileErr);
res.status(500).send('Error saving the video');
unlink(filePath, () => {}); s
});
}).on('error', (err) => {
console.error('Error downloading the video:', err.message);
res.status(500).send('Error downloading video');
unlink(filePath, () => {});
});
} catch (err) {
console.error('Error processing request:', err.message);
res.status(500).send('Internal server error');
}
});
setInterval(() => {
if(!isRunning) {
const files = readdirSync('./reels');
for (const file of files)
unlink(`./reels/${file}`, () => {});
}
}, 1000 * 60 * 5);
module.exports = reelRouter;