/** * Scraper TikTok Downloader * Creator : @wilz * Base : https://wilz.web.id/ * * Fitur: * - Sanitasi input URL TikTok. * - Ekstraksi aman (optional chaining) untuk properti null. * - Normalisasi data dari berbagai kemungkinan format respons API V3. * - Struktur data hasil return yang bersih dan terstandarisasi. * * Instalasi: * npm i axios * * CLI: * node scraper/tiktokdl.js "https://vt.tiktok.com/ZSxxxx/" */ const axios = require('axios'); // ─── Konfigurasi ──────────────────────────────────────────────── const API_URL = 'https://api.wilz.web.id/api/download/tiktokv3'; const TIMEOUT = 30000; const client = axios.create({ timeout: TIMEOUT, headers: { 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Mobile Safari/537.36', Accept: 'application/json, text/plain, */*', 'Accept-Language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', }, }); // ─── Helper: Sanitasi URL ─────────────────────────────────────── function sanitizeUrl(input) { return String(input || '').trim(); } function isTiktokUrl(url) { return /(tiktok\.com|vt\.tiktok\.com|vm\.tiktok\.com)/i.test(url); } // ─── Helper: Normalisasi Respons ──────────────────────────────── function normalize(json, sourceUrl) { // API mungkin membungkus result di dalam "data" atau "result" const root = json.data || json.result || json; // Cek status error const status = json.status || root.status; if (status === 'error' || json.success === false) { throw new Error(`API Error: ${json.message || root.message || 'Gagal mengambil data'}`); } const result = root.result || root; const author = result.author || {}; const music = result.music || {}; return { success: true, source: sourceUrl, type: result.type || 'video', title: result.desc || result.title || '', author: { nickname: author.nickname || null, avatar: author.avatar || null, }, music: { title: typeof music === 'object' ? music.title : music, url: typeof music === 'object' ? music.playUrl || music.url : null, }, video: { hd: result.videoHD || result.video_hd || result.hd || null, watermark: result.videoWatermark || result.video_watermark || result.watermark || null, noWatermark: result.videoNowm || result.video_no_watermark || result.nowm || null, }, images: Array.isArray(result.images) ? result.images : [], raw: result, // Menyimpan data mentah untuk referensi fetchedAt: new Date().toISOString(), }; } // ─── Fungsi Utama ─────────────────────────────────────────────── async function downloadTiktok(rawUrl) { const targetUrl = sanitizeUrl(rawUrl); if (!targetUrl) { throw new Error('URL TikTok wajib diisi.'); } if (!isTiktokUrl(targetUrl)) { throw new Error('URL yang diberikan bukan dari TikTok.'); } console.log(`[Scraper] Mengambil data untuk: ${targetUrl}`); let res; try { res = await client.get(API_URL, { params: { url: targetUrl } }); } catch (err) { if (err.response) { throw new Error(`API Error (HTTP ${err.response.status}): ${JSON.stringify(err.response.data)}`); } throw new Error(`Koneksi gagal: ${err.message}`); } const json = res.data; if (!json || typeof json !== 'object') { throw new Error('Respons API tidak valid (bukan JSON).'); } return normalize(json, targetUrl); } // ─── CLI Handler ──────────────────────────────────────────────── if (require.main === module) { const url = process.argv[2]; if (!url) { console.error( '❌ Kesalahan: URL tidak ditemukan!\n\n' + 'Cara Penggunaan (CLI):\n' + ' node scraper/tiktok-v3.js ""\n\n' + 'Contoh:\n' + ' node scraper/tiktok-v3.js "https://vt.tiktok.com/ZSxxxx/"' ); process.exit(1); } (async () => { try { const result = await downloadTiktok(url); console.log('\n✅ Hasil Scrape Berhasil & Divalidasi:'); console.log(JSON.stringify(result, null, 2)); } catch (err) { console.error('❌ Error:', err.message); process.exit(1); } })(); } module.exports = { downloadTiktok };