<commit_before>'use strict';

var imports = require('soop').imports();
var bitcore = require('bitcore');

function Insight(opts) {
  opts = opts || {};
  this.host = opts.host || 'localhost';
  this.port = opts.port || '3001';
  this.scheme = opts.scheme || 'http';
}

function _asyncForEach(array, fn, callback) {
  array = array.slice(0);

  function processOne() {
    var item = array.pop();
    fn(item, function(result) {
      if (array.length > 0) {
        setTimeout(processOne, 0); // schedule immediately
      } else {
        callback(); // Done!
      }
    });
  }
  if (array.length > 0) {
    setTimeout(processOne, 0); // schedule immediately
  } else {
    callback(); // Done!
  }
};

function removeRepeatedElements(ar) {
  var ya = false,
    v = "",
    aux = [].concat(ar),
    r = Array();
  for (var i in aux) { // 
    v = aux[i];
    ya = false;
    for (var a in aux) {
      if (v == aux[a]) {
        if (ya == false) {
          ya = true;
        } else {
          aux[a] = "";
        }
      }
    }
  }
  for (var a in aux) {
    if (aux[a] != "") {
      r.push(aux[a]);
    }
  }
  return r;
}

Insight.prototype.getTransactions = function(addresses, cb) {
  var self = this;

  if (!addresses || !addresses.length) return cb([]);

  var txids = [];
  var txs = [];

  _asyncForEach(addresses, function(addr, callback) {
    var options = {
      host: self.host,
      port: self.port,
      scheme: self.scheme,
      method: 'GET',
      path: '/api/addr/' + addr,

      headers: {
        'Access-Control-Request-Headers': ''
      }
    };

    self._request(options, function(err, res) {
      var txids_tmp = res.transactions;
      for (var i = 0; i < txids_tmp.length; i++) {
        txids.push(txids_tmp[i]);
      }
      callback();
    });
  }, function() {
    var clean_txids = removeRepeatedElements(txids);
    _asyncForEach(clean_txids, function(txid, callback2) {
      var options = {
        host: self.host,
        port: self.port,
        scheme: self.scheme,
        method: 'GET',
        path: '/api/tx/' + txid,
        headers: {
          'Access-Control-Request-Headers': ''
        }
      };
      self._request(options, function(err, res) {
        txs.push(res);
        callback2();
      });
    }, function() {
      return cb(txs);
    });
  });
};

Insight.prototype.getUnspent = function(addresses, cb) {
  var self = this;

  if (!addresses || !addresses.length) return cb([]);

  var all = [];

  var options = {
    host: self.host,
    port: self.port,
    scheme: self.scheme,
    method: 'GET',
    path: '/api/addrs/' + addresses.join(',') + '/utxo',

    headers: {
      'Access-Control-Request-Headers': ''
    }
  };

  self._request(options, function(err, res) {
    if (res && res.length > 0) {
      all = all.concat(res);
    }<commit_msg>using POST method instead GET<commit_after>'use strict';

var imports = require('soop').imports();
var bitcore = require('bitcore');

function Insight(opts) {
  opts = opts || {};
  this.host = opts.host || 'localhost';
  this.port = opts.port || '3001';
  this.scheme = opts.scheme || 'http';
}

function _asyncForEach(array, fn, callback) {
  array = array.slice(0);

  function processOne() {
    var item = array.pop();
    fn(item, function(result) {
      if (array.length > 0) {
        setTimeout(processOne, 0); // schedule immediately
      } else {
        callback(); // Done!
      }
    });
  }
  if (array.length > 0) {
    setTimeout(processOne, 0); // schedule immediately
  } else {
    callback(); // Done!
  }
};

function removeRepeatedElements(ar) {
  var ya = false,
    v = "",
    aux = [].concat(ar),
    r = Array();
  for (var i in aux) { // 
    v = aux[i];
    ya = false;
    for (var a in aux) {
      if (v == aux[a]) {
        if (ya == false) {
          ya = true;
        } else {
          aux[a] = "";
        }
      }
    }
  }
  for (var a in aux) {
    if (aux[a] != "") {
      r.push(aux[a]);
    }
  }
  return r;
}

Insight.prototype.getTransactions = function(addresses, cb) {
  var self = this;

  if (!addresses || !addresses.length) return cb([]);

  var txids = [];
  var txs = [];

  _asyncForEach(addresses, function(addr, callback) {
    var options = {
      host: self.host,
      port: self.port,
      scheme: self.scheme,
      method: 'GET',
      path: '/api/addr/' + addr,

      headers: {
        'Access-Control-Request-Headers': ''
      }
    };

    self._request(options, function(err, res) {
      var txids_tmp = res.transactions;
      for (var i = 0; i < txids_tmp.length; i++) {
        txids.push(txids_tmp[i]);
      }
      callback();
    });
  }, function() {
    var clean_txids = removeRepeatedElements(txids);
    _asyncForEach(clean_txids, function(txid, callback2) {
      var options = {
        host: self.host,
        port: self.port,
        scheme: self.scheme,
        method: 'GET',
        path: '/api/tx/' + txid,
        headers: {
          'Access-Control-Request-Headers': ''
        }
      };
      self._request(options, function(err, res) {
        txs.push(res);
        callback2();
      });
    }, function() {
      return cb(txs);
    });
  });
};

Insight.prototype.getUnspent = function(addresses, cb) {
  if (!addresses || !addresses.length) return cb([]);

  var all = [];

  var options = {
    host: this.host,
    port: this.port,
    scheme: this.scheme,
    method: 'POST',
    data: 'addrs=' + addresses.join(','),
    path: '/api/addrs/utxo',
    headers: {
      'Access-Control-Request-Headers': ''
    }
  };

  this._request(options, function(err, res) {
    if (res && res.length > 0) {
      all = all.concat(res);
    }
{'fraction_non_alphanumeric': 0.17053854276663147, 'fraction_numerical': 0.0045758535726856744, 'mean_word_length': 2.125962596259626, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3689858', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1926, 'n_words': 430}