-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiktok-dl.js
244 lines (215 loc) · 9.15 KB
/
tiktok-dl.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import axios from 'axios';
// Add user-agent to avoid ban
const headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
};
/**
* Creates a promise that resolves after a specified delay.
* @param {number} ms - The delay in milliseconds.
* @returns {Promise<void>} - A promise that resolves after the delay.
*/
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
/**
* Downloads media (video or image slides) from a TikTok URL without watermarks.
* @param {string} url - The TikTok post URL to download media from.
* @returns {Promise<{ id: string, url: string, username: string | null, media: { type: string, data: Buffer }[] } | { error: string }>} - Promise that resolves with the downloaded media buffers and information, or an error message if media not found.
*/
const downloadFromTikTok = async (url) => {
try {
const finalUrl = await resolveRedirects(url);
const Medias = await fetchMedia(finalUrl, false); // Download without watermark
if (Medias && Medias.length > 0) {
const mediaItems = await downloadMedia(Medias[0]);
return {
id: Medias[0].id,
url: url,
username: extractUsername(url),
media: mediaItems
};
} else {
return {
error: `[X] ${JSON.stringify(Medias)}\nMedia not found at URL: ${url}`
};
}
} catch (err) {
return { error: `Error in downloadFromTikTok: ${err.message}` };
}
};
/**
* Extracts the username from a TikTok URL.
* @param {string} url - The TikTok URL.
* @returns {string | null} - The extracted username, or null if not found.
*/
const extractUsername = (url) => {
const match = url.match(/tiktok\.com\/@([^/]+)/);
return match ? match[1] : null;
};
/**
* Resolves any redirects in the given URL.
* @param {string} url - The URL to resolve.
* @returns {Promise<string>} - The final URL after following redirects.
*/
const resolveRedirects = async (url) => {
try {
if (!url) {
throw new Error('URL is undefined or empty.');
}
let finalUrl = url;
// Check if the URL is a TikTok short link that redirects
if (finalUrl.includes('vm.tiktok.com') || finalUrl.includes('vt.tiktok.com')) {
const response = await axios.get(finalUrl, {
maxRedirects: 10,
headers: headers,
});
finalUrl = response.request.res.responseUrl;
// console.log("[*] Redirected to:", finalUrl);
}
return finalUrl;
} catch (err) {
return { error: `Error in resolveRedirects: ${err.message}` };
}
};
/**
* Fetches video or slideshow information from TikTok API.
* @param {string} url - The TikTok video URL.
* @param {boolean} watermark - Whether to include watermarks.
* @returns {Promise<{ url: string, images: string[], id: string }[] | { error: string }>} - Promise that resolves with an array of video or slideshow information objects, or an error message if not found.
*/
const fetchMedia = async (url, watermark) => {
try {
const ids = await fetchIds(url);
const results = [];
const maxRetries = 5; // Max retries for any error
for (const id of ids) {
const API_URL = `https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?aweme_id=${id}&iid=7318518857994389254&device_id=7318517321748022790&channel=googleplay&app_name=musical_ly&version_code=300904&device_platform=android&device_type=ASUS_Z01QD&version=9`;
let attempts = 0;
let success = false;
while (attempts < maxRetries && !success) {
try {
await delay(2000); // 2 second delay between requests
const response = await axios({
url: API_URL,
method: 'OPTIONS',
headers: headers,
});
const body = response.data;
if (body.aweme_list && body.aweme_list.length > 0 && body.aweme_list[0].aweme_id === id) {
const aweme = body.aweme_list[0];
let mediaItems = [];
if (aweme.image_post_info) {
aweme.image_post_info.images.forEach((image) => {
mediaItems.push({
type: 'image',
data: image.display_image.url_list[1],
});
});
} else {
const urlMedia = watermark
? aweme.video.download_addr.url_list[0]
: aweme.video.play_addr.url_list[0];
mediaItems.push({
type: 'video',
data: urlMedia,
});
}
const url = aweme?.video?.download_addr?.url_list[0] ? aweme?.video?.download_addr?.url_list[0] : aweme.video.play_addr.url_list[0];
const data = {
url: url,
id: id,
media: mediaItems,
};
results.push(data);
success = true; // Exit the retry loop if successful
} else {
throw new Error('No media found');
}
} catch (err) {
await delay(2000 * (attempts + 1)); // Exponential backoff
attempts++;
}
}
if (!success) {
return { error: `Failed to fetch media after ${maxRetries} attempts` };
}
}
return results.length > 0 ? results : { error: '[X] No media found' };
} catch (err) {
return { error: `Error in fetchMedia: ${err.message}` };
}
};
/**
* Downloads media buffers (images or video) from provided URLs.
* @param {{ url: string, id: string, media: { type: string, url: string, fileName: string, buffer: Buffer }[] }} item - The object containing URLs and ID of media.
* @returns {Promise<{ type: string, url: string, fileName: string, buffer: Buffer }[] | { error: string }>} - Promise that resolves with an array of objects containing downloaded media buffers and their corresponding URLs.
*/
const downloadMedia = async (item) => {
try {
let mediaItems = [];
let index = 1;
for (const mediaItem of item.media) {
let url = mediaItem.data;
const fileName = `${item.id}_${index++}.${mediaItem.type === 'video' ? 'mp4' : 'jpeg'}`;
if (mediaItem.type === 'video') {
const res = await axios.get(url, { responseType: 'arraybuffer' });
const buffer = Buffer.from(res.data);
mediaItems.push({
url: url,
fileName: fileName,
type: 'video',
buffer: buffer
});
} else if (mediaItem.type === 'image') {
const res = await axios.get(url, { responseType: 'arraybuffer' });
const buffer = Buffer.from(res.data);
mediaItems.push({
url: url,
fileName: fileName,
type: 'image',
buffer: buffer
});
}
}
return mediaItems;
} catch (err) {
return { error: `Error in downloadMedia: ${err.message}` };
}
};
/**
* Extracts TikTok video or photo IDs from the provided URL.
* @param {string} url - The TikTok URL.
* @returns {Promise<string[] | { error: string }>} - Promise that resolves with an array of extracted video or photo IDs.
*/
const fetchIds = async (url) => {
try {
let ids = [];
if (url.includes('/t/')) {
url = await new Promise((resolve) => {
require('follow-redirects').https.get(url, function (res) {
return resolve(res.responseUrl);
});
});
}
const matching = url.includes('/video/');
const matchingPhoto = url.includes('/photo/');
if (matching) {
let idVideo = url.substring(
url.indexOf('/video/') + 7,
url.indexOf('/video/') + 26
);
ids.push(idVideo.length > 19 ? idVideo.substring(0, idVideo.indexOf('?')) : idVideo);
} else if (matchingPhoto) {
let idPhoto = url.substring(
url.indexOf('/photo/') + 7,
url.indexOf('/photo/') + 26
);
ids.push(idPhoto.length > 19 ? idPhoto.substring(0, idPhoto.indexOf('?')) : idPhoto);
} else {
return { error: '[X] Error: URL not found' };
}
return ids;
} catch (err) {
return { error: `Error in fetchIds: ${err.message}` };
}
};
// Export the function
export default downloadFromTikTok;