Search code examples
node.jszombie.js

How to stream/pipe response object from Zombie.js


How can I download a file with Zombie.js with a web app that uses http-equiv meta tag to emulate the Refresh HTTP header?

In essence how do you download/stream/pipe a downloadable resource that is loaded after sometime have passed and not via a direct download link.

I was thinking I could do it with the Zombie.js Pipeline... but the docs on that are pretty senseless to a zombiejs newbie


Solution

  • Can be used to download BINARY FILES

    var fs = require('fs');
    var URL = require('url');
    var Request = require('request'); // Named 'Request' to prevent conflict with zombie's 'request' object
    var Browser = require('zombie'), browser = new Browser();
    
    browser.on('request', function (request) {
    
      // let's pullover for a download incase we find our request signboard :)
      if (request.url.indexOf('https://matchingString') == 0) {
    
        console.log('Beginning download in a flash...');
    
        var 
        parsedURL = URL.parse(request.url), 
        cookies = browser.cookies, 
        cookieHeader = cookies.serialize(parsedURL.hostname, parsedURL.pathname);
    
        if (cookieHeader) request.headers.append('Cookie', cookieHeader);
    
        var writeStream = fs.createWriteStream(pathToFileYetToBeWritten);
    
        writeStream
        .on('error', function(err) {
            console.error(err);
        })
        .on('finish', function() {
            console.log('finished writing file');
        });
    
        Request({
            method: request.method, 
            uri: request.url, 
            headers: request.headers.toObject(), 
            proxy: browser.proxy, 
            body: request.body, 
            jar: false, 
            followRedirect: false, 
            strictSSL: browser.strictSSL, 
            localAddress: browser.localAddress || 0
        })
        .on('error', function(err) {
            console.error(err);
        })
        .pipe(writeStream);
    
    }
    
    });
    
    
    
    browser.visit(url, function () {
    
    browser.wait(6000); // Wait only if you need to
    
    });