
var readline = require("readline");
var ipworks = require("ipworks");

if(!ipworks) {
    console.error("Cannot find ipworks.");
    return 1;
}
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var http = new ipworks.http();
var key='31504A42534131535542524131535542414E4742333132370000000000000000000000000000000031335442474D5452000058574E355234434A304A34440000'
http.setRuntimeLicense(key)

var oldTransferred=0
var totalTransferred=0
var speed=0
const prettyBytes = require('pretty-bytes');

http.on("Transfer", function(e){
    // console.log(e.text.toString());
    totalTransferred=e.bytesTransferred
    console.log(prettyBytes(e.bytesTransferred),e.percentDone,prettyBytes( speed))
});
http.on("EndTransfer", function(e){
    console.log("Transfer finished.");
});

http.on("SSLServerAuthentication", function(e) {
    e.accept = true;
});

http.setFollowRedirects(1);
http.setLocalFile("./test.mp3");
http.get("cdn.smgmultimedia.com/f7ff408d-9915-4353-8898-854759163697.mp3", function(err) {
    if(err) {
        console.log(err);
        process.exit();
    }
    console.log("This is from the callback function.");
});

setInterval(()=>{
        //console.log("tick tack")
    if(totalTransferred!=oldTransferred){
        speed=(totalTransferred-oldTransferred)*10
        oldTransferred=totalTransferred
    }
    },10
)

console.log("This is the last line in the source.");