frostpv 1.0.18 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -4,11 +4,12 @@ const path = require("path");
4
4
  const os = require("os");
5
5
  const FormData = require("form-data");
6
6
  const crypto = require("crypto");
7
- const { igdl, ttdl, fbdown, mediafire, capcut, youtube, gdrive, pinterest, twitter } = require("btch-downloader");
7
+ const { igdl, ttdl, fbdown, mediafire, capcut, gdrive, pinterest, twitter } = require("btch-downloader");
8
8
  const { TwitterDL } = require("twitter-downloader");
9
+ const instagramCustom = require('./instagramcustom');
10
+ const tiktokCustom = require('./tiktokcustom');
9
11
  const btch = require("btch-downloader");
10
12
  const btchOld = require("btch-downloader-old");
11
- const ytdl = require("@distube/ytdl-core");
12
13
 
13
14
 
14
15
  const Tiktok = require("@tobyg74/tiktok-api-dl");
@@ -26,7 +27,7 @@ const TEMP_DIR = path.join(os.tmpdir(), "downloader-dl-bot");
26
27
  try { fs.mkdirSync(TEMP_DIR, { recursive: true }); } catch (_) { }
27
28
  const OUTPUT_RETENTION_MIN = Number(process.env.OUTPUT_RETENTION_MIN || 5);
28
29
 
29
- const GOFILE_API = "Vs4e2PL8n65ExPz6wgDlqSY8kcEBRrzN";
30
+ const GOFILE_API = "1eNR6qXgKMn6GGl4oRcfOlQnxrgSrNSL";
30
31
  const GOFILE_UPLOAD_URL = "https://upload.gofile.io/uploadfile";
31
32
  const SIZE_LIMIT_MB = 10;
32
33
  const SIZE_LIMIT_BYTES = SIZE_LIMIT_MB * 1024 * 1024;
