-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (26 loc) · 1.32 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
const axios = require('axios');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static('public'));
app.get('/video/download', async (req, res) => {
const { url } = req.query;
if(!url) return res.status(400).json({ error: 'Missing URL parameter' });
const vaildUrls = [ "https://www.tiktok.com/", "http://www.tiktok.com", "https://tiktok.com", "http://tiktok.com/"]
if(!vaildUrls.some(vaildUrl => url.includes(vaildUrl))) return res.status(400).json({ error: 'Invalid URL' });
try {
const id = url.split("/")[5].split("?")[0];
const respone = await axios.get(`https://api16-normal-c-useast1a.tiktokv.com/aweme/v1/feed/?aweme_id=${id}`);
const json = respone.data;
const video = json.aweme_list[0].video.play_addr.url_list[0];
const videoID = json.aweme_list[0].aweme_id;
res.setHeader('Content-Type', 'video/mp4');
res.setHeader('Content-Disposition', `attachment; filename=${videoID}.mp4`);
const response = await axios.get(video, { responseType: 'stream' });
response.data.pipe(res);
} catch(err) {
return res.status(400).json({ error: 'Invalid URL' });
}
});
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));