Back

MediaFire Downloader

86 views
rapp Code by: @rapp

Description

Base url: https://mediafire.com Ini scrape buat download MediaFire, jadi ya gitu

Features

Code (javascript)

MediaFire.js
const axios = require('axios');
const cheerio = require('cheerio');

class MediaFireScraper {
    constructor() {
        this.client = axios.create({
            headers: {
                'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36'
            },
            timeout: 30000
        });
    }

    async getFileInfo(mediafireUrl) {
        try {
            const response = await this.client.get(mediafireUrl);
            const $ = cheerio.load(response.data);

            let fileName = $('.filename').text().trim();
            if (!fileName) {
                const title = $('title').text().trim();
                fileName = title.split(' - ')[0] || 'file';
            }

            fileName = fileName.replace(/[<>:"/\\|?*]/g, '_').trim();

            let fileSize = $('.file-size').text().trim() || 
                         $('.file-size > span').text().trim() ||
                         $('.details > div').first().text().trim();

            if (!fileSize || fileSize === 'Unknown') {
                const sizeText = $('body').text();
                const sizeMatch = sizeText.match(/(\d+\.?\d*)\s*(MB|KB|GB)/i);
                fileSize = sizeMatch ? sizeMatch[0] : '0 MB';
            }

            const downloadLink = await this.extractDownloadLink($);

            return {
                fileName: fileName,
                fileSize: fileSize,
                urlDownload: downloadLink
            };

        } catch (error) {
            throw new Error(`Failed to get file info: ${error.message}`);
        }
    }

    async extractDownloadLink($) {
        let downloadLink = $('#downloadButton').attr('href');

        if (!downloadLink) {
            const onclick = $('#downloadButton').attr('onclick');
            if (onclick) {
                const urlMatch = onclick.match(/(https?:\/\/[^'"]+)/);
                if (urlMatch) downloadLink = urlMatch[0];
            }
        }

        if (!downloadLink) {
            const scriptContent = $('script').toString();
            const patterns = [
                /"download_link":"([^"]+)"/,
                /"direct_link":"([^"]+)"/,
                /(https:\/\/download[0-9]*\.mediafire\.com\/[^"']+)/,
                /window\.location\.href\s*=\s*'([^']+)'/
            ];

            for (const pattern of patterns) {
                const match = scriptContent.match(pattern);
                if (match && match[1]) {
                    downloadLink = match[1];
                    break;
                }
            }
        }

        if (downloadLink) {
            downloadLink = downloadLink.replace(/window\.location\.href\s*=\s*'|'|"/g, '');
            
            if (downloadLink.includes('//www.mediafire.com//www.mediafire.com')) {
                downloadLink = downloadLink.replace('//www.mediafire.com//www.mediafire.com', '//www.mediafire.com');
            }
            
            if (downloadLink.startsWith('//')) {
                downloadLink = 'https:' + downloadLink;
            } else if (downloadLink.startsWith('/')) {
                downloadLink = 'https://www.mediafire.com' + downloadLink;
            }

            return downloadLink;
        }

        return null;
    }
}

async function main() {
    const mediafireUrl = process.argv[2];
    
    if (!mediafireUrl) {
        process.exit(1);
    }

    const scraper = new MediaFireScraper();
    
    try {
        const result = await scraper.getFileInfo(mediafireUrl);
        console.log(JSON.stringify(result, null, 2));
    } catch (error) {
        const errorResult = {
            fileName: "Unknown",
            fileSize: "0 MB", 
            urlDownload: null
        };
        console.log(JSON.stringify(errorResult, null, 2));
        process.exit(1);
    }
}

if (require.main === module) {
    main();
}

module.exports = MediaFireScraper;