@@ -44,10 +45,6 @@ const videoPlatforms = [
44
45
  "https://fb.watch",
45
46
  "https://www.fb.watch",
46
47
  "https://fb.com",
47
- "https://www.youtube.com",
48
- "https://youtube.com",
49
- "https://www.youtu.be",
50
- "https://youtu.be",
51
48
  "https://www.fb.com",
52
49
  "https://web.facebook.com",
53
50
  "https://www.mediafire.com",
@@ -89,7 +86,6 @@ const defaultConfig = {
89
86
  autocrop: false,
90
87
  limitSizeMB: null,
91
88
  rotation: null,
92
- YTBmaxduration: 1200,
93
89
  outputFormat: null,
94
90
  };
95
91
 
@@ -184,13 +180,19 @@ async function uploadToGoFileIfNeeded(filePath) {
184
180
  return { type: "local", value: filePath };
185
181
  }
186
182
 
183
+ const uploadUrl = "https://upload.gofile.io/uploadfile";
184
+
187
185
  const form = new FormData();
188
- form.append("file", fs.createReadStream(filePath), path.basename(filePath));
186
+ form.append("file", fs.createReadStream(filePath), {
187
+ filename: path.basename(filePath),
188
+ knownLength: fileSizeInBytes
189
+ });
189
190
 
190
- const response = await axios.post(GOFILE_UPLOAD_URL, form, {
191
+ const response = await axios.post(uploadUrl, form, {
191
192
  headers: {
192
193
  ...form.getHeaders(),
193
194
  "Authorization": `Bearer ${GOFILE_API}`,
195
+ "Content-Length": form.getLengthSync()
194
196
  },
195
197
  maxContentLength: Infinity,
196
198
  maxBodyLength: Infinity
@@ -199,6 +201,7 @@ async function uploadToGoFileIfNeeded(filePath) {
199
201
  if (response.data && response.data.status === "ok") {
200
202
  const downloadLink = response.data.data?.downloadPage;
201
203
  if (downloadLink) {
204
+ await safeUnlinkWithRetry(filePath);
202
205
  return { type: "gofile", value: downloadLink };
203
206
  } else {
204
207
  throw new Error("GoFile API response OK, but download link not found.");
@@ -277,7 +280,7 @@ function getPlatformType(url) {
277
280
  if (lowerUrl.includes("facebook.com") || lowerUrl.includes("fb.watch") || lowerUrl.includes("fb.com")) return "facebook";
278
281
  if (lowerUrl.includes("mediafire.com")) return "mediafire";
279
282
  if (lowerUrl.includes("x.com") || lowerUrl.includes("twitter.com")) return "twitter";
280
- if (lowerUrl.includes("youtube.com") || lowerUrl.includes("youtu.be") || lowerUrl.includes("www.youtube.com")) return "youtube";
283
+ if (lowerUrl.includes("youtube.com") || lowerUrl.includes("you112t12u.be") || lowerUrl.includes("m.y11outu314be.com")) return "youtu1354be";
281
284
 
282
285
  return "unknown";
283
286
  }
@@ -292,6 +295,13 @@ async function tryFallbackDownload(url, maxRetries = 3) {
292
295
  switch (platform) {
293
296
  case "instagram": {
294
297
  const methods = [
298
+ async () => {
299
+ try {
300
+ return await instagramCustom(url);
301
+ } catch (e) {
302
+ throw new Error(`instagramCustom failed: ${e.message}`);
303
+ }
304
+ },
295
305
  async () => {
296
306
  const data = await igdl(url);
297
307
  if (data && Array.isArray(data) && data[0] && data[0].url) {
@@ -353,6 +363,13 @@ async function tryFallbackDownload(url, maxRetries = 3) {
353
363
  case "tiktok": {
354
364
 
355
365
  const methods = [
366
+ async () => {
367
+ try {
368
+ return await tiktokCustom(url);
369
+ } catch (e) {
370
+ throw new Error(`tiktokCustom failed: ${e.message}`);
371
+ }
372
+ },
356
373
  async () => {
357
374
  const data = await ttdl(url);
358
375
  if (data && data.video && data.video[0]) {
@@ -755,80 +772,61 @@ const MediaDownloader = async (url, options = {}) => {
755
772
  throw new Error(`URL not supported: ${blacklisted.reason}`);
756
773
  }
757
774
 
758
- await deleteTempVideos(); // Clean up previous temp files
759
-
760
- if (url.includes('youtube') || url.includes('youtu.be')) {
761
- let cookies = options.YTBcookies || (options.YTBcookie ? [options.YTBcookie] : []);
762
-
763
- // Automatically load external cookies if none provided
764
- if (cookies.length === 0) {
765
- cookies = loadExternalCookies();
766
- }
767
-
768
- if (cookies.length === 0) {
769
- throw new Error("YouTube download requires a session. Please Provide a valid session in 'session_data.json'.");
770
- }
771
-
772
- // Determine if audio-only is requested
773
- const isAudioOnly = options.outputFormat === 'mp3' || options.downloadAudio === true || config.downloadAudio === true;
774
-
775
- const videofile = await downloadYoutubeVideo(url, config, cookies, config.YTBmaxduration, isAudioOnly);
776
- if (videofile) {
777
- const result = await uploadToGoFileIfNeeded(videofile);
778
- return result;
779
- } else {
780
- throw new Error("URL not supported. Please provide a video URL from a valid platform.");
781
- }
775
+ // Verificar se o link está na lista de plataformas suportadas
776
+ if (!isVideoLink(url)) {
777
+ throw new Error("This URL is not from a supported platform. Supported platforms: Instagram, X(Twitter), TikTok, Facebook");
782
778
  }
783
779
 
784
- else if (url.includes("http")) {
785
- const blacklisted = blacklistLink(url);
786
- if (blacklisted) {
787
- throw new Error(`URL not supported: ${blacklisted.reason}`);
788
- }
780
+ // Verificar se é YouTube e lançar erro customizado
781
+ const platform = getPlatformType(url);
782
+ if (platform === "youtube") {
783
+ throw new Error("This URL is not from a supported platform. Supported platforms: Instagram, X(Twitter), TikTok, Facebook");
784
+ }
789
785
 
790
- // Verificar se o link está na lista de plataformas suportadas
791
- if (!isVideoLink(url)) {
792
- throw new Error("This URL is not from a supported platform. Supported platforms: Instagram, X(Twitter), TikTok, Facebook");
793
- }
786
+ await cleanupTempFiles(); // Clean up previous temp files
794
787
 
795
- let downloadedFilePath = null;
788
+ let downloadedFilePath = null;
796
789
 
790
+ try {
791
+ // Try primary download method
797
792
  try {
798
- // Try primary download method
799
- try {
800
- downloadedFilePath = await downloadSmartVideo(url, config);
801
- } catch (error) {
802
- const platform = getPlatformType(url);
793
+ downloadedFilePath = await downloadSmartVideo(url, config);
794
+ } catch (error) {
795
+ const platform = getPlatformType(url);
803
796
 
804
- try {
805
- const fallbackUrl = await tryFallbackDownload(url);
797
+ // YouTube doesn't use fallback method - it has its own specific implementation
798
+ if (platform === "youtube") {
799
+ throw new Error(`YouTube download failed: ${error.message}`);
800
+ }
806
801
 
807
- // Validate fallback URL
808
- const isValid = await validateVideoUrl(fallbackUrl, platform);
809
- if (!isValid) {
810
- throw new Error("Fallback URL validation failed");
811
- }
802
+ try {
803
+ const fallbackUrl = await tryFallbackDownload(url);
812
804
 
813
- downloadedFilePath = await downloadDirectVideo(fallbackUrl, config);
814
- } catch (fallbackError) {
815
- throw new Error(`Failed to download video with both methods: ${fallbackError.message}`);
805
+ // Validate fallback URL
806
+ const isValid = await validateVideoUrl(fallbackUrl, platform);
807
+ if (!isValid) {
808
+ throw new Error("Fallback URL validation failed");
816
809
  }
810
+
811
+ downloadedFilePath = await downloadDirectVideo(fallbackUrl, config);
812
+ } catch (fallbackError) {
813
+ throw new Error(`Failed to download video with both methods: ${fallbackError.message}`);
817
814
  }
815
+ }
818
816
 
819
- if (downloadedFilePath) {
820
- const result = await uploadToGoFileIfNeeded(downloadedFilePath);
821
- return result;
822
- } else {
823
- throw new Error("Failed to obtain downloaded file path.");
817
+ if (downloadedFilePath) {
818
+ if (config.noGofile) {
819
+ return { type: "local", value: downloadedFilePath };
824
820
  }
825
- } catch (error) {
826
- // Clean up any remaining temp files on error
827
- await cleanupTempFiles();
828
- throw new Error(`Error in downloader videos: ${error.message}`);
821
+ const result = await uploadToGoFileIfNeeded(downloadedFilePath);
822
+ return result;
823
+ } else {
824
+ throw new Error("Failed to obtain downloaded file path.");
829
825
  }
830
- } else {
831
- throw new Error("Please specify a video URL from Instagram, facebook or TikTok...");
826
+ } catch (error) {
827
+ // Clean up any remaining temp files on error
828
+ await cleanupTempFiles();
829
+ throw new Error(`Error in downloader videos: ${error.message}`);
832
830
  }
833
831
  };
834
832
 
@@ -1197,188 +1195,9 @@ async function rotateVideo(fileName, rotation) {
1197
1195
 
1198
1196
  // Function to extract URL from string
1199
1197
  function extractUrlFromString(text) {
1200
- const urlRegex = /(https?:\/\/[^\s]+)/g;
1198
+ const urlRegex = /(https?:\/\/[^\s]+)/;
1201
1199
  const match = text.match(urlRegex);
1202
- return match ? match[0] : text;
1203
- }
1204
-
1205
- async function downloadYoutubeVideo(url, config, cookies, YTBmaxduration, isAudioOnly = false) {
1206
- let lastError = null;
1207
-
1208
- for (let i = 0; i < Math.min(cookies.length, 6); i++) {
1209
- const currentCookie = cookies[i];
1210
- try {
1211
- const parsedCookies = parseCookies(currentCookie);
1212
-
1213
- if (parsedCookies.length === 0) {
1214
- continue;
1215
- }
1216
-
1217
- const agent = ytdl.createAgent(parsedCookies);
1218
- const info = await ytdl.getInfo(url, { agent });
1219
-
1220
- const durationSeconds = parseInt(info.videoDetails.lengthSeconds, 10);
1221
- if (durationSeconds > YTBmaxduration) {
1222
- throw new Error(`❌ The video is longer than ${YTBmaxduration} seconds. Aborting.`);
1223
- }
1224
-
1225
- let bestFormat;
1226
- if (isAudioOnly) {
1227
- // Filter for audio-only formats
1228
- const audioFormats = info.formats.filter(f => !f.hasVideo && f.hasAudio);
1229
- bestFormat = audioFormats.sort((a, b) => (b.audioBitrate || 0) - (a.audioBitrate || 0))[0];
1230
-
1231
- if (!bestFormat) {
1232
- // Fallback: any format with audio
1233
- bestFormat = info.formats.filter(f => f.hasAudio).sort((a, b) => (b.audioBitrate || 0) - (a.audioBitrate || 0))[0];
1234
- }
1235
- if (!bestFormat) {
1236
- throw new Error('❌ No suitable audio format found.');
1237
- }
1238
- } else {
1239
- // Filter for formats that have both audio and video (muxed)
1240
- // We prioritize higher quality but limit to 1080p if possible
1241
- let formats = info.formats.filter(format => {
1242
- return format.hasAudio && format.hasVideo && (!format.height || format.height <= 1080);
1243
- });
1244
-
1245
- if (formats.length === 0) {
1246
- // If no 1080p muxed formats, take any muxed format
1247
- formats = info.formats.filter(format => format.hasAudio && format.hasVideo);
1248
- }
1249
-
1250
- if (formats.length === 0) {
1251
- throw new Error('❌ No suitable video format found with both audio and video.');
1252
- }
1253
-
1254
- // Sort by resolution (height) descending to get best quality
1255
- bestFormat = formats.sort((a, b) => (b.height || 0) - (a.height || 0))[0];
1256
- }
1257
-
1258
- const ext = isAudioOnly ? 'mp3' : 'mp4';
1259
- // Create minimal unique file name in output dir
1260
- let fileName = path.join(OUTPUT_DIR, `v_${uuidv4().slice(0, 4)}.${ext}`);
1261
-
1262
- const videoStream = ytdl(url, {
1263
- format: bestFormat,
1264
- agent
1265
- });
1266
-
1267
- const videoWriter = fs.createWriteStream(fileName);
1268
- videoStream.pipe(videoWriter);
1269
-
1270
- return await new Promise((resolve, reject) => {
1271
- videoWriter.on('finish', async () => {
1272
- try {
1273
- let finalFilePath = fileName;
1274
- if (!isAudioOnly) {
1275
- finalFilePath = await processDownloadedFile(fileName, config, "youtube");
1276
- }
1277
- resolve(finalFilePath);
1278
- } catch (error) {
1279
- reject(error);
1280
- }
1281
- });
1282
- videoWriter.on('error', (error) => reject(error));
1283
- });
1284
-
1285
- } catch (error) {
1286
- console.error(`Attempt with cookie ${i + 1} failed: ${error.message}`);
1287
- lastError = error;
1288
- // If the error is about duration, don't retry with other cookies
1289
- if (error.message.includes('longer than')) {
1290
- throw error;
1291
- }
1292
- continue; // Try next cookie
1293
- }
1294
- }
1295
-
1296
- throw new Error(`An error occurred while downloading the YouTube video (all ${Math.min(cookies.length, 10)} cookies failed): ${lastError?.message}`);
1297
- }
1298
-
1299
- /**
1300
- * Automatically loads cookies from cookies.json if present
1301
- * @returns {Array} Array of cookie sets (one or more accounts)
1302
- */
1303
- function loadExternalCookies() {
1304
- const cookiesPath = path.join(process.cwd(), 'session_data.json');
1305
- if (!fs.existsSync(cookiesPath)) return [];
1306
-
1307
- try {
1308
- const fileContent = fs.readFileSync(cookiesPath, 'utf8');
1309
- const parsed = JSON.parse(fileContent);
1310
- if (!Array.isArray(parsed)) return [];
1311
-
1312
- // If it's an array of objects, it's ONE account.
1313
- // If it's an array of arrays, it's MULTIPLE accounts.
1314
- if (parsed.length > 0) {
1315
- if (Array.isArray(parsed[0])) {
1316
- // Multiple accounts: return them as strings for the rotation logic
1317
- return parsed.map(acc => JSON.stringify(acc));
1318
- } else {
1319
- // Single account: return the whole JSON as a string
1320
- return [fileContent];
1321
- }
1322
- }
1323
- } catch (e) {
1324
- console.error("Error loading external cookies:", e.message);
1325
- }
1326
- return [];
1327
- }
1328
-
1329
- /**
1330
- * Parses cookies in various formats (JSON, Netscape, or Header String)
1331
- * @param {string} cookieInput
1332
- * @returns {Array} Array of cookie objects
1333
- */
1334
- function parseCookies(cookieInput) {
1335
- if (!cookieInput) return [];
1336
-
1337
- const trimmedInput = cookieInput.trim();
1338
-
1339
- // 1. Try JSON (EditThisCookie export)
1340
- try {
1341
- const parsed = JSON.parse(trimmedInput);
1342
- if (Array.isArray(parsed)) return parsed;
1343
- if (typeof parsed === 'object') return [parsed];
1344
- } catch (e) { }
1345
-
1346
- // 2. Try Netscape format (tab-separated)
1347
- if (trimmedInput.includes('\t')) {
1348
- const lines = trimmedInput.split(/\r?\n/);
1349
- const cookies = [];
1350
- for (const line of lines) {
1351
- if (!line.trim() || line.startsWith('#')) continue;
1352
- const parts = line.split('\t');
1353
- if (parts.length >= 7) {
1354
- cookies.push({
1355
- domain: parts[0],
1356
- path: parts[2],
1357
- secure: parts[3].toUpperCase() === 'TRUE',
1358
- expirationDate: parseInt(parts[4]),
1359
- name: parts[5],
1360
- value: parts[6]
1361
- });
1362
- }
1363
- }
1364
- if (cookies.length > 0) return cookies;
1365
- }
1366
-
1367
- // 3. Fallback: Cookie header string (name=value; name2=value2)
1368
- return trimmedInput.split(';').map(v => v.split('=')).reduce((acc, v) => {
1369
- if (v.length >= 2) {
1370
- const name = v[0].trim();
1371
- const value = v.slice(1).join('=').trim();
1372
- if (name && value) {
1373
- acc.push({
1374
- name: name,
1375
- value: value,
1376
- domain: '.youtube.com'
1377
- });
1378
- }
1379
- }
1380
- return acc;
1381
- }, []);
1200
+ return match ? match[0] : null;
1382
1201
  }
1383
1202
 
1384
1203
  // Function to delete temporary videos
@@ -1386,7 +1205,7 @@ async function deleteTempVideos() {
1386
1205
  try {
1387
1206
  if (!fs.existsSync(TEMP_DIR)) return;
1388
1207
  const files = fs.readdirSync(TEMP_DIR);
1389
- const tempVideoFiles = files.filter(file => /^temp_video.*\.mp4$/.test(file) || /-player-script\.js$/.test(file));
1208
+ const tempVideoFiles = files.filter(file => /^temp_video.*\.mp4$/.test(file));
1390
1209
  for (const file of tempVideoFiles) {
1391
1210
  safeUnlink(path.join(TEMP_DIR, file));
1392
1211
  }
@@ -1407,8 +1226,7 @@ async function cleanupTempFiles() {
1407
1226
  /_audio\.mp3$/.test(file) ||
1408
1227
  /_rotated\.mp4$/.test(file) ||
1409
1228
  /_cropped\.mp4$/.test(file) ||
1410
- /_compressed\.mp4$/.test(file) ||
1411
- /-player-script\.js$/.test(file)
1229
+ /_compressed\.mp4$/.test(file)
1412
1230
  );
1413
1231
  for (const file of tempFiles) {
1414
1232
  await safeUnlinkWithRetry(path.join(TEMP_DIR, file));
@@ -1648,17 +1466,17 @@ const AudioDownloader = async (url, options = {}) => {
1648
1466
  let audioFilePath = null;
1649
1467
 
1650
1468
  try {
1651
- if (url.includes('youtube') || url.includes('youtu.be')) {
1652
- let cookies = options.YTBcookies || (options.YTBcookie ? [options.YTBcookie] : []);
1653
- if (cookies.length === 0) {
1654
- cookies = loadExternalCookies();
1655
- }
1469
+ let platform = getPlatformType(url);
1470
+ if (platform === "y124outube") {
1656
1471
 
1657
- if (cookies.length === 0) {
1658
- throw new Error("YouTube download requires a cookie. Please Provide a valid cookie.");
1472
+ let fileName = "temp_audio.mp3";
1473
+ let count = 1;
1474
+ while (fs.existsSync(fileName)) {
1475
+ fileName = `temp_audio_${count}.mp3`;
1476
+ count++;
1659
1477
  }
1660
-
1661
- audioFilePath = await downloadYoutubeVideo(url, config, cookies, config.YTBmaxduration, true);
1478
+ await downloadYoutubeAudio(url, fileName);
1479
+ audioFilePath = fileName;
1662
1480
  const result = await uploadToGoFileIfNeeded(audioFilePath);
1663
1481
  return result;
1664
1482
  }
@@ -0,0 +1,55 @@
1
+ const axios = require('axios');
2
+ const cheerio = require('cheerio');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ // This module exposes a function that accepts an Instagram URL and
7
+ // returns a direct video download URL (string) when possible.
8
+ module.exports = async function instagramCustom(instaUrl) {
9
+ try {
10
+ if (!instaUrl || typeof instaUrl !== 'string') throw new Error('Invalid URL');
11
+
12
+ const response = await axios.post(
13
+ 'https://instadown.org/wp-json/visolix/api/download',
14
+ {
15
+ url: instaUrl,
16
+ format: "",
17
+ captcha_response: null
18
+ },
19
+ {
20
+ headers: {
21
+ 'accept': '*/*',
22
+ 'content-type': 'application/json',
23
+ 'x-visolix-nonce': 'fed490c4ea',
24
+ 'Referer': 'https://instadown.org/fr/'
25
+ },
26
+ timeout: 15000
27
+ }
28
+ );
29
+
30
+ if (!response || !response.data) throw new Error('No response from instadown');
31
+ if (!response.data.status) throw new Error('instadown returned an error');
32
+
33
+ const $ = cheerio.load(response.data.data || '');
34
+ // The button/link typically has class 'visolix-item-download'
35
+ let downloadUrl = $('.visolix-item-download').attr('href');
36
+
37
+ // fallback: look for any <a href="...mp4"> in the HTML
38
+ if (!downloadUrl) {
39
+ const anchors = $('a');
40
+ anchors.each((i, el) => {
41
+ const href = $(el).attr('href');
42
+ if (href && href.match(/\.(mp4|m3u8)(\?|$)/i)) {
43
+ downloadUrl = href;
44
+ return false;
45
+ }
46
+ });
47
+ }
48
+
49
+ if (!downloadUrl) throw new Error('Download link not found');
50
+
51
+ return downloadUrl;
52
+ } catch (err) {
53
+ throw new Error(`instagramCustom failed: ${err.message}`);
54
+ }
55
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frostpv",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "downloads",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,7 +12,6 @@
12
12
  "author": "Delta",
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
- "@distube/ytdl-core": "^4.16.12",
16
15
  "@tobyg74/tiktok-api-dl": "^1.3.7",
17
16
  "ab-downloader": "^1.0.1",
18
17
  "axios": "1.12.0",
@@ -24,6 +23,7 @@
24
23
  "fs": "^0.0.1-security",
25
24
  "path": "^0.12.7",
26
25
  "twitter-downloader": "^1.1.8",
27
- "uuid": "^11.1.0"
26
+ "uuid": "^11.1.0",
27
+ "cheerio": "^1.1.2"
28
28
  }
29
29
  }
@@ -0,0 +1,114 @@
1
+ const axios = require('axios');
2
+
3
+ function extractTikTokVideoId(input) {
4
+ if (!input || typeof input !== 'string') return null;
5
+
6
+ const trimmed = input.trim();
7
+
8
+ if (/^\d{5,}$/.test(trimmed)) return trimmed;
9
+
10
+ const match1 = trimmed.match(/\/video\/(\d{5,})/);
11
+ if (match1 && match1[1]) return match1[1];
12
+
13
+ const match2 = trimmed.match(/[?&]item_id=(\d{5,})/);
14
+ if (match2 && match2[1]) return match2[1];
15
+
16
+ const match3 = trimmed.match(/(\d{10,})/);
17
+ if (match3 && match3[1]) return match3[1];
18
+
19
+ return null;
20
+ }
21
+
22
+ function findFirstMediaUrl(value) {
23
+ if (!value) return null;
24
+ if (typeof value === 'string') {
25
+ if (/^https?:\/\//i.test(value)) return value;
26
+ return null;
27
+ }
28
+ if (Array.isArray(value)) {
29
+ for (const v of value) {
30
+ const found = findFirstMediaUrl(v);
31
+ if (found) return found;
32
+ }
33
+ return null;
34
+ }
35
+ if (typeof value === 'object') {
36
+ const candidates = [
37
+ value.url,
38
+ value.play,
39
+ value.playUrl,
40
+ value.play_url,
41
+ value.download,
42
+ value.downloadUrl,
43
+ value.download_url,
44
+ value.video,
45
+ value.videoUrl,
46
+ value.video_url,
47
+ value.wmplay,
48
+ value.wmPlay,
49
+ value.hdplay,
50
+ value.hdPlay,
51
+ value.hd
52
+ ];
53
+
54
+ for (const c of candidates) {
55
+ const found = findFirstMediaUrl(c);
56
+ if (found) return found;
57
+ }
58
+
59
+ for (const key of Object.keys(value)) {
60
+ const found = findFirstMediaUrl(value[key]);
61
+ if (found) return found;
62
+ }
63
+ }
64
+ return null;
65
+ }
66
+
67
+ module.exports = async function tiktokCustom(tiktokUrlOrId) {
68
+ try {
69
+ const id = extractTikTokVideoId(tiktokUrlOrId);
70
+ if (!id) throw new Error('Invalid TikTok URL/ID');
71
+
72
+ const apiUrl = `https://api.twitterpicker.com/tiktok/mediav2?id=${encodeURIComponent(id)}`;
73
+
74
+ const headers = {
75
+ 'accept': '*/*',
76
+ 'accept-language': 'fr-FR,fr;q=0.6',
77
+ 'origin': 'https://tiktokdownloader.com',
78
+ 'priority': 'u=1, i',
79
+ 'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Brave";v="144"',
80
+ 'sec-ch-ua-mobile': '?0',
81
+ 'sec-ch-ua-platform': '"Windows"',
82
+ 'sec-fetch-dest': 'empty',
83
+ 'sec-fetch-mode': 'cors',
84
+ 'sec-fetch-site': 'cross-site',
85
+ 'sec-gpc': '1',
86
+ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36'
87
+ };
88
+
89
+ const response = await axios.get(apiUrl, { headers, timeout: 15000 });
90
+ const data = response && response.data ? response.data : null;
91
+ if (!data) throw new Error('No response from twitterpicker');
92
+
93
+
94
+
95
+ // Prefer no-watermark video URL when available
96
+ if (data.video_no_watermark && typeof data.video_no_watermark === 'object') {
97
+ const directNoWm = data.video_no_watermark.url;
98
+ if (typeof directNoWm === 'string' && /^https?:\/\//i.test(directNoWm)) return directNoWm;
99
+
100
+ if (Array.isArray(data.video_no_watermark_alternatives)) {
101
+ for (const alt of data.video_no_watermark_alternatives) {
102
+ if (alt && typeof alt.url === 'string' && /^https?:\/\//i.test(alt.url)) return alt.url;
103
+ }
104
+ }
105
+ }
106
+
107
+ const mediaUrl = findFirstMediaUrl(data);
108
+ if (!mediaUrl) throw new Error('Media URL not found');
109
+
110
+ return mediaUrl;
111
+ } catch (err) {
112
+ throw new Error(`tiktokCustom failed: ${err.message}`);
113
+ }
114
+ };