Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a wav file using Horseman and PhantomJS losing data quality

I'm using PhantomJS and HorsemanJS to download a wav file from a remote server. However when the file is base64 encoded and written to a new file it loses quality which makes it unusable. The audio is there but its distorted which leads me to think that its an encoding problem. I'm running on Ubuntu 14.04 using node v5

Below is my script any ideas maybe to improve the base64 encoding?

var Horseman = require('node-horseman');
var horseman = new Horseman({cookiesFile:'./cookiejar'});
var fs = require('fs');

horseman.on("urlChanged", function(url){
  console.log(url);
});

horseman.on('consoleMessage', function( msg ){
  console.log(msg);
});

horseman
  .userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
  .open('https://remoteserver.com/audo.aspx?guid=01439900-5361-4828-ad0e-945b56e9fe51')
  .waitForNextPage()
  .type('input[name="password"]', process.env.PASS)
  .type('input[name="username"]', process.env.UN)
  .click("button:contains('Login')")
  .waitForNextPage()
  .evaluate(function(){
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    function base64encode(str) {
        var out, i, len;
        var c1, c2, c3;

        len = str.length;
        i = 0;
        out = "";
        while(i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
        }
        return out;
    }

    var url = $("a:contains('Uncompressed file')").attr("href");

    console.log(url);

    var out;
    $.ajax({
      'async' : false,
      'url' : url,
      'success' : function(data, status, xhr) {
        console.log(status);
        console.log(xhr.getResponseHeader('Content-Type'));
        out = base64encode(data);
      }
    });
    return out;
  })
  .then(function(out){
    fs.writeFile('./mydownloadedfile.txt', out, 'base64', function(){
      return horseman.close();
    });
  });

The content-type comes back as audio/wav

If the file is manually downloaded and played via the same player it plays fine, it is only when it goes through this process.

like image 791
thegogz Avatar asked Mar 23 '16 10:03

thegogz


1 Answers

Why don't you check environment and use Buffer in NodeJS or btoa()/atob() in browser, for base64 encoding and decoding:

function base64Encode(plainData) {
    if(isNode) {
        return new Buffer(plainData).toString('base64');
    }else {
        return window.btoa(plainData);
    }
}

function base64Decode(encodedData) {
    if(isNode) {
        return new Buffer(encodedData, 'base64').toString();
    }else {
        return window.atob(encodedData);
    }
}

in your script:

var out;
$.ajax({
  'async' : false,
  'url' : url,
  'success' : function(data, status, xhr) {
    console.log(status);
    console.log(xhr.getResponseHeader('Content-Type'));
    out = base64Encode(data);
  }
});
return out;
})...
like image 139
Burak Kurkcu Avatar answered Nov 09 '22 08:11

Burak Kurkcu