Back to Home
Gistify Snippet
pengunduh-omdown-serbaguna.jsjavascript
404 lines| 1 | /** |
| 2 | * Pengunduh Media Serbaguna (Mesin Omdown) |
| 3 | * Penyelesai tautan unduhan all-in-one untuk berbagai platform yang didukung |
| 4 | * omdown.site, hasil rekayasa balik dari server action Next.js situs tersebut. |
| 5 | * |
| 6 | * Cara pakai sebagai modul: |
| 7 | * import { ambilMedia, unduhMedia } from './pengunduh-omdown-serbaguna.js'; |
| 8 | * const hasil = await ambilMedia('https://www.tiktok.com/@pengguna/video/123'); |
| 9 | * const berkas = await unduhMedia('https://www.instagram.com/reel/xxxx'); |
| 10 | * |
| 11 | * Cara pakai dari terminal: |
| 12 | * node pengunduh-omdown-serbaguna.js "<tautan>" [--stdout] |
| 13 | * |
| 14 | * Catatan: URL stream langsung memakai cookie sesi, kedaluwarsa sekitar |
| 15 | * 15 menit, dan terikat ke jaringan peminta. Beberapa platform (douyin, |
| 16 | * rednote, snackvideo, mangadex, pinedrama) gagal dari sisi server omdown, |
| 17 | * bukan dari mesin pengais ini. |
| 18 | * |
| 19 | * Dependensi: tidak ada (hanya modul bawaan Node.js). |
| 20 | **/ |
| 21 | |
| 22 | import fs from 'node:fs'; |
| 23 | import path from 'node:path'; |
| 24 | import { Readable } from 'node:stream'; |
| 25 | import { pipeline } from 'node:stream/promises'; |
| 26 | import { fileURLToPath } from 'node:url'; |
| 27 | |
| 28 | const URL_DASAR = 'https://www.omdown.site'; |
| 29 | const BAHASA = 'id'; |
| 30 | const AKSI_CADANGAN = '70516a7ceba440173e417c61be9db79a38ee7b121d'; |
| 31 | const AGEN_PENGGUNA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'; |
| 32 | const BATAS_WAKTU = 30000; |
| 33 | const BATAS_WAKTU_UNDUH = 300000; |
| 34 | const STATUS_HTTP = { |
| 35 | 200: 'OK', |
| 36 | 301: 'Moved Permanently', |
| 37 | 302: 'Found', |
| 38 | 400: 'Bad Request', |
| 39 | 403: 'Forbidden', |
| 40 | 404: 'Not Found', |
| 41 | 429: 'Too Many Requests', |
| 42 | 500: 'Internal Server Error', |
| 43 | 503: 'Service Unavailable', |
| 44 | 504: 'Gateway Timeout', |
| 45 | }; |
| 46 | |
| 47 | let idAksi = AKSI_CADANGAN; |
| 48 | |
| 49 | function buatError(pesan, status = 500) { |
| 50 | const galat = new Error(pesan); |
| 51 | galat.status = status; |
| 52 | return galat; |
| 53 | } |
| 54 | |
| 55 | function statusDariPesan(pesan = '') { |
| 56 | if (/batas permintaan|rate.?limit|too many|terlalu banyak|coba lagi dalam/i.test(pesan)) return 429; |
| 57 | if (/akses ditolak|access forbidden|forbidden|origin tidak valid/i.test(pesan)) return 403; |
| 58 | if (/tidak ditemukan|tidak ada|dihapus|not found|404/i.test(pesan)) return 404; |
| 59 | if (/tidak didukung|tidak valid|format tautan|wajib diisi|belum berupa|invalid|unsupported/i.test(pesan)) return 400; |
| 60 | if (/timeout|terlalu lama|timed out/i.test(pesan)) return 504; |
| 61 | return 500; |
| 62 | } |
| 63 | |
| 64 | async function kirimPermintaan(tautan, opsi = {}, batasWaktu = BATAS_WAKTU) { |
| 65 | return fetch(tautan, { |
| 66 | ...opsi, |
| 67 | signal: AbortSignal.timeout(batasWaktu), |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | function bacaCookie(respon) { |
| 72 | const daftar = typeof respon.headers.getSetCookie === 'function' |
| 73 | ? respon.headers.getSetCookie() |
| 74 | : (respon.headers.get('set-cookie') ? [respon.headers.get('set-cookie')] : []); |
| 75 | const toples = new Map(); |
| 76 | for (const butir of daftar) { |
| 77 | const pasangan = butir.split(';')[0]; |
| 78 | const indeks = pasangan.indexOf('='); |
| 79 | if (indeks > 0) toples.set(pasangan.slice(0, indeks), pasangan.slice(indeks + 1)); |
| 80 | } |
| 81 | return [...toples].map(([kunci, nilai]) => `${kunci}=${nilai}`).join('; '); |
| 82 | } |
| 83 | |
| 84 | async function temukanIdAksi() { |
| 85 | const halaman = await kirimPermintaan(`${URL_DASAR}/${BAHASA}`, { |
| 86 | headers: { |
| 87 | 'User-Agent': AGEN_PENGGUNA, |
| 88 | Accept: 'text/html,application/xhtml+xml', |
| 89 | }, |
| 90 | }); |
| 91 | if (!halaman.ok) throw buatError(`Gagal membuka halaman omdown (HTTP ${halaman.status}).`, halaman.status); |
| 92 | const html = await halaman.text(); |
| 93 | const berkasChunk = [...new Set([...html.matchAll(/\/_next\/static\/chunks\/[A-Za-z0-9_.-]+\.js/g)].map((cocok) => cocok[0]))]; |
| 94 | const hasilSemua = await Promise.all(berkasChunk.map(async (chunk) => { |
| 95 | try { |
| 96 | const responChunk = await kirimPermintaan(URL_DASAR + chunk, { |
| 97 | headers: { |
| 98 | 'User-Agent': AGEN_PENGGUNA, |
| 99 | }, |
| 100 | }); |
| 101 | return responChunk.ok ? await responChunk.text() : ''; |
| 102 | } catch { |
| 103 | return ''; |
| 104 | } |
| 105 | })); |
| 106 | for (const skrip of hasilSemua) { |
| 107 | const cocok = skrip.match(/createServerReference\)\("([0-9a-f]{42})"[^)]*resolveMediaAction/); |
| 108 | if (cocok) { |
| 109 | idAksi = cocok[1]; |
| 110 | return idAksi; |
| 111 | } |
| 112 | } |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | function uraikanFlight(isi) { |
| 117 | const baris = {}; |
| 118 | let posisi = 0; |
| 119 | while (posisi < isi.length) { |
| 120 | if (isi[posisi] === ':') { |
| 121 | const akhirBaris = isi.indexOf('\n', posisi); |
| 122 | posisi = akhirBaris === -1 ? isi.length : akhirBaris + 1; |
| 123 | continue; |
| 124 | } |
| 125 | const cocokBaris = /^([0-9a-f]+):/.exec(isi.slice(posisi)); |
| 126 | if (!cocokBaris) break; |
| 127 | const id = cocokBaris[1]; |
| 128 | const tanda = isi[posisi + cocokBaris[0].length]; |
| 129 | if (tanda === 'T') { |
| 130 | const koma = isi.indexOf(',', posisi + cocokBaris[0].length); |
| 131 | const panjang = parseInt(isi.slice(posisi + cocokBaris[0].length + 1, koma), 16); |
| 132 | baris[id] = isi.slice(koma + 1, koma + 1 + panjang); |
| 133 | posisi = koma + 1 + panjang; |
| 134 | } else { |
| 135 | const akhirBaris = isi.indexOf('\n', posisi); |
| 136 | const akhir = akhirBaris === -1 ? isi.length : akhirBaris; |
| 137 | baris[id] = isi.slice(posisi + cocokBaris[0].length, akhir); |
| 138 | posisi = akhir + 1; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | const idHasil = Object.keys(baris).find((id) => baris[id].startsWith('{"success"')); |
| 143 | if (!idHasil) return null; |
| 144 | const mentah = JSON.parse(baris[idHasil]); |
| 145 | |
| 146 | const selesaikan = (nilai) => { |
| 147 | if (typeof nilai === 'string') { |
| 148 | if (nilai === '$undefined' || nilai === '$null') return null; |
| 149 | if (/^\$[0-9a-f]+$/.test(nilai)) return baris[nilai.slice(1)] ?? nilai; |
| 150 | return nilai; |
| 151 | } |
| 152 | if (Array.isArray(nilai)) return nilai.map(selesaikan); |
| 153 | if (nilai && typeof nilai === 'object') { |
| 154 | for (const kunci of Object.keys(nilai)) nilai[kunci] = selesaikan(nilai[kunci]); |
| 155 | return nilai; |
| 156 | } |
| 157 | return nilai; |
| 158 | }; |
| 159 | return selesaikan(mentah); |
| 160 | } |
| 161 | |
| 162 | function urlMutlak(nilai) { |
| 163 | if (typeof nilai !== 'string' || !nilai) return nilai; |
| 164 | if (nilai.startsWith('/')) return URL_DASAR + nilai; |
| 165 | return nilai; |
| 166 | } |
| 167 | |
| 168 | function normalkanUrl(nilai) { |
| 169 | if (Array.isArray(nilai)) return nilai.map(normalkanUrl); |
| 170 | if (nilai && typeof nilai === 'object') { |
| 171 | for (const [kunci, anak] of Object.entries(nilai)) { |
| 172 | if (typeof anak === 'string' && (kunci === 'url' || kunci === 'authorAvatar' || /Url$/.test(kunci))) { |
| 173 | nilai[kunci] = urlMutlak(anak); |
| 174 | } else { |
| 175 | nilai[kunci] = normalkanUrl(anak); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | return nilai; |
| 180 | } |
| 181 | |
| 182 | function kembangkanArsip(data) { |
| 183 | const daftarFormat = Array.isArray(data.formats) ? data.formats : []; |
| 184 | data.formats = daftarFormat; |
| 185 | const daftarSlide = Array.isArray(data.slides) ? data.slides : []; |
| 186 | data.slides = daftarSlide; |
| 187 | |
| 188 | for (const format of daftarFormat) { |
| 189 | format.direct = Boolean(format.downloadUrl && /^https?:\/\//i.test(format.downloadUrl)); |
| 190 | } |
| 191 | for (const format of daftarFormat) { |
| 192 | if (format.id === 'all-slides' || format.downloadUrl === 'all-slides') { |
| 193 | format.archive = 'slides'; |
| 194 | format.downloadUrl = null; |
| 195 | format.files = daftarSlide |
| 196 | .map((slide, indeks) => ({ |
| 197 | url: slide.url || slide.previewUrl || slide.thumbnailUrl, |
| 198 | type: slide.type || 'image', |
| 199 | index: slide.index ?? indeks, |
| 200 | })) |
| 201 | .filter((berkas) => berkas.url); |
| 202 | const jumlahAudio = Number((format.description || '').match(/(\d+)\s*(?:lagu|audio|mp3|musik|music)/i)?.[1] || 0); |
| 203 | if (jumlahAudio > 0) { |
| 204 | const daftarAudio = daftarFormat |
| 205 | .filter((butir) => butir !== format && butir.kind === 'audio' && butir.direct) |
| 206 | .slice(0, jumlahAudio); |
| 207 | for (const trek of daftarAudio) { |
| 208 | format.files.push({ |
| 209 | url: trek.downloadUrl, |
| 210 | id: trek.id, |
| 211 | label: trek.label, |
| 212 | type: 'audio', |
| 213 | ext: trek.ext || 'mp3', |
| 214 | index: format.files.length, |
| 215 | }); |
| 216 | } |
| 217 | } |
| 218 | } else if (format.id === 'all-playlist' || format.downloadUrl === 'all-playlist') { |
| 219 | format.archive = 'playlist'; |
| 220 | format.downloadUrl = null; |
| 221 | format.files = daftarFormat |
| 222 | .filter((butir) => butir !== format && butir.kind === 'audio' && /^https?:\/\//i.test(butir.downloadUrl || '')) |
| 223 | .map((butir) => ({ |
| 224 | url: butir.downloadUrl, |
| 225 | id: butir.id, |
| 226 | label: butir.label, |
| 227 | })); |
| 228 | } |
| 229 | } |
| 230 | return data; |
| 231 | } |
| 232 | |
| 233 | export async function selesaikanMedia(masukan = '') { |
| 234 | let tautan = String(masukan).trim(); |
| 235 | const ditemukan = tautan.match(/https?:\/\/[^\s]+/i); |
| 236 | if (ditemukan) tautan = ditemukan[0]; |
| 237 | else if (tautan && !/^https?:\/\//i.test(tautan)) tautan = `https://${tautan}`; |
| 238 | |
| 239 | let terurai; |
| 240 | try { |
| 241 | terurai = new URL(tautan); |
| 242 | } catch { |
| 243 | throw buatError('URL tidak valid. Tempel tautan publik yang lengkap.', 400); |
| 244 | } |
| 245 | if (!/^https?:$/.test(terurai.protocol)) throw buatError('URL harus memakai http atau https.', 400); |
| 246 | |
| 247 | for (let percobaan = 0; percobaan < 2; percobaan += 1) { |
| 248 | const respon = await kirimPermintaan(`${URL_DASAR}/${BAHASA}`, { |
| 249 | method: 'POST', |
| 250 | headers: { |
| 251 | 'User-Agent': AGEN_PENGGUNA, |
| 252 | 'Content-Type': 'text/plain;charset=UTF-8', |
| 253 | Accept: 'text/x-component', |
| 254 | 'Accept-Language': 'id-ID,id;q=0.9,en;q=0.8', |
| 255 | 'Next-Action': idAksi, |
| 256 | Origin: URL_DASAR, |
| 257 | Referer: `${URL_DASAR}/${BAHASA}`, |
| 258 | }, |
| 259 | body: JSON.stringify([tautan, '', BAHASA]), |
| 260 | }); |
| 261 | |
| 262 | const isi = await respon.text(); |
| 263 | const aksiHilang = respon.headers.get('x-nextjs-action-not-found') === '1' || /Server action not found/i.test(isi); |
| 264 | if (aksiHilang && percobaan === 0) { |
| 265 | const ketemu = await temukanIdAksi(); |
| 266 | if (ketemu) continue; |
| 267 | throw buatError('Server action omdown tidak ditemukan. Website mungkin sedang diperbarui.', 500); |
| 268 | } |
| 269 | if (respon.status >= 400 && !isi.includes('"success"')) { |
| 270 | throw buatError(`Omdown membalas HTTP ${respon.status}.`, respon.status); |
| 271 | } |
| 272 | |
| 273 | const flight = uraikanFlight(isi); |
| 274 | if (!flight) throw buatError('Respons mesin omdown tidak dapat dibaca. Coba lagi.', 500); |
| 275 | if (!flight.success) throw buatError(flight.error || 'Gagal mengambil media.', statusDariPesan(flight.error)); |
| 276 | if (!flight.data || (!Array.isArray(flight.data.formats) && !Array.isArray(flight.data.slides))) { |
| 277 | throw buatError('Media tidak ditemukan atau tidak memiliki data unduhan.', 404); |
| 278 | } |
| 279 | |
| 280 | const data = kembangkanArsip(normalkanUrl(flight.data)); |
| 281 | if (data.formats.length === 0 && data.slides.length === 0) { |
| 282 | throw buatError('Media tidak memiliki format yang bisa diunduh.', 404); |
| 283 | } |
| 284 | return { |
| 285 | data, |
| 286 | cookie: bacaCookie(respon), |
| 287 | }; |
| 288 | } |
| 289 | throw buatError('Gagal memanggil mesin omdown.', 500); |
| 290 | } |
| 291 | |
| 292 | export async function ambilMedia(masukan = '') { |
| 293 | try { |
| 294 | const { data, cookie } = await selesaikanMedia(masukan); |
| 295 | const meta = { |
| 296 | status: 200, |
| 297 | response: 'OK', |
| 298 | platform: data.platform || null, |
| 299 | url: data.rawUrl || String(masukan).trim(), |
| 300 | }; |
| 301 | if (cookie) { |
| 302 | meta.session = { |
| 303 | cookie, |
| 304 | userAgent: AGEN_PENGGUNA, |
| 305 | }; |
| 306 | } |
| 307 | return [ |
| 308 | meta, |
| 309 | { |
| 310 | data: [data], |
| 311 | }, |
| 312 | ]; |
| 313 | } catch (galat) { |
| 314 | const status = galat.status || statusDariPesan(galat.message); |
| 315 | const teksStatus = STATUS_HTTP[status] || 'Unknown'; |
| 316 | return [ |
| 317 | { |
| 318 | status, |
| 319 | response: teksStatus, |
| 320 | error: `ERROR ${status} ${teksStatus}: ${galat.message}`, |
| 321 | }, |
| 322 | { |
| 323 | data: [], |
| 324 | }, |
| 325 | ]; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | export async function unduhMedia(masukan, idFormat = '', tujuan = process.cwd()) { |
| 330 | const { data, cookie } = await selesaikanMedia(masukan); |
| 331 | const daftarFormat = data.formats || []; |
| 332 | const terpilih = idFormat |
| 333 | ? daftarFormat.find((format) => format.id === idFormat) |
| 334 | : daftarFormat.find((format) => format.kind === 'video' && format.direct) |
| 335 | || daftarFormat.find((format) => format.kind === 'audio' && format.direct) |
| 336 | || daftarFormat.find((format) => format.direct); |
| 337 | |
| 338 | if (!terpilih) throw buatError(`Format "${idFormat || 'video'}" tidak tersedia.`, 404); |
| 339 | if (!terpilih.direct) throw buatError('Format ini adalah arsip. Gunakan daftar files di JSON.', 400); |
| 340 | |
| 341 | const respon = await kirimPermintaan(terpilih.downloadUrl, { |
| 342 | headers: { |
| 343 | 'User-Agent': AGEN_PENGGUNA, |
| 344 | Accept: '*/*', |
| 345 | Origin: URL_DASAR, |
| 346 | Referer: `${URL_DASAR}/${BAHASA}`, |
| 347 | Cookie: cookie, |
| 348 | }, |
| 349 | }, BATAS_WAKTU_UNDUH); |
| 350 | if (!respon.ok || !respon.body) throw buatError(`Gagal mengunduh media (HTTP ${respon.status}).`, respon.status); |
| 351 | |
| 352 | const tipe = respon.headers.get('content-type') || ''; |
| 353 | const ekstensi = (terpilih.ext || '').replace(/^\./, '') || (tipe.includes('video/mp4') ? 'mp4' : tipe.includes('audio/mpeg') ? 'mp3' : tipe.includes('image/jpeg') ? 'jpg' : tipe.includes('image/png') ? 'png' : 'bin'); |
| 354 | const judul = String(data.title || 'omdown').replace(/[\\/:*?"<>|]+/g, '_').replace(/\s+/g, ' ').trim().slice(0, 100) || 'omdown'; |
| 355 | const sasaran = path.resolve(tujuan, `${judul}.${ekstensi}`); |
| 356 | |
| 357 | fs.mkdirSync(path.dirname(sasaran), { recursive: true }); |
| 358 | await pipeline(Readable.fromWeb(respon.body), fs.createWriteStream(sasaran)); |
| 359 | const ukuran = fs.statSync(sasaran).size; |
| 360 | |
| 361 | return { |
| 362 | status: 200, |
| 363 | response: 'OK', |
| 364 | file: sasaran, |
| 365 | bytes: ukuran, |
| 366 | format: terpilih.id, |
| 367 | platform: data.platform, |
| 368 | title: data.title, |
| 369 | }; |
| 370 | } |
| 371 | |
| 372 | async function utama() { |
| 373 | const argumen = process.argv.slice(2); |
| 374 | const masukan = argumen.find((arg) => !arg.startsWith('--')); |
| 375 | if (!masukan) { |
| 376 | console.error('Usage: node pengunduh-omdown-serbaguna.js "<url>" [--stdout]'); |
| 377 | process.exit(1); |
| 378 | } |
| 379 | |
| 380 | const hasil = await ambilMedia(masukan); |
| 381 | const keluaran = path.resolve(process.cwd(), 'result.json'); |
| 382 | fs.writeFileSync(keluaran, `${JSON.stringify(hasil, null, 2)}\n`); |
| 383 | |
| 384 | if (argumen.includes('--stdout')) { |
| 385 | console.log(JSON.stringify(hasil, null, 2)); |
| 386 | } else if (hasil[0].status === 200) { |
| 387 | const media = hasil[1].data[0]; |
| 388 | console.log(`OK ${media.platform} | ${media.title}`); |
| 389 | console.log(`Formats: ${media.formats.length}`); |
| 390 | for (const format of media.formats) { |
| 391 | const tautan = format.archive ? `archive:${format.archive} (${format.files.length} files)` : format.downloadUrl || '-'; |
| 392 | console.log(`- ${format.id} | ${format.label} | ${tautan}`); |
| 393 | } |
| 394 | console.log(`Saved: ${keluaran}`); |
| 395 | console.log('Direct stream URLs need meta.session.cookie from result.json.'); |
| 396 | } else { |
| 397 | console.error(hasil[0].error); |
| 398 | console.error(`Saved: ${keluaran}`); |
| 399 | process.exitCode = 1; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | const jalanUtama = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url); |
| 404 | if (jalanUtama) await utama(); |
Discussion & Comments0
No comments yet. Be the first to leave feedback or start a code discussion.
Sign in with your GitHub account to write comments and join the discussion.