Back to Home
Gistify Snippet
kyiov
Aug 30·1 file(s)
Public

MediaFire Direct Download Link & Metadata Scraper

MediaFire file downloader & resolver with cookiejar session support, scrambled link decoding, file metadata extraction (filename, filesize, extension, mimeType), and direct CDN download link generator.

#mediafire#downloader#scraper#javascript#tools#cdn
13
Raw
mediafire.jsjavascript
88 lines
1import axios from "axios";
2import { wrapper } from "axios-cookiejar-support";
3import * as cheerio from "cheerio";
4import { CookieJar } from "tough-cookie";
5
6const jar = new CookieJar();
7const client = wrapper(axios.create({ jar }));
8
9const HEADERS = {
10 "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
11 "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
12 "accept-language": "en-US,en;q=0.9,id;q=0.8",
13};
14
15function normalizeUrl(input) {
16 if (!/mediafire\.com/.test(input)) throw new Error("URL MediaFire tidak valid");
17 return input.replace("://mediafire.com", "://www.mediafire.com").replace(/^http:/, "https:");
18}
19
20/**
21 * MediaFire Direct Download Link & Metadata Scraper
22 * Extracts filename, size, extension, mimeType, and direct CDN download URL.
23 */
24async function mediafire(url) {
25 try {
26 const { data: html } = await client.get(normalizeUrl(url), { headers: HEADERS });
27 const $ = cheerio.load(html);
28
29 const $btn = $("#downloadButton");
30 if (!$btn.length) throw new Error("Tombol download tidak ditemukan (file dihapus / private?)");
31
32 let downloadUrl = $btn.attr("href");
33 const scrambled = $btn.attr("data-scrambled-url");
34 if ((!downloadUrl || downloadUrl === "#") && scrambled) {
35 downloadUrl = Buffer.from(scrambled, "base64").toString("utf8");
36 }
37 if (!downloadUrl || !/^https?:/.test(downloadUrl)) {
38 throw new Error("Link download tidak bisa di-resolve");
39 }
40
41 // metadata
42 const filename =
43 $(".dl-btn-label").attr("title") ||
44 $(".filename").first().text().trim() ||
45 (downloadUrl.split("/").pop() || "").split("?")[0];
46
47 const details = {};
48 $(".details li").each((_, li) => {
49 const text = $(li).text();
50 const idx = text.indexOf(":");
51 if (idx === -1) return;
52 details[text.slice(0, idx).trim().toLowerCase()] = text.slice(idx + 1).trim();
53 });
54
55 let mimeType = null;
56 let sizeBytes = null;
57 try {
58 const probe = await client.get(downloadUrl, {
59 headers: { ...HEADERS, range: "bytes=0-0" },
60 responseType: "stream",
61 validateStatus: () => true,
62 });
63 mimeType = probe.headers["content-type"] || null;
64 const cr = probe.headers["content-range"];
65 sizeBytes = cr ? Number(cr.split("/")[1]) : Number(probe.headers["content-length"]) || null;
66 probe.data.destroy();
67 } catch {}
68
69 const cookies = await jar.getCookieString("https://www.mediafire.com");
70
71 return {
72 filename,
73 size: details["file size"] || null,
74 sizeBytes,
75 uploaded: details["uploaded"] || null,
76 extension: (filename.match(/\.(\w+)$/) || [])[1] || null,
77 mimeType,
78 downloadUrl,
79 cookies: cookies || null,
80 };
81 } catch (error) {
82 throw new Error(`Mediafire Scraper Error: ${error.message}`);
83 }
84}
85
86export { mediafire };
87export default mediafire;
88
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.