-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
86 lines (73 loc) · 2.14 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
const express = require("express");
const bodyParser = require("body-parser");
const { exit } = require("process");
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Adding user agent to avoid IP bans
const headers = new Headers();
headers.append(
"User-Agent",
"TikTok 26.2.0 rv:262018 (iPhone; iOS 14.4.2; en_US) Cronet"
);
app.get("/", (req, res) => {
res.send("Welcome to TikTok Video Downloader API");
});
app.post("/download", async (req, res) => {
try {
const url = req.body.url;
if (!url) {
return res.status(400).json({ error: "URL is required" });
}
const data = await getVideoNoWM(url);
const videoData = {
url: data.url,
id: data.id,
};
// You can add additional logic here to save or process the video data.
res.json(videoData);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
const getVideoNoWM = async (url) => {
try {
const idVideo = await getIdVideo(url);
const API_URL = `https://api16-normal-c-useast1a.tiktokv.com/aweme/v1/feed/?aweme_id=${idVideo}`;
const request = await fetch(API_URL, {
method: "GET",
headers: headers,
});
const body = await request.text();
try {
var res = JSON.parse(body);
} catch (err) {
console.error("Error:", err);
console.error("Response body:", body);
}
const urlMedia = res.aweme_list[0].video.play_addr.url_list[0];
const data = {
url: urlMedia,
id: idVideo,
};
return data;
} catch (error) {
throw error;
}
};
const getIdVideo = (url) => {
const matching = url.includes("/video/");
if (!matching) {
throw new Error("URL not found");
}
const idVideo = url.substring(url.indexOf("/video/") + 7, url.length);
return idVideo.length > 19
? idVideo.substring(0, idVideo.indexOf("?"))
: idVideo;
};