<commit_before>'use strict';

let request = require('requestretry');
let config = require('./config');

const api_prefix = config.api_prefix;
const db_store = config.db_store;
const db_name = config.db_name; // elasticsearch index name
const user_agent = config.user_agent;
const client_id = config.client_id;
const client_secret = config.client_secret;

let access_token = '';
let access_token_expire = 0;

let getAccessToken = () => new Promise((resolve, reject) => {
  if (access_token && access_token_expire - Math.floor(new Date().getTime() / 1000) > 300) {
    resolve(access_token);
  } else {
    console.log(`Renewing access_token`);
    request({
      method: 'POST',
      url: `${api_prefix}auth/access_token`,
      json: true,
      form: {
        grant_type: 'client_credentials',
        client_id: client_id,
        client_secret: client_secret
      }
    }, (error, response, body) => {
      if (!error && response.statusCode === 200) {
        access_token = body.access_token;
        access_token_expire = body.expires;
        let expireDateTime = new Date(access_token_expire * 1000).toISOString();
        console.log(`Renewed access_token ${access_token} (valid until ${expireDateTime})`);
        resolve(access_token);
      } else {
        reject(Error(error));
      }
    });
  }
});

let fetchData = (type, id) => new Promise((resolve, reject) => {
  // console.log(`Fetching ${type} ${id}`);
  getAccessToken().then(access_token => {
    request({
      method: 'GET',
      url: `${api_prefix}${type}/${id}/page?access_token=${access_token}`,
      json: true,
      maxAttempts: 5,
      retryDelay: 5000,
      retryStrategy: request.RetryStrategies.HTTPOrNetworkError
    }, (error, response, data) => {
      if (!error && response.statusCode === 200) {
        resolve(data);
        // console.log(`Fetched ${type} ${id}`);
      } else {
        console.log(error);
        console.log(response);
        reject(Error(error));
      }
    });
  });
});

let storeData = (data, index, type, id) => new Promise((resolve, reject) => {
  // console.log(`Storing ${type} ${data.id}`);
  let dataPath = `${db_store}${index}/${type}/${id}`;
  request({
    method: 'PUT',
    url: dataPath,
    json: data
  }, (error, response, body) => {
    if (!error && response.statusCode < 400) {
      resolve(data);
      // console.log(`Stored ${type} ${data.id}`);
    } else {
      console.log(error);
      console.log(response);
      reject(Error(error));
    }
  });
});

let fetchAnime = id => new Promise((resolve, reject) => {
  console.log(`Crawling anime ${id}`);
  fetchData('anime', id)
    .then(anime => {delete anime.airing_stats; return anime;})
    .then(anime =>
      Promise.all([
        storeData(anime, db_name, 'anime', anime.id),

        Promise.all(
          anime.characters.map(character => character.id)
          .filter((elem, index, self) => index === self.indexOf(elem))
          .map(id => fetchData('character', id)
            .then(character => storeData(character, db_name, 'character', character.id))
          )
        ),

        Promise.all(
          anime.staff.map(staff => staff.id).concat(
            anime.characters
            .filter(character => character.actor[0])
            .map(character => character.actor[0].id)
          )
          .filter((elem, index, self) => index === self.indexOf(elem))
          .map(id => fetchData('staff', id)
            .then(staff => storeData(staff, db_name, 'staff', staff.id))
          )
        )
      ])
    )
    .then((anime) => {
      console.log(`Completed anime ${id} (${anime[0].title_japanese})`);
      resolve();
    });
});

let fetchPage = (start, end) => new Promise((resolve, reject) => {
  console.log(`Fetching page ${start}`);
  getAccessToken().then(access_token => {
    request({
      method: 'GET',
      url: `${api_prefix}browse/anime?sort=id&page=${start}&access_token=${access_token}`,
      json: true
    }, (error, response, data) => {
      console.log(`Fetched page ${start}`);
      resolve(data.map(anime => anime.id));
    });
  });
});

let getLastPage = (last_page) => new Promise((resolve, reject) => {
  fetchPage(last_page).then(ids => {
    if (ids.length < 40) {
      console.log(`The last page is ${last_page}`);
      resolve(last_page);
    } else {
      resolve(getLastPage(last_page + 1));
    }
  });
});

let getAnimeIDs = (from, size) => {
  return new Promise((resolve, reject) => {
    let animeList = [];
    request({
      method: 'POST',
      url: `${db_store}anilist/anime/_search`,
      json: {
        "from": from,
        "size": size,
        "stored_fields": ["id"],
        "query": {
          "match_all": {}
        },
        "sort": [{
          "id": {
            "order": "asc"
          }
        }]
      }
    }, function(error, res) {
      if (!error && res.statusCode < 400) {
        res.body.hits.hits.forEach((anime) => {
          animeList.push(anime.sort[0]);
        });
        resolve(animeList);
      } else {
        console.error(res.body);
      }
    });
  });
};

let args = process.argv.slice(2);

args.forEach((param, index) => {
  let value = args[index + 1];
  if (param === '--anime') {
    let animeID = parseInt(value, 10);
    fetchAnime(animeID);
  }
  if (param === '--page') {
    let format = /^(\d+)(-)?(\d+)?$/;
    let startPage = parseInt(value.match(format)[1]);
    let fetchToEnd = value.match(format)[2] === '-';
    let endPage = fetchToEnd ? parseInt(value.match(format)[3]) : startPage;

    getLastPage(256)
      .then(last_page => (endPage < last_page ? endPage : last_page))
      .then(last_page => Array.from(new Array(last_page + 1), (val, index) => index)
        .slice(startPage, last_page + 1)
      )
      .then(pages =>
        pages
        .reduce((result, page) => result.then(() => fetchPage(page)
          .then(ids =>
            ids.reduce((result, id) => result.then(() => fetchAnime(id)), Promise.resolve())
          )
        ), Promise.resolve())
      );
  }
  if (param === '--cleanup') {
    let startPage = 1;

    getLastPage(256)
      .then(last_page => Array.from(new Array(last_page + 1), (val, index) => index)
        .slice(startPage, last_page + 1)
      )
      .then(pages =>
        pages
        .reduce((result, page) => result.then((allAnimeIDs) => fetchPage(page)
          .then(ids => allAnimeIDs.concat(ids))
        ), Promise.resolve([]))
      )
      .then(remoteAnimeIDs => {
        getAnimeIDs(0,100000).then(localIDs => {
          localIDs.forEach(id => {
            if(remoteAnimeIDs.indexOf(id) === -1){
              console.log(id, 'is not found on anilist');
            }
          });
        });
      });
  }
});
<commit_msg>Update last page to 260<commit_after>'use strict';

let request = require('requestretry');
let config = require('./config');

const api_prefix = config.api_prefix;
const db_store = config.db_store;
const db_name = config.db_name; // elasticsearch index name
const user_agent = config.user_agent;
const client_id = config.client_id;
const client_secret = config.client_secret;

let access_token = '';
let access_token_expire = 0;

let getAccessToken = () => new Promise((resolve, reject) => {
  if (access_token && access_token_expire - Math.floor(new Date().getTime() / 1000) > 300) {
    resolve(access_token);
  } else {
    console.log(`Renewing access_token`);
    request({
      method: 'POST',
      url: `${api_prefix}auth/access_token`,
      json: true,
      form: {
        grant_type: 'client_credentials',
        client_id: client_id,
        client_secret: client_secret
      }
    }, (error, response, body) => {
      if (!error && response.statusCode === 200) {
        access_token = body.access_token;
        access_token_expire = body.expires;
        let expireDateTime = new Date(access_token_expire * 1000).toISOString();
        console.log(`Renewed access_token ${access_token} (valid until ${expireDateTime})`);
        resolve(access_token);
      } else {
        reject(Error(error));
      }
    });
  }
});

let fetchData = (type, id) => new Promise((resolve, reject) => {
  // console.log(`Fetching ${type} ${id}`);
  getAccessToken().then(access_token => {
    request({
      method: 'GET',
      url: `${api_prefix}${type}/${id}/page?access_token=${access_token}`,
      json: true,
      maxAttempts: 5,
      retryDelay: 5000,
      retryStrategy: request.RetryStrategies.HTTPOrNetworkError
    }, (error, response, data) => {
      if (!error && response.statusCode === 200) {
        resolve(data);
        // console.log(`Fetched ${type} ${id}`);
      } else {
        console.log(error);
        console.log(response);
        reject(Error(error));
      }
    });
  });
});

let storeData = (data, index, type, id) => new Promise((resolve, reject) => {
  // console.log(`Storing ${type} ${data.id}`);
  let dataPath = `${db_store}${index}/${type}/${id}`;
  request({
    method: 'PUT',
    url: dataPath,
    json: data
  }, (error, response, body) => {
    if (!error && response.statusCode < 400) {
      resolve(data);
      // console.log(`Stored ${type} ${data.id}`);
    } else {
      console.log(error);
      console.log(response);
      reject(Error(error));
    }
  });
});

let fetchAnime = id => new Promise((resolve, reject) => {
  console.log(`Crawling anime ${id}`);
  fetchData('anime', id)
    .then(anime => {delete anime.airing_stats; return anime;})
    .then(anime =>
      Promise.all([
        storeData(anime, db_name, 'anime', anime.id),

        Promise.all(
          anime.characters.map(character => character.id)
          .filter((elem, index, self) => index === self.indexOf(elem))
          .map(id => fetchData('character', id)
            .then(character => storeData(character, db_name, 'character', character.id))
          )
        ),

        Promise.all(
          anime.staff.map(staff => staff.id).concat(
            anime.characters
            .filter(character => character.actor[0])
            .map(character => character.actor[0].id)
          )
          .filter((elem, index, self) => index === self.indexOf(elem))
          .map(id => fetchData('staff', id)
            .then(staff => storeData(staff, db_name, 'staff', staff.id))
          )
        )
      ])
    )
    .then((anime) => {
      console.log(`Completed anime ${id} (${anime[0].title_japanese})`);
      resolve();
    });
});

let fetchPage = (start, end) => new Promise((resolve, reject) => {
  console.log(`Fetching page ${start}`);
  getAccessToken().then(access_token => {
    request({
      method: 'GET',
      url: `${api_prefix}browse/anime?sort=id&page=${start}&access_token=${access_token}`,
      json: true
    }, (error, response, data) => {
      console.log(`Fetched page ${start}`);
      resolve(data.map(anime => anime.id));
    });
  });
});

let getLastPage = (last_page) => new Promise((resolve, reject) => {
  fetchPage(last_page).then(ids => {
    if (ids.length < 40) {
      console.log(`The last page is ${last_page}`);
      resolve(last_page);
    } else {
      resolve(getLastPage(last_page + 1));
    }
  });
});

let getAnimeIDs = (from, size) => {
  return new Promise((resolve, reject) => {
    let animeList = [];
    request({
      method: 'POST',
      url: `${db_store}anilist/anime/_search`,
      json: {
        "from": from,
        "size": size,
        "stored_fields": ["id"],
        "query": {
          "match_all": {}
        },
        "sort": [{
          "id": {
            "order": "asc"
          }
        }]
      }
    }, function(error, res) {
      if (!error && res.statusCode < 400) {
        res.body.hits.hits.forEach((anime) => {
          animeList.push(anime.sort[0]);
        });
        resolve(animeList);
      } else {
        console.error(res.body);
      }
    });
  });
};

let args = process.argv.slice(2);

args.forEach((param, index) => {
  let value = args[index + 1];
  if (param === '--anime') {
    let animeID = parseInt(value, 10);
    fetchAnime(animeID);
  }
  if (param === '--page') {
    let format = /^(\d+)(-)?(\d+)?$/;
    let startPage = parseInt(value.match(format)[1]);
    let fetchToEnd = value.match(format)[2] === '-';
    let endPage = fetchToEnd ? parseInt(value.match(format)[3]) : startPage;

    getLastPage(260)
      .then(last_page => (endPage < last_page ? endPage : last_page))
      .then(last_page => Array.from(new Array(last_page + 1), (val, index) => index)
        .slice(startPage, last_page + 1)
      )
      .then(pages =>
        pages
        .reduce((result, page) => result.then(() => fetchPage(page)
          .then(ids =>
            ids.reduce((result, id) => result.then(() => fetchAnime(id)), Promise.resolve())
          )
        ), Promise.resolve())
      );
  }
  if (param === '--cleanup') {
    let startPage = 1;

    getLastPage(260)
      .then(last_page => Array.from(new Array(last_page + 1), (val, index) => index)
        .slice(startPage, last_page + 1)
      )
      .then(pages =>
        pages
        .reduce((result, page) => result.then((allAnimeIDs) => fetchPage(page)
          .then(ids => allAnimeIDs.concat(ids))
        ), Promise.resolve([]))
      )
      .then(remoteAnimeIDs => {
        getAnimeIDs(0,100000).then(localIDs => {
          localIDs.forEach(id => {
            if(remoteAnimeIDs.indexOf(id) === -1){
              console.log(id, 'is not found on anilist');
            }
          });
        });
      });
  }
});
<|endoftext|>


<commit_before>var util = require('util')
  , fs = require('fs')
  , spawn = require('./spawn')
  , proxy = require('./proxy')
  , errors = require('./errors')
  , cwd = process.cwd();

/**
 * Make some curl opts friendlier.
 */

var curl_map = {
    timeout: 'max-time'
  , redirects: 'max-redirs'
  , method: 'request'
  , useragent: 'user-agent'
};

/**
 * Default user-agents.
 */

var ua_file = __dirname + '/useragents.txt'
  , user_agents = fs.readFileSync(ua_file).toString().split('\n').slice(0, -1)
  , user_agents_len = user_agents.length;

/**
 * Default request headers.
 */

var default_headers = {
    'Accept': '*/*'
  , 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
  , 'Accept-Language': 'en-US,en;q=0.8'
};

/**
 * Make a request with cURL.
 *
 * @param {Object|String} options (optional) - see `man curl`
 * @param {Function} callback (optional)
 * @api public
 */

exports.request = function (options, callback) {
    if (arguments.length === 1) {
        var defaults = options;
        return function (options, callback) {
            if (typeof options === 'function') {
                callback = options;
                options = {};
            } else if (typeof options === 'string') {
                options = { url: options };
            }
            for (var key in defaults) {
                if (typeof options[key] === 'undefined') {
                    options[key] = defaults[key];
                }
            }
            exports.request.call(this, options, callback);
        };
    }

    if (options.retries) {
        var remaining = options.retries;
        delete options.retries;
        return (function curl() {
            exports.request(options, function (err) {
                if (!err || !--remaining) {
                    return callback.apply(this, arguments);
                }
                process.nextTick(curl);
            });
        })();
    }

    if (typeof options === 'string') {
        options = { url: options };
    } else {
        options = exports.copy(options);
    }

    for (var key in curl_map) {
        if (typeof options[key] !== 'undefined') {
            options[curl_map[key]] = options[key];
            delete options[key];
        }
    }

    if (options.proxies) {
        if (!proxy.transform) {
            proxy.transform = proxy.unpack(options.key).transform;
        }
        options = proxy.transform(options);
        delete options.key;
    }

    var curl
      , curl_path = 'curl'
      , args = ['--silent', '--show-error', '--no-buffer']
      , start = new Date
      , err
      , stdoutlen
      , stdout = new Buffer(stdoutlen = 0)
      , encoding
      , complete
      , cleanup
      , postprocess
      , require_str
      , require_not_str
      , scope = {}
      , cmd = 'curl'
      , timeout;

    function finish() {
        if (err in errors) {
            err = errors[err];
        }
        callback.call(scope, err, stdout, {
            cmd: cmd
          , args: args
          , time: (new Date().getTime() - start.getTime())
        });
        complete = true;
    }

    //Allow for a custom curl path
    if (options.curl_path) {
        curl_path = options.curl_path;
        delete options.curl_path;
    }

    //Follow location by default
    if ('max-redirs' in options) {
        options.location = !!options['max-redirs'];
    } else {
        options.location = true;
        options['max-redirs'] = 3;
    }

    //Add an additional setTimeout for max-time
    if (options['max-time']) {
        timeout = setTimeout(function () {
            if (complete) return;
            stderr = 'timeout', stdout = null;
            finish();
            if (curl && curl.kill) curl.kill('SIGKILL');
        }, 1000 * options['max-time']);
    }

    //Default encoding is utf8. Set encoding = null to get a buffer
    if (!options.encoding && options.encoding !== null) {
        options.encoding = 'utf8';
    }
    encoding = options.encoding;
    if (encoding === 'ascii') {
        options['use-ascii'] = true;
    }
    delete options.encoding;

    //Parse POST data
    if (options.data && typeof options.data === 'object') {
        var data = [];
        for (var key in options.data) {
            data.push(encodeURIComponent(key) + '=' + encodeURIComponent(options.data[key]));
        }
        options.data = data.join('&');
    }

    //Check for the occurrence of a string and fail if not found
    if (options.require) {
        require_str = options.require;
        if (!Array.isArray(require_str)) {
            require_str = [require_str];
        }
        delete options.require;
    }

    //Check for the occurrence of a string and fail if found
    if (options.require_not) {
        require_not_str = options.require_not;
        if (!Array.isArray(require_not_str)) {
            require_not_str = [require_not_str];
        }
        delete options.require_not;
    }

    //Call the callback in a custom scope
    if (options.scope) {
        scope = options.scope;
        delete options.scope;
    }

    //Apply a post-processing function?
    if (options.process) {
        postprocess = options.process;
        delete options.process;
    }

    //Setup default headers
    var key, headers = {};
    for (key in default_headers) {
        headers[key] = default_headers[key];
    }
    if (options.headers) {
        for (key in options.headers) {
            key = key.replace(/[_-]/g, ' ').split(' ').map(function (str) {
                if (str.length) {
                    str = str[0].toUpperCase() + str.substr(1);
                }
                return str;
            }).join('-');
            headers[key] = options.headers[key];
        }
        delete options.headers;
    }
    options.header = options.header || [];
    for (key in headers) {
        options.header.push(key + ': ' + headers[key]);
    }

    //Select a random user agent if one wasn't provided
    if (!headers['User-Agent'] && !options['user-agent']) {
        options['user-agent'] = user_agents[Math.random() * user_agents_len | 0];
    }

    //Prepare curl args
    var key, values;
    for (key in options) {
        if (key === 'pretend') {
            continue;
        }
        values = Array.isArray(options[key]) ? options[key] : [options[key]];
        values.forEach(function (value) {
            args.push('--' + key);
            if (true !== value) {
                args.push(value);
            }
        });
    }

    if (options.file) {
        cmd = 'cat';
        args = [options.file];
    }

    //Simulate the spawn?
    if (options.pretend) {
        return finish();
    }

    //Spawn the process
    var child = spawn(cmd, args, { cwd: options.cwd || cwd }, function (curl) {

        //Collect stdout
        curl.stdout.on('data', function (data) {
            if (complete) return;
            var len = data.length, prev = stdout;
            stdout = new Buffer(len + stdoutlen);
            prev.copy(stdout, 0, 0, stdoutlen);
            data.copy(stdout, stdoutlen, 0, len);
            stdoutlen += len;
        });

        //Pipe stderr to the current process?
        if (options.stderr) {
            if (options.stderr === true) {
                curl.stderr.pipe(process.stderr);
                delete options.stderr
            }
        }

        //Handle curl exit
        curl.on('exit', function (code) {
            try {
                err = code;
                if (complete) return;
                if (encoding) {
                    stdout = stdout.toString(encoding);
                }
                if (postprocess && stdout) {
                    stdout = postprocess(stdout);
                }
                if (require_str) {
                    var valid = false;
                    if (!encoding) {
                        stdout = stdout.toString();
                    }
                    for (var i = 0, l = require_str.length; i < l; i++) {
                        if ((util.isRegExp(require_str[i]) && require_str[i].test(stdout))
                                || stdout.indexOf(require_str[i]) !== -1) {
                            valid = true;
                            break;
                        }
                    }
                    if (!valid) {
                        err = 'response does not contain required string(s)';
                        stdout = null
                    } else if (!encoding) {
                        stdout = new Buffer(stdout);
                    }
                }
                if (require_not_str) {
                    var valid = true;
                    if (!encoding) {
                        stdout = stdout.toString();
                    }
                    for (var i = 0, l = require_not_str.length; i < l; i++) {
                        if ((util.isRegExp(require_not_str[i]) && require_not_str[i].test(stdout))
                                || stdout.indexOf(require_not_str[i]) !== -1) {
                            valid = false;
                            break;
                        }
                    }
                    if (!valid) {
                        err = 'response contains bad string(s)';
                        stdout = null
                    } else if (!encoding) {
                        stdout = new Buffer(stdout);
                    }
                }
            } catch (e) {
                err = typeof e === 'object' ? e.message || '' : e;
            }
            finish();
            if (timeout) clearTimeout(timeout);
        });
    });
};

/**
 * Expose a helper for scraping urls from a page.
 */

var urls = /(?:href|src|HREF|SRC)=["']?([^"' >]+)/g;

exports.urls = function (data, regex) {
    var match, matches = [];
    while (match = urls.exec(data)) {
        if (regex && !regex.test(match[1])) {
            continue;
        }
        matches.push(match[1].replace(/[\r\n\t\s]/g, ''));
    }
    return matches;
};

/**
 * A helper for handling async concurrency.
 */

exports.concurrent = function (input, concurrency, fn) {
    if (arguments.length === 3) {
        var len = input.length, pos = 0, remaining = concurrency;
        for (var i = 0; i < concurrency; i++) {
            (function exec() {
                if (pos >= len) {
                    if (!--remaining) {
                        fn(null, function () {});
                    }
                } else {
                    fn(input[pos++], function () {
                        process.nextTick(exec);
                    });
                }
            })();
        }
    } else {
        fn = concurrency;
        concurrency = input;
        for (var i = 0; i < concurrency; i++) {
            (function exec() {
                fn(function () {
                    process.nextTick(exec);
                });
            })();
        }
    }
};

/**
 * A helper for copying an object.
 */

exports.copy = function (obj) {
    var copy = {};
    for (var i in obj) {
        if (typeof obj[i] === 'object') {
            copy[i] = obj[i] ? exports.copy(obj[i]) : null;
        } else {
            copy[i] = obj[i];
        }
    }
    return copy;
};

<commit_msg>If 'fail' option is provided, give curl's error message to callback as 'err'<commit_after>var util = require('util')
  , fs = require('fs')
  , spawn = require('./spawn')
  , proxy = require('./proxy')
  , errors = require('./errors')
  , cwd = process.cwd();

/**
 * Make some curl opts friendlier.
 */

var curl_map = {
    timeout: 'max-time'
  , redirects: 'max-redirs'
  , method: 'request'
  , useragent: 'user-agent'
};

/**
 * Default user-agents.
 */

var ua_file = __dirname + '/useragents.txt'
  , user_agents = fs.readFileSync(ua_file).toString().split('\n').slice(0, -1)
  , user_agents_len = user_agents.length;

/**
 * Default request headers.
 */

var default_headers = {
    'Accept': '*/*'
  , 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
  , 'Accept-Language': 'en-US,en;q=0.8'
};

/**
 * Make a request with cURL.
 *
 * @param {Object|String} options (optional) - see `man curl`
 * @param {Function} callback (optional)
 * @api public
 */

exports.request = function (options, callback) {
    if (arguments.length === 1) {
        var defaults = options;
        return function (options, callback) {
            if (typeof options === 'function') {
                callback = options;
                options = {};
            } else if (typeof options === 'string') {
                options = { url: options };
            }
            for (var key in defaults) {
                if (typeof options[key] === 'undefined') {
                    options[key] = defaults[key];
                }
            }
            exports.request.call(this, options, callback);
        };
    }

    if (options.retries) {
        var remaining = options.retries;
        delete options.retries;
        return (function curl() {
            exports.request(options, function (err) {
                if (!err || !--remaining) {
                    return callback.apply(this, arguments);
                }
                process.nextTick(curl);
            });
        })();
    }

    if (typeof options === 'string') {
        options = { url: options };
    } else {
        options = exports.copy(options);
    }

    for (var key in curl_map) {
        if (typeof options[key] !== 'undefined') {
            options[curl_map[key]] = options[key];
            delete options[key];
        }
    }

    if (options.proxies) {
        if (!proxy.transform) {
            proxy.transform = proxy.unpack(options.key).transform;
        }
        options = proxy.transform(options);
        delete options.key;
    }

    var curl
      , curl_path = 'curl'
      , args = ['--silent', '--show-error', '--no-buffer']
      , start = new Date
      , err
      , stderr = ''
      , stdoutlen
      , stdout = new Buffer(stdoutlen = 0)
      , encoding
      , complete
      , cleanup
      , postprocess
      , require_str
      , require_not_str
      , scope = {}
      , cmd = 'curl'
      , timeout;

    function finish() {
        if (options.fail && stderr) {
            err = String(stderr).replace(/^curl: \(\d+\) /, ''); // "curl: (22) The requested URL returned error..." => "The requested URL returned error..."
        } else if (err in errors) {
            err = errors[err];
        }
        callback.call(scope, err, stdout, {
            cmd: cmd
          , args: args
          , time: (new Date().getTime() - start.getTime())
        });
        complete = true;
    }

    //Allow for a custom curl path
    if (options.curl_path) {
        curl_path = options.curl_path;
        delete options.curl_path;
    }

    //Follow location by default
    if ('max-redirs' in options) {
        options.location = !!options['max-redirs'];
    } else {
        options.location = true;
        options['max-redirs'] = 3;
    }

    //Add an additional setTimeout for max-time
    if (options['max-time']) {
        timeout = setTimeout(function () {
            if (complete) return;
            stderr = 'timeout', stdout = null;
            finish();
            if (curl && curl.kill) curl.kill('SIGKILL');
        }, 1000 * options['max-time']);
    }

    //Default encoding is utf8. Set encoding = null to get a buffer
    if (!options.encoding && options.encoding !== null) {
        options.encoding = 'utf8';
    }
    encoding = options.encoding;
    if (encoding === 'ascii') {
        options['use-ascii'] = true;
    }
    delete options.encoding;

    //Parse POST data
    if (options.data && typeof options.data === 'object') {
        var data = [];
        for (var key in options.data) {
            data.push(encodeURIComponent(key) + '=' + encodeURIComponent(options.data[key]));
        }
        options.data = data.join('&');
    }

    //Check for the occurrence of a string and fail if not found
    if (options.require) {
        require_str = options.require;
        if (!Array.isArray(require_str)) {
            require_str = [require_str];
        }
        delete options.require;
    }

    //Check for the occurrence of a string and fail if found
    if (options.require_not) {
        require_not_str = options.require_not;
        if (!Array.isArray(require_not_str)) {
            require_not_str = [require_not_str];
        }
        delete options.require_not;
    }

    //Call the callback in a custom scope
    if (options.scope) {
        scope = options.scope;
        delete options.scope;
    }

    //Apply a post-processing function?
    if (options.process) {
        postprocess = options.process;
        delete options.process;
    }

    //Setup default headers
    var key, headers = {};
    for (key in default_headers) {
        headers[key] = default_headers[key];
    }
    if (options.headers) {
        for (key in options.headers) {
            key = key.replace(/[_-]/g, ' ').split(' ').map(function (str) {
                if (str.length) {
                    str = str[0].toUpperCase() + str.substr(1);
                }
                return str;
            }).join('-');
            headers[key] = options.headers[key];
        }
        delete options.headers;
    }
    options.header = options.header || [];
    for (key in headers) {
        options.header.push(key + ': ' + headers[key]);
    }

    //Select a random user agent if one wasn't provided
    if (!headers['User-Agent'] && !options['user-agent']) {
        options['user-agent'] = user_agents[Math.random() * user_agents_len | 0];
    }

    //Prepare curl args
    var key, values;
    for (key in options) {
        if (key === 'pretend') {
            continue;
        }
        values = Array.isArray(options[key]) ? options[key] : [options[key]];
        values.forEach(function (value) {
            args.push('--' + key);
            if (true !== value) {
                args.push(value);
            }
        });
    }

    if (options.file) {
        cmd = 'cat';
        args = [options.file];
    }

    //Simulate the spawn?
    if (options.pretend) {
        return finish();
    }

    //Spawn the process
    var child = spawn(cmd, args, { cwd: options.cwd || cwd }, function (curl) {

        //Collect stdout
        curl.stdout.on('data', function (data) {
            if (complete) return;
            var len = data.length, prev = stdout;
            stdout = new Buffer(len + stdoutlen);
            prev.copy(stdout, 0, 0, stdoutlen);
            data.copy(stdout, stdoutlen, 0, len);
            stdoutlen += len;
        });

        //Pipe stderr to the current process?
        if (options.stderr) {
            if (options.stderr === true) {
                curl.stderr.pipe(process.stderr);
                delete options.stderr
            }
        }

        curl.stderr.on('data', function (data) {
          if (complete) return;
          stderr += data;
        });

        //Handle curl exit
        curl.on('exit', function (code) {
            try {
                err = code;
                if (complete) return;
                if (encoding) {
                    stdout = stdout.toString(encoding);
                }
                if (postprocess && stdout) {
                    stdout = postprocess(stdout);
                }
                if (require_str) {
                    var valid = false;
                    if (!encoding) {
                        stdout = stdout.toString();
                    }
                    for (var i = 0, l = require_str.length; i < l; i++) {
                        if ((util.isRegExp(require_str[i]) && require_str[i].test(stdout))
                                || stdout.indexOf(require_str[i]) !== -1) {
                            valid = true;
                            break;
                        }
                    }
                    if (!valid) {
                        err = 'response does not contain required string(s)';
                        stdout = null
                    } else if (!encoding) {
                        stdout = new Buffer(stdout);
                    }
                }
                if (require_not_str) {
                    var valid = true;
                    if (!encoding) {
                        stdout = stdout.toString();
                    }
                    for (var i = 0, l = require_not_str.length; i < l; i++) {
                        if ((util.isRegExp(require_not_str[i]) && require_not_str[i].test(stdout))
                                || stdout.indexOf(require_not_str[i]) !== -1) {
                            valid = false;
                            break;
                        }
                    }
                    if (!valid) {
                        err = 'response contains bad string(s)';
                        stdout = null
                    } else if (!encoding) {
                        stdout = new Buffer(stdout);
                    }
                }
            } catch (e) {
                err = typeof e === 'object' ? e.message || '' : e;
            }
            finish();
            if (timeout) clearTimeout(timeout);
        });
    });
};

/**
 * Expose a helper for scraping urls from a page.
 */

var urls = /(?:href|src|HREF|SRC)=["']?([^"' >]+)/g;

exports.urls = function (data, regex) {
    var match, matches = [];
    while (match = urls.exec(data)) {
        if (regex && !regex.test(match[1])) {
            continue;
        }
        matches.push(match[1].replace(/[\r\n\t\s]/g, ''));
    }
    return matches;
};

/**
 * A helper for handling async concurrency.
 */

exports.concurrent = function (input, concurrency, fn) {
    if (arguments.length === 3) {
        var len = input.length, pos = 0, remaining = concurrency;
        for (var i = 0; i < concurrency; i++) {
            (function exec() {
                if (pos >= len) {
                    if (!--remaining) {
                        fn(null, function () {});
                    }
                } else {
                    fn(input[pos++], function () {
                        process.nextTick(exec);
                    });
                }
            })();
        }
    } else {
        fn = concurrency;
        concurrency = input;
        for (var i = 0; i < concurrency; i++) {
            (function exec() {
                fn(function () {
                    process.nextTick(exec);
                });
            })();
        }
    }
};

/**
 * A helper for copying an object.
 */

exports.copy = function (obj) {
    var copy = {};
    for (var i in obj) {
        if (typeof obj[i] === 'object') {
            copy[i] = obj[i] ? exports.copy(obj[i]) : null;
        } else {
            copy[i] = obj[i];
        }
    }
    return copy;
};

<|endoftext|>


<commit_before>var https = require('http')
  , qs = require('querystring')
  , urlParser = require('url')

  , rest = require('restler')

  , fructose = require('fructose');

var smsPilot = function (opts){
  if(!opts)
    opts = {};

  var url = 'https://smspilot.ru/api.php'
    , responseParser = function(data, callback){
        if(data.match(/^(ERROR=.+)/)){
          this.statusCode = 500;
          data = new Error(data);
        } else if(data.match(/^SUCCESS/)) {
          if(data.match(/SMS\sSENT (\d+)\/(\d+)\n/)){
            var price = parseInt(RegExp.$1)
              , credits = parseInt(RegExp.$2)
              , messages = [];

            data = data.split('\n');
            data.shift();
            for(var i in data){
              if(data[i].match(/^(\d+)\,(\d+)\,(\d+)\,(\d+)$/)){
                messages.push({
                  id: parseInt(RegExp.$1),
                  phone: RegExp.$2,
                  zone: parseInt(RegExp.$3),
                  status: parseInt(RegExp.$4)
                });
              }
            }

            data = {
              price: price,
              credits: credits,
              messages: messages
            };
          }
        }

        callback(data);
      };

  if(opts.ssl === false)
    url = url.replace(/^https/, 'http');

  var send = function(data){
    data.apikey = settings.apikey;
    if(!data.from)
      data.from = settings.from;

    return rest[opts.method || 'post'](url, { parser: responseParser, data: data });
  }

  settings = {};
  this.set = function(name, value){
    if(typeof name == 'object' && !value){
      settings = name;
      return this;
    }
    if(value){
      settings[name] = value;
      return this;
    } else {
      return settings[name];
    }
  }

  this.sms = function(data, callback){
    if(Array.isArray(data.to)){
      data.to = data.to.join(',');
    }

    return send(data);
  }

  return this;
}

//
//
//

var pilot = new smsPilot();
pilot
  .set('apikey', 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ')
  .set('from', 'test');

pilot.sms({
  to: '79165193353,79165555555',
  send: 'test test'
}).on('success', function(data){
  console.log('>>', data)
}).on('error', function(){
  console.log('error', arguments)
});

//module.exports = sms;
<commit_msg>Добавил методы:   pilot.status()   pilot.balance()<commit_after>var https = require('http')
  , qs = require('querystring')
  , urlParser = require('url')

  , rest = require('restler')

  , fructose = require('fructose');

//
//
//

var smsPilot = function (opts){
  if(!opts)
    opts = {};

  var url = 'https://smspilot.ru/api.php';

  //
  //
  //

  var self = this
    , responseParser = function(data, callback){
    if(data.match(/^(ERROR=.+)/)){
      this.statusCode = 500;
      data = new Error(data);
    } else if(data.match(/^SUCCESS/)) {

      if(data.match(/SMS\sSENT (\d+)\/(\d+)\n/)){ // sms
        var price = parseInt(RegExp.$1)
          , credits = parseInt(RegExp.$2)
          , messages = [];

        data = data.split('\n');
        data.shift();

        var getStatus = function(){ // get status from messages array
          return self.status(this.id);
        }

        for(var i in data){
          if(data[i].match(/^(\d+)\,(\d+)\,(\d+)\,(\d+)$/)){
            messages.push({
              id: parseInt(RegExp.$1),
              phone: RegExp.$2,
              zone: parseInt(RegExp.$3),
              status: parseInt(RegExp.$4),
              getStatus: getStatus
            });
          }
        }

        data = {
          price: price,
          credits: credits,
          messages: messages
        };

      }
    } else {
      if(data.match(/^(\d+)$/)){ // balance

        data = parseInt(RegExp.$1);

      } else { // status

        data = data.split('\n');
        var statuses = [];
        for(var i in data){
          if(data[i].match(/(\d+),(\d+),(\d+),(-?\d+)/)){
            statuses.push({
              id: RegExp.$1,
              phone: RegExp.$2,
              zone: RegExp.$3,
              status: RegExp.$4
            });
          }
        }

        if(statuses.length == 1){
          data = statuses[0];
        } else {
          data = statuses;
        }

      }
    }

    callback(data);
  }

  if(opts.ssl === false)
    url = url.replace(/^https/, 'http');

  var send = function(data){
    data.apikey = settings.apikey;
    if(!data.from)
      data.from = settings.from;

    return rest[opts.method || 'post'](url, { parser: responseParser, data: data });
  }

  settings = {};
  this.set = function(name, value){
    if(typeof name == 'object' && !value){
      settings = name;
      return this;
    }
    if(value){
      settings[name] = value;
      return this;
    } else {
      return settings[name];
    }
  }

  this.sms = function(data){
    if(Array.isArray(data.to))
      data.to = data.to.join(',');

    return send(data);
  }

  this.status = function(id){
    if(Array.isArray(id))
      id = id.join(',');

    return send({ check: id });
  }

  this.balance = function(){
    return send({ balance: 'sms' });
  }

  return this;
}

//
//
//

var pilot = new smsPilot();
pilot
  .set('apikey', 'XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ')
  .set('from', 'test');
pilot.balance().on('success', function(data){
  console.log(data)
})

pilot.status([ 10000, 10001 ]).on('success', function(data){
  console.log(data)
})

pilot.sms({
  to: '79165193353,79165555555',
  send: 'test test'
}).on('success', function(data){
  data.messages[0].getStatus().on('success', function(data){
    console.log('from messages array', data)
  })
  console.log('>>', data)
}).on('error', function(){
  console.log('error', arguments)
});

//module.exports = sms;
<|endoftext|>


<commit_before>var fs = require('fs');
var aync = require('async');
var exec = require('child_process').exec;
var helpers = require('../helpers/helpers.js');
var builder = require('../helpers/dockerfileBuilder.js');
var spawnSeriesLib = require('../helpers/spawnSeries.js');
var dockerSpawnSeries = spawnSeriesLib.dockerSpawnSeries;
var configFile = "./.lifter/lifter.yml";

var exitInstructions = [];

// if boot2docker VM doesn't exist: init , else boot
var start_b2d = function() {
  exec('boot2docker info', function(err, stdout, stderr) {
    if (err) {
      if (/machine not exist/.test(stderr)) {
        console.log('boot2docker VM not installed, running boot2docker init');
        exec_b2d_init();
      }
    } else {
      boot_b2d();
    }
  });
};

// create boot2docker VM, then boot
var exec_b2d_init = function() {
  exec('boot2docker init', function(err, stdout, stderr) {
    if (err) console.log(stderr);
    console.log('boot2docker VM created!');
    boot_b2d();
  });
};

// If b2d is powered off, power it on
var boot_b2d = function() {
  exec('boot2docker status', function (err, stdout, stderr) {
    if (err) console.log(err);
    if (/running/.test(stdout)) {
      console.log('boot2docker is already running');
      finishInit();
    } else { //if (/poweroff/.test(stdout)) {
      console.log('boot2docker not running, powering on...');
      exec_b2d_Up();
    }
  });
}

var exec_b2d_Up = function() {
  var setEnvs = false;
  var exportCmds = [];
  exec('boot2docker up', function(err, stdout, stderr) {
    if (err) console.log("exec err: ", err);
    console.log('boot2docker VM powered on');
    finishInit();
  });
};

// check /etc/hosts for proper dockerhost IP
var checkHostsFileForDockerhost = function() {
  var hosts = fs.readFileSync('/etc/hosts');
  var hostsWritten = /dockerhost/.test(hosts);

  exec('boot2docker ip', function(err, stdout, stderr) {
    if (err) console.log(stderr);
    // grab ip out of stdout
    var ip = /\d*\.\d*\.\d*\.\d*/.exec(stdout)[0];
    var ipRegex = new RegExp(ip);
    // change line if ip not in hosts file

    createShellScript();
    builder.buildDockerfile();

    if (hostsWritten) {
      console.log('dockerhost found in /etc/hosts');
      if (!ipRegex.test(hosts)) {
        console.log('dockerhost IP incorrect... removing');
        // removeIPinHostsFile(ip);
        exitInstructions.push(
          'echo sed \'/dockerhost/d\' /etc/hosts | sudo tee /etc/hosts');
        exitInstructions.push(
          'echo ' + ip + ' dockerhost | sudo tee -a /etc/hosts');
      } else {
        console.log('dockerhost IP is correct');
      }
    } else {
      console.log('dockerhost not found in /etc/hosts... adding');
      exitInstructions.push(
          'echo ' + ip + ' dockerhost | sudo tee -a /etc/hosts');
      // addDockerhostToHostsFile(ip);
    }
    // console.log(exitInstructions);
    createLocalContainer();
  });

}

// create shell script to launch app
var createShellScript = function() {
  var settings = helpers.readYAML();

  var dbLink = settings.dbContainerName + '-link';

  var shellFileContent = '#!/bin/sh\n' +

                         // wait 10 seconds for containers to be configured
                         'sleep 10\n' +

                         'DB_PORT=' + settings.dbPort + "\n" +
                         'CURL_OUTPUT=$(curl ' +dbLink+ ':$DB_PORT)\n' +
                         // this success message is  specific to mongo, need to change
                         'SUCCESS="It looks like you are trying to access MongoDB over HTTP on the native driver port."\n' +
                         'LAUNCH_CMD="' + settings.launchCommand + '"\n' +

                         // checking if application and database container are linked
                         'until [[ $CURL_OUTPUT == $SUCCESS ]]\n' +
                         // if they are not, waits 20seconds and checks again
                         'do\n' +
                         'echo "Linking database container"\n' +
                         'sleep 20\n' +
                         'CURL_OUTPUT=$(curl ' +dbLink+ ':$DB_PORT)\n' +
                         // otherwise it runns the launch command
                         'done\n' +
                         'echo "Containers linked, running application launch command"\n' +
                         'cd prod\n' +
                         '$LAUNCH_CMD';

  fs.writeFile("./.lifter/app.sh", shellFileContent, function(err) {
    if (err) console.log(err);
    console.log("Launch script created: app.sh");
  });
}

var createLocalContainer = function() {
  var settings = helpers.readYAML();
  //docker build -t username_on_docker_hub/create_new_repo_name .
  var appContainerName = settings.containerName;
  var imageName = settings.username + '/' + settings.repoName;

  var dbContainerName = settings.dbContainerName;
  var dbImageName = settings.dbTag;
  var dbLinkName = dbContainerName+'-link';

  var buildCmd = ['docker',
    ['build', '-t', imageName, '.']];
  var dbRunCmd = ['docker',
    ['run', '--restart=always', '-d',
     '--name', dbContainerName, dbImageName]];
  var appRunCmd = ['docker',
    ['run', '--restart=always',
     '--name', appContainerName,
     '--link', dbContainerName+':'+dbLinkName,
     '-p', settings.portPrivate+':'+settings.portPublic,
     '-v', settings.launchPath+':/src:ro', imageName, 'sh', '/src/app.sh']];

  dockerSpawnSeries([
    buildCmd,
    dbRunCmd,
    appRunCmd
  ], finishInit);
}

var printInstructions = function() {
  if (exitInstructions.length > 0) {
    console.log("Run these commands:")
    exitInstructions.forEach(function(str) {
      console.log("    " + str);
    });
  }
}

var finishInit = function () {
  aync.series([
    checkHostsFileForDockerhost,
    createShellScript,
    createLocalContainer
  ]);
  console.log('Local docker environment created!');
  printInstructions();
}

module.exports = {
  start_b2d: start_b2d
}
<commit_msg>remove async series, refactor to just call next functions as needed -add docker container removal commands at start of script -change dblink name in generated app.sh -change cd prod to cd /src in app.sh -change settings.launchPath to settings.currentWorkingDir for docker cmd -change /src/app.sh to /src/.lifter/app.sh in docker cmd<commit_after>var fs = require('fs');
var aync = require('async');
var exec = require('child_process').exec;
var helpers = require('../helpers/helpers.js');
var builder = require('../helpers/dockerfileBuilder.js');
var spawnSeriesLib = require('../helpers/spawnSeries.js');
var dockerSpawnSeries = spawnSeriesLib.dockerSpawnSeries;
var configFile = "./.lifter/lifter.yml";

var exitInstructions = [];

// if boot2docker VM doesn't exist: init , else boot
var start_b2d = function() {
  exec('boot2docker info', function(err, stdout, stderr) {
    if (err) {
      if (/machine not exist/.test(stderr)) {
        console.log('boot2docker VM not installed, running boot2docker init');
        exec_b2d_init();
      }
    } else {
      boot_b2d();
    }
  });
};

// create boot2docker VM, then boot
var exec_b2d_init = function() {
  exec('boot2docker init', function(err, stdout, stderr) {
    if (err) console.log(stderr);
    console.log('boot2docker VM created!');
    boot_b2d();
  });
};

// If b2d is powered off, power it on
var boot_b2d = function() {
  exec('boot2docker status', function (err, stdout, stderr) {
    if (err) console.log(err);
    if (/running/.test(stdout)) {
      console.log('boot2docker is already running');
      createShellScript();
    } else { //if (/poweroff/.test(stdout)) {
      console.log('boot2docker not running, powering on...');
      exec_b2d_Up();
    }
  });
}

var exec_b2d_Up = function() {
  var setEnvs = false;
  var exportCmds = [];
  exec('boot2docker up', function(err, stdout, stderr) {
    if (err) console.log("exec err: ", err);
    console.log('boot2docker VM powered on');
    createShellScript();
  });
};

// check /etc/hosts for proper dockerhost IP
var checkHostsFileForDockerhost = function() {
  var hosts = fs.readFileSync('/etc/hosts');
  var hostsWritten = /dockerhost/.test(hosts);

  exec('boot2docker ip', function(err, stdout, stderr) {
    if (err) console.log(stderr);
    // grab ip out of stdout
    var ip = /\d*\.\d*\.\d*\.\d*/.exec(stdout)[0];
    var ipRegex = new RegExp(ip);
    // change line if ip not in hosts file

    if (hostsWritten) {
      console.log('dockerhost found in /etc/hosts');
      if (!ipRegex.test(hosts)) {
        console.log('dockerhost IP incorrect... removing');
        // removeIPinHostsFile(ip);
        exitInstructions.push(
          'echo sed \'/dockerhost/d\' /etc/hosts | sudo tee /etc/hosts');
        exitInstructions.push(
          'echo ' + ip + ' dockerhost | sudo tee -a /etc/hosts');
      } else {
        console.log('dockerhost IP is correct');
      }
    } else {
      console.log('dockerhost not found in /etc/hosts... adding');
      exitInstructions.push(
          'echo ' + ip + ' dockerhost | sudo tee -a /etc/hosts');
      // addDockerhostToHostsFile(ip);
    }
    // console.log(exitInstructions);
  });

}

// create shell script to launch app
var createShellScript = function() {
  // asynchronously build dockerfile
  builder.buildDockerfile();
  var settings = helpers.readYAML();

  var dbLink = settings.dbContainerName + '-link';

  var shellFileContent = '#!/bin/sh\n' +

                         // wait 10 seconds for containers to be configured
                         'sleep 10\n' +

                         'DB_PORT=' + settings.dbPort + "\n" +
                         'CURL_OUTPUT=$(curl ' +dbLink+ ':$DB_PORT)\n' +

                         // this success message is  specific to mongo, need to change
                         'SUCCESS="It looks like you are trying to access MongoDB over HTTP on the native driver port."\n' +
                         'LAUNCH_CMD="' + settings.launchCommand + '"\n' +

                         // checking if application and database container are linked
                         'until [[ $CURL_OUTPUT == $SUCCESS ]]\n' +
                         // if they are not, waits 20seconds and checks again
                         'do\n' +
                         'echo "Linking database container"\n' +
                         'sleep 20\n' +
                         'CURL_OUTPUT=$(curl ' +dbLink+ ':$DB_PORT)\n' +

                         // otherwise it runns the launch command
                         'done\n' +
                         'echo "Containers linked, running application launch command"\n' +
                         'cd /src\n' +
                         '$LAUNCH_CMD';

  fs.writeFile("./.lifter/app.sh", shellFileContent, function(err) {
    if (err) console.log(err);
    console.log("Launch script created: app.sh");
    createLocalContainer();
  });

}

var createLocalContainer = function() {
  var rmAppContainer = ['docker',
    ['rm', '-f', 'app']];
  var rmDbContainer = ['docker' ,
    ['rm', '-f', 'app-db']];

  var settings = helpers.readYAML();
  //docker build -t username_on_docker_hub/create_new_repo_name .
  var appContainerName = settings.containerName;
  var imageName = settings.username + '/' + settings.repoName;

  var dbContainerName = settings.dbContainerName;
  var dbImageName = settings.dbTag;
  var dbLinkName = dbContainerName+'-link';

  var buildCmd = ['docker',
    ['build', '-t', imageName, '.']];
  var dbRunCmd = ['docker',
    ['run', '--restart=always', '-d',
     '--name', dbContainerName, dbImageName]];
  var appRunCmd = ['docker',
    ['run', '--restart=always',
     '--name', appContainerName,
     '--link', dbContainerName+':'+dbLinkName,
     '-p', settings.portPrivate+':'+settings.portPublic,
     '-v', settings.currentWorkingDir+':/src:ro', imageName, 'sh', '/src/.lifter/app.sh']];

  dockerSpawnSeries([
    rmAppContainer,
    rmDbContainer,
    buildCmd,
    dbRunCmd,
    appRunCmd
  ], finishInit);
}

var printInstructions = function() {
  console.log('Local docker environment created!');
  if (exitInstructions.length > 0) {
    console.log("Run these commands:")
    exitInstructions.forEach(function(str) {
      console.log("    " + str);
    });
  }
}

var finishInit = function () {
  console.log('FINISHED FINISH INIT');
}

module.exports = {
  start_b2d: start_b2d
}
<|endoftext|>


<commit_before>var exec = require('child_process').exec;
var execFile = require('child_process').execFile;
var os = require('os');
var path = require('path');

var listeners = [],
	idle = {};

// timeout value to ask the system it's idle value
var AFK_SYSTEM_POLLING_TIMEOUT_MSEC = 2 * 1000;

// percentage of the interval to be checked against the idle time. This avoid settings dynamic intervals.
var INTERVAL_AWAY_IDLE_TIME_PERCENTAGE = 0.70;

idle.tick = function (callback) {
	callback = callback || function (){};

	if (/^win/.test(process.platform)) {
		var cmd = path.join( __dirname, 'bin', 'idle.exe');
		execFile(cmd, function (error, stdout, stderr) {
			if(error) {
				callback(0, error);
				return;
			}
			callback(Math.floor(parseInt(stdout, 10) / 1000), null);
		});
	}
	else if (/darwin/.test(process.platform)) {
		var cmd = '/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk \'/HIDIdleTime/ {print int($NF/1000000000); exit}\'';
		exec(cmd, function (error, stdout, stderr) {
			if(error) {
				callback(0, error);
				return;
			}
			callback(parseInt(stdout, 10), null);
		});
	}
	else if (/linux/.test(process.platform)) {
		var cmd = 'xprintidle';
		exec(cmd, function (error, stdout, stderr) {
			if(error) {
				callback(0, error);
				return;
			}
			callback(Math.round(parseInt(stdout, 10) / 1000), null);
		});
	}
	else {
		callback(0);
	}
};

idle.addListener = function (intervalSec, callback) {
    var isAfk = false;

    var listenerIndex = listeners.push(true) - 1;
    var defaultIntervalMsec = intervalSec * 1000;
    var timeoutId = null;
    var lastCheckDateMsec = null;

    var checkIsAway = function () {

        if(!listeners[listenerIndex]) {
            clearTimeout(timeoutId);
            return;
        }

        // calculate the duration of the interval. if there's no previous lastCheckDateMsec the duration is a very big number
        // to have no influence on the afk status.
        intervalDurationMsec = Number.MAX_VALUE;
        if(lastCheckDateMsec) {
            intervalDurationMsec = Date.now() - lastCheckDateMsec;
        }

        lastCheckDateMsec = Date.now();

        // ask the system what's the idle time
        idle.tick(function(idleSeconds, error){

            if(error) {
                callback({ id: listenerIndex }, error);
                timeoutId = setTimeout(checkIsAway, defaultIntervalMsec);
                return;
            }

            // is aways if the idle duration is bigger than a interval duration fraction
            isAway = (idleSeconds * 1000) >= (intervalDurationMsec * INTERVAL_AWAY_IDLE_TIME_PERCENTAGE);

            if(!isAfk && isAway) {
                callback({
                	status: 'away',
                	seconds: idleSeconds,
                	id: listenerIndex
                });

                isAfk = true;
            }
            else if(isAfk && !isAway) {
                callback({
                    status: 'back',
                    seconds: idleSeconds,
                    id: listenerIndex
                });

                isAfk = false;
            }

            timeoutId = setTimeout(checkIsAway, isAfk ? AFK_SYSTEM_POLLING_TIMEOUT_MSEC : defaultIntervalMsec);
        });
    };

    checkIsAway();

    return listenerIndex;
};

idle.removeListener = function (listenerIndex) {
    if(listenerIndex < listeners.length){
        listeners.splice(listenerIndex, 1);
        return true;
    }
    else {
        return false;
    }
};

module.exports = idle;
<commit_msg>convert tabs to spaces<commit_after>var exec = require('child_process').exec;
var execFile = require('child_process').execFile;
var os = require('os');
var path = require('path');

var listeners = [],
    idle = {};

// timeout value to ask the system it's idle value
var AFK_SYSTEM_POLLING_TIMEOUT_MSEC = 2 * 1000;

// percentage of the interval to be checked against the idle time. This avoid settings dynamic intervals.
var INTERVAL_AWAY_IDLE_TIME_PERCENTAGE = 0.70;

idle.tick = function (callback) {
    callback = callback || function (){};

    if (/^win/.test(process.platform)) {
        var cmd = path.join( __dirname, 'bin', 'idle.exe');
        execFile(cmd, function (error, stdout, stderr) {
            if(error) {
                callback(0, error);
                return;
            }
            callback(Math.floor(parseInt(stdout, 10) / 1000), null);
        });
    }
    else if (/darwin/.test(process.platform)) {
        var cmd = '/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk \'/HIDIdleTime/ {print int($NF/1000000000); exit}\'';
        exec(cmd, function (error, stdout, stderr) {
            if(error) {
                callback(0, error);
                return;
            }
            callback(parseInt(stdout, 10), null);
        });
    }
    else if (/linux/.test(process.platform)) {
        var cmd = 'xprintidle';
        exec(cmd, function (error, stdout, stderr) {
            if(error) {
                callback(0, error);
                return;
            }
            callback(Math.round(parseInt(stdout, 10) / 1000), null);
        });
    }
    else {
        callback(0);
    }
};

idle.addListener = function (intervalSec, callback) {
    var isAfk = false;

    var listenerIndex = listeners.push(true) - 1;
    var defaultIntervalMsec = intervalSec * 1000;
    var timeoutId = null;
    var lastCheckDateMsec = null;

    var checkIsAway = function () {

        if(!listeners[listenerIndex]) {
            clearTimeout(timeoutId);
            return;
        }

        // calculate the duration of the interval. if there's no previous lastCheckDateMsec the duration is a very big number
        // to have no influence on the afk status.
        intervalDurationMsec = Number.MAX_VALUE;
        if(lastCheckDateMsec) {
            intervalDurationMsec = Date.now() - lastCheckDateMsec;
        }

        lastCheckDateMsec = Date.now();

        // ask the system what's the idle time
        idle.tick(function(idleSeconds, error){

            if(error) {
                callback({ id: listenerIndex }, error);
                timeoutId = setTimeout(checkIsAway, defaultIntervalMsec);
                return;
            }

            // is aways if the idle duration is bigger than a interval duration fraction
            isAway = (idleSeconds * 1000) >= (intervalDurationMsec * INTERVAL_AWAY_IDLE_TIME_PERCENTAGE);

            if(!isAfk && isAway) {
                callback({
                	status: 'away',
                	seconds: idleSeconds,
                	id: listenerIndex
                });

                isAfk = true;
            }
            else if(isAfk && !isAway) {
                callback({
                    status: 'back',
                    seconds: idleSeconds,
                    id: listenerIndex
                });

                isAfk = false;
            }

            timeoutId = setTimeout(checkIsAway, isAfk ? AFK_SYSTEM_POLLING_TIMEOUT_MSEC : defaultIntervalMsec);
        });
    };

    checkIsAway();

    return listenerIndex;
};

idle.removeListener = function (listenerIndex) {
    if(listenerIndex < listeners.length){
        listeners.splice(listenerIndex, 1);
        return true;
    }
    else {
        return false;
    }
};

module.exports = idle;
<|endoftext|>


<commit_before>/*globals document*/

import React from 'react';
import Header from './Header/Header';
import Footer from './Footer/Footer';
import ApplicationStore from '../stores/ApplicationStore';
import { connectToStores, provideContext } from 'fluxible-addons-react';
import { handleHistory } from 'fluxible-router';
import pages from '../configs/routes';
import ErrorStore from '../stores/ErrorStore';
import ErrorComponent from './Error/Error';

class Application extends React.Component {
    render() {
        let Handler = this.props.currentRoute.handler;
        if (this.props.ErrorStore.error) {
            return (

                    <ErrorComponent error={this.props.ErrorStore.error} />

            );
        }
        else {
            return (

                    <Handler />

            );
        }
    }

    componentDidUpdate(prevProps, prevState) {
        const newProps = this.props;
        if (newProps.pageTitle === prevProps.pageTitle) {
            return;
        }
        document.title = newProps.pageTitle;
    }
}

export default provideContext(handleHistory(connectToStores(
    Application,
    [ApplicationStore, ErrorStore],
    (context, props) => {
        let appStore = context.getStore(ApplicationStore);
        return {
            pageTitle: appStore.getPageTitle(),
            ErrorStore: context.getStore(ErrorStore).getState()
        };
    }
)));
<commit_msg>Fix for SWIK-651. ErrorComponent changed to Error<commit_after>/*globals document*/

import React from 'react';
import Header from './Header/Header';
import Footer from './Footer/Footer';
import ApplicationStore from '../stores/ApplicationStore';
import { connectToStores, provideContext } from 'fluxible-addons-react';
import { handleHistory } from 'fluxible-router';
import pages from '../configs/routes';
import ErrorStore from '../stores/ErrorStore';
import Error from './Error/Error';

class Application extends React.Component {
    render() {
        let Handler = this.props.currentRoute.handler;
        if (this.props.ErrorStore.error) {
            return (

                    <Error error={this.props.ErrorStore.error} />

            );
        }
        else {
            return (

                    <Handler />

            );
        }
    }

    componentDidUpdate(prevProps, prevState) {
        const newProps = this.props;
        if (newProps.pageTitle === prevProps.pageTitle) {
            return;
        }
        document.title = newProps.pageTitle;
    }
}

export default provideContext(handleHistory(connectToStores(
    Application,
    [ApplicationStore, ErrorStore],
    (context, props) => {
        let appStore = context.getStore(ApplicationStore);
        return {
            pageTitle: appStore.getPageTitle(),
            ErrorStore: context.getStore(ErrorStore).getState()
        };
    }
)));
<|endoftext|>


<commit_before>import React from "react"
import PropTypes from "prop-types"
import { connect } from "react-redux"
import MetaPayload from "../MetaPayload/MetaPayload"
import { Grid, Row, Col } from "react-flexbox-grid"
import { Card, CardTitle, CardText } from "material-ui/Card"

const nodeProps = ["ID", "Name", "Status", "Datacenter", "Drain", "HTTPAddr", "NodeClass"]

const withPrefix = function withPrefix(obj, prefix) {
  const result = {}

  Object.keys(obj || {}).forEach(key => {
    if (key.startsWith(prefix)) {
      result[key.replace(prefix, "")] = obj[key]
    }
  })

  return result
}

const ClientInfo = ({ node }) =>
  <Grid fluid style={{ padding: 0 }}>
    <Row>
      <Col key="properties-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Client Properties" />
          <CardText>

              {nodeProps.map(nodeProp =>

                    {nodeProp}

                    {node[nodeProp] ? node[nodeProp] : "-"}

              )}

          </CardText>
        </Card>
      </Col>
      <Col key="meta-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Meta Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={node.Meta} identifier="meta" />
          </CardText>
        </Card>
      </Col>
    </Row>
    <Row style={{ marginTop: "1rem" }}>
      <Col key="cpu-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="CPU Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "cpu.")} identifier="cpu" />
          </CardText>
        </Card>
      </Col>
      <Col key="driver-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Driver Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "driver.")} identifier="driver" />
          </CardText>
        </Card>
      </Col>
    </Row>
    <Row style={{ marginTop: "1rem" }}>
      <Col key="kernel-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Kernel Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "kernel.")} identifier="kernel" />
          </CardText>
        </Card>
      </Col>
      <Col key="unique-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Unique Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "unique.")} identifier="unique" />
          </CardText>
        </Card>
      </Col>
    </Row>
    <Row style={{ marginTop: "1rem" }}>
      <Col key="nomad-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Nomad Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "nomad.")} identifier="nomad" />
          </CardText>
        </Card>
      </Col>
      <Col key="value-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Vault Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "vault.")} identifier="vault" />
          </CardText>
        </Card>
      </Col>
    </Row>
    <Row style={{ marginTop: "1rem" }}>
      <Col key="consul-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Consul Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "consul.")} identifier="consul" />
          </CardText>
        </Card>
      </Col>
      <Col key="os-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="OS Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "os.")} identifier="os" />
          </CardText>
        </Card>
      </Col>
    </Row>
    <Row style={{ marginTop: "1rem" }}>
      <Col key="memory-pane" xs={12} sm={12} md={6} lg={6}>
        <Card>
          <CardTitle title="Memory Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "memory.")} identifier="memory" />
          </CardText>
        </Card>
      </Col>
    </Row>
  </Grid>

function mapStateToProps({ node }) {
  return { node }
}

ClientInfo.propTypes = {
  node: PropTypes.object.isRequired
}

export default connect(mapStateToProps)(ClientInfo)
<commit_msg>restructure client info page<commit_after>import React from "react"
import PropTypes from "prop-types"
import { connect } from "react-redux"
import MetaPayload from "../MetaPayload/MetaPayload"
import { Grid, Row, Col } from "react-flexbox-grid"
import { Card, CardTitle, CardText } from "material-ui/Card"

const nodeProps = ["ID", "Name", "Status", "Datacenter", "Drain", "HTTPAddr", "NodeClass"]

const withPrefix = function withPrefix(obj, prefix) {
  const result = {}

  Object.keys(obj || {}).forEach(key => {
    if (key.startsWith(prefix)) {
      result[key.replace(prefix, "")] = obj[key]
    }
  })

  return result
}

const ClientInfo = ({ node }) =>
  <Grid fluid style={{ padding: 0 }}>
    <Row style={{ marginTop: "1rem" }}>
      <Col key="resource-pane" xs={12} sm={12} md={6} lg={6}>
        <Card key="client">
          <CardTitle title="Client Properties" />
          <CardText>

              {nodeProps.map(nodeProp =>

                    {nodeProp}

                    {node[nodeProp] ? node[nodeProp] : "-"}

              )}

          </CardText>
        </Card>
        <Card key="nomad">
          <CardTitle title="Nomad Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "nomad.")} identifier="nomad" />
          </CardText>
        </Card>
        <Card key="vault">
          <CardTitle title="Vault Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "vault.")} identifier="vault" />
          </CardText>
        </Card>
        <Card key="cpu">
          <CardTitle title="CPU Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "cpu.")} identifier="cpu" />
          </CardText>
        </Card>
        <Card key="memory">
          <CardTitle title="Memory Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "memory.")} identifier="memory" />
          </CardText>
        </Card>
        <Card key="kernel">
          <CardTitle title="Kernel Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "kernel.")} identifier="kernel" />
          </CardText>
        </Card>
        <Card key="os">
          <CardTitle title="OS Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "os.")} identifier="os" />
          </CardText>
        </Card>
      </Col>
      <Col key="driver-pane" xs={12} sm={12} md={6} lg={6}>
        <Card key="meta">
          <CardTitle title="Meta Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={node.Meta} identifier="meta" />
          </CardText>
        </Card>
        <Card key="consul">
          <CardTitle title="Consul Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "consul.")} identifier="consul" />
          </CardText>
        </Card>
        <Card key="driver">
          <CardTitle title="Driver Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "driver.")} identifier="driver" />
          </CardText>
        </Card>
        <Card key="unique">
          <CardTitle title="Unique Properties" />
          <CardText>
            <MetaPayload dtWithClass="wide" metaBag={withPrefix(node.Attributes, "unique.")} identifier="unique" />
          </CardText>
        </Card>
      </Col>
    </Row>
  </Grid>

function mapStateToProps({ node }) {
  return { node }
}

ClientInfo.propTypes = {
  node: PropTypes.object.isRequired
}

export default connect(mapStateToProps)(ClientInfo)
<|endoftext|>


<commit_before>'use strict';

var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var _ = require('lodash');
var nunjucks = require('nunjucks');
var path = require('path');
var fs = require('fs');

// Constants
var PLUGIN_NAME = 'gulp-nunjucks';
var PLUGINS_DIR = 'plugins';

module.exports = function (options) {
	options = _.extend({
		templatePath: 'templates',
		templateExtension: '.html',
		defaultTemplate: 'base',
		nunjucksConfig: {
			autoescape: false
		},
		pluginDirectory: 'plugins',
		property: 'meta'
	}, options || {});

	// Configure Nunjucks
	var nj = new nunjucks.Environment(
		new nunjucks.FileSystemLoader(options.templatePath),
		options.nunjucksConfig);

	// Load plugins
	_.each(fs.readdirSync(PLUGINS_DIR), function(file) {
		var plugin = file.match(/^(filter|tag)-(.*)(?:.js$)/);
		var type = plugin[1];
		var name = plugin[2];
		var fn = require(path.join(options.pluginDirectory, file));

		if (type === 'filter') {
			nj.addFilter(name, fn);
		}
		if (type === 'tag') {
			nj.addExtension(name, fn);
		}
	});

	return through.obj(function(file, enc, callback) {
		if (file.isNull()) {
			this.push(file); // Do nothing if no contents
			return callback();
		}

		if (file.isStream()) {
			this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported for now.'));
			return callback();
		}

		if (file.isBuffer()) {
			var meta = file[options.property];

			var context = _.extend(
				_.omit(meta, ['nunjucks', 'template']),
				_.omit(file, ['contents','meta','_contents', 'isBuffer', 'isStream', 'isNull', 'isDirectory', 'clone', 'pipe', 'inspect', 'stat']),
				{
					relativeBase: file.base.substring(file.cwd.length),
					filename: file.path.substring(file.base.length),
					content: file.contents.toString()
				});

			var template = meta && meta.template || options.defaultTemplate;
			template = template + options.templateExtension;

			nj.render(template, context, function(error, result) {
				if (error) {
					this.emit(
						'error',
						new PluginError(PLUGIN_NAME, error));
				}

				file.contents = new Buffer(result);
				file.path = gutil.replaceExtension(file.path, '.html');

				this.push(file);
				return callback();
			}.bind(this));
		}
	});
};
<commit_msg>Handle missing plugin folder<commit_after>'use strict';

var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var _ = require('lodash');
var nunjucks = require('nunjucks');
var path = require('path');
var fs = require('fs');

// Constants
var PLUGIN_NAME = 'gulp-nunjucks';
var PLUGINS_DIR = 'plugins';

module.exports = function (options) {
	options = _.extend({
		templatePath: 'templates',
		templateExtension: '.html',
		defaultTemplate: 'base',
		nunjucksConfig: {
			autoescape: false
		},
		pluginDirectory: 'plugins',
		property: 'meta'
	}, options || {});

	// Configure Nunjucks
	var nj = new nunjucks.Environment(
		new nunjucks.FileSystemLoader(options.templatePath),
		options.nunjucksConfig);

	// Load plugins
	var pluginFiles;
	try {
		pluginFiles = fs.readdirSync(PLUGINS_DIR)
	} catch(err) {
		pluginFiles = [];
	}
	_.each(pluginFiles, function(file) {
		var plugin = file.match(/^(filter|tag)-(.*)(?:.js$)/);
		var type = plugin[1];
		var name = plugin[2];
		var fn = require(path.join(options.pluginDirectory, file));

		if (type === 'filter') {
			nj.addFilter(name, fn);
		}
		if (type === 'tag') {
			nj.addExtension(name, fn);
		}
	});

	return through.obj(function(file, enc, callback) {
		if (file.isNull()) {
			this.push(file); // Do nothing if no contents
			return callback();
		}

		if (file.isStream()) {
			this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported for now.'));
			return callback();
		}

		if (file.isBuffer()) {
			var meta = file[options.property];

			var context = _.extend(
				_.omit(meta, ['nunjucks', 'template']),
				_.omit(file, ['contents','meta','_contents', 'isBuffer', 'isStream', 'isNull', 'isDirectory', 'clone', 'pipe', 'inspect', 'stat']),
				{
					relativeBase: file.base.substring(file.cwd.length),
					filename: file.path.substring(file.base.length),
					content: file.contents.toString()
				});

			var template = meta && meta.template || options.defaultTemplate;
			template = template + options.templateExtension;

			nj.render(template, context, function(error, result) {
				if (error) {
					this.emit(
						'error',
						new PluginError(PLUGIN_NAME, error));
				}

				file.contents = new Buffer(result);
				file.path = gutil.replaceExtension(file.path, '.html');

				this.push(file);
				return callback();
			}.bind(this));
		}
	});
};
<|endoftext|>


<commit_before>if (typeof FRP == 'undefined')
    FRP = {};

(function(){
    this.test = "Test";
    this.parsers = [];
    this.parse = function(rawData){
        for(var index in FRP.Parser) {
            if (FRP.Parser.hasOwnProperty(index)) {
                if (FRP.Parser[index].valid(rawData))
                {
                    return FRP.Parser[index].parse(rawData);
                }
            }
        }
        return undefined;
    };

function ReportEntry(){
    this.date = new Date();
    this.name ="";
    this.debit = 0;
    this.credit = 0;
    this.total = 0;
    this.description = "";
}

function textToFloat(text) {
    var tempNum = text.split(',');
    tempNum = parseFloat(tempNum[0] + tempNum[1]);
    if (isNaN(tempNum)) tempNum =0;
    return  tempNum;
}

if (typeof FRP.Parser == 'undefined')
    FRP.Parser = {};

if (typeof FRP.Parser.OtzahrHahayal == 'undefined') {
    FRP.Parser.OtzahrHahayal = {
        id: "יתרה   	  תאריך ערך  	 זכות 	 חובה 	 תאור 	אסמכתא 	 סוג פעולה  	 תאריך",
        valid: function (rawData) {
            return (this.id.substring(1,20) === rawData.substring(1,20));
        },
        parse: function (rawData) {
            var report = [];
            rawReport = rawData.split('\n');
            for (var i=2; i<rawReport.length; i++)
            {
                var rawEntry =  rawReport[i].split('\t');
                if (rawEntry != undefined)
                if (rawEntry.length >= 7) {
                    var entry = new ReportEntry();
                    var MissingTotal = 0;
                    if (rawEntry.length == 8) {
                        entry.total = rawEntry[0];
                    }else{
                        MissingTotal = 1;
                    }
                    var credit = (rawEntry[2-MissingTotal]);
                    var debit = (rawEntry[3-MissingTotal]);
                    //rawEntry[1-MissingTotal]; //Value date - not used in our reports
                    entry.credit = textToFloat(credit);
                    entry.debit = textToFloat(debit);
                    entry.name = rawEntry[4-MissingTotal];
                    //rawEntry[5-MissingTotal]; //Reference - not used in our reports
                    //rawEntry[6-MissingTotal]; //Action type - not used in our reports
                    entry.date = rawEntry[7-MissingTotal];

                    entry.description = ""; //Bank report does not have detailed description

                    report.push(entry);
                }
            }
            return report;
        }
    };
}

function getText(html){
    return html.substring(html.lastIndexOf(">")+1);
}

if (typeof FRP.Parser.Isracard == 'undefined') {
    var isracardId = '<META CONTENT="text/html" HTTP-EQUIV="Content-Type" charset="iso-8859-8"></META><META CONTENT="no-cache" HTTP-EQUIV="Pragma"></META><META CONTENT="0" HTTP-EQUIV="expires"></META>';
    FRP.Parser.Isracard = {
        id: isracardId  ,
        valid: function (rawData) {
            if (this.id.substring(1,this.id.length) == rawData.substring(1,this.id.length))
                {
                return  true;
            }
            else {
                return false;
            }

        },
        parse: function (rawData) {
            var report = [];
            var rawReport = rawData.split("
");
            var firsttime = true;
            for (var i=4; i<rawReport.length; i++){
                var rawEntry = rawReport[i].split("	");
                switch (rawEntry.length)
                {
                    case 7:
                        //Israeli transactions
                        var entry = new ReportEntry();
                        entry.date = getText(rawEntry[0]);
                        if (entry.date == "") break; //This line is just informative and will not help in out report.
                        entry.name = getText(rawEntry[1]);
                        // rawEntry[2]; //Total amount of the deal
                        var tempMoney = textToFloat(getText(rawEntry[3]));
                        entry.debit = tempMoney > 0 ? tempMoney : 0;
                        entry.credit = tempMoney < 0 ? (tempMoney*(-1)) : 0;
                        // rawEntry[4]; //Transaction id
                        entry.description = getText(rawEntry[5]);
                        report.push(entry);
                        break;
                    case 10:
                        //International Transactions
                        if (firsttime) { firsttime=false;  break;} //skip international deal headers
                        var entry = new ReportEntry();
                        entry.date = getText(rawEntry[0]);
                        if (entry.date == "") break; //This line is just informative and will not help in out report.
                        // rawEntry[1]; //Date of buying
                        entry.name = getText(rawEntry[2]);
                        // rawEntry[3]; //City code
                        // rawEntry[4]; //Original currency
                        var tempMoney = textToFloat(getText(rawEntry[5]));
                        entry.debit = tempMoney > 0 ? tempMoney : 0;
                        entry.credit = tempMoney < 0 ? (tempMoney*(-1)) : 0;
                        // rawEntry[6]; //Currency to bill
                        // rawEntry[7]; //Total in the [6] currency
                        // rawEntry[8]; //Transaction id
                        entry.description = ""; //No description in international deals
                        report.push(entry);
                        break;
                    default:
                        break;
                }

                }
            return report;

        }
    }
}
}).call(FRP);<commit_msg>dates are now being saved a string containing Date object. Next step is to convert it to unix time string<commit_after>if (typeof FRP == 'undefined')
    FRP = {};

(function(){
    this.test = "Test";
    this.parsers = [];
    this.parse = function(rawData){
        for(var index in FRP.Parser) {
            if (FRP.Parser.hasOwnProperty(index)) {
                if (FRP.Parser[index].valid(rawData))
                {
                    return FRP.Parser[index].parse(rawData);
                }
            }
        }
        return undefined;
    };

function ReportEntry(){
    this.date = new Date();
    this.name ="";
    this.debit = 0;
    this.credit = 0;
    this.total = 0;
    this.description = "";
}

function textToFloat(text) {
    var tempNum = text.split(',');
    tempNum = parseFloat(tempNum[0] + tempNum[1]);
    if (isNaN(tempNum)) tempNum =0;
    return  tempNum;
}
function textToDate(text) {
    //Parse the format DD/MM/YYYY
    var da = text.split("/");
    if (da.length != 3) return undefined;
    return new Date(da[2],da[1]-1,da[0]);
}

if (typeof FRP.Parser == 'undefined')
    FRP.Parser = {};

if (typeof FRP.Parser.OtzahrHahayal == 'undefined') {
    FRP.Parser.OtzahrHahayal = {
        id: "יתרה   	  תאריך ערך  	 זכות 	 חובה 	 תאור 	אסמכתא 	 סוג פעולה  	 תאריך",
        valid: function (rawData) {
            return (this.id.substring(1,20) === rawData.substring(1,20));
        },
        parse: function (rawData) {
            var report = [];
            rawReport = rawData.split('\n');
            for (var i=2; i<rawReport.length; i++)
            {
                var rawEntry =  rawReport[i].split('\t');
                if (rawEntry != undefined)
                if (rawEntry.length >= 7) {
                    var entry = new ReportEntry();
                    var MissingTotal = 0;
                    if (rawEntry.length == 8) {
                        entry.total = rawEntry[0];
                    }else{
                        MissingTotal = 1;
                    }
                    var credit = (rawEntry[2-MissingTotal]);
                    var debit = (rawEntry[3-MissingTotal]);
                    //rawEntry[1-MissingTotal]; //Value date - not used in our reports
                    entry.credit = textToFloat(credit);
                    entry.debit = textToFloat(debit);
                    entry.name = rawEntry[4-MissingTotal];
                    //rawEntry[5-MissingTotal]; //Reference - not used in our reports
                    //rawEntry[6-MissingTotal]; //Action type - not used in our reports
                    entry.date = textToDate(rawEntry[7-MissingTotal]);

                    entry.description = ""; //Bank report does not have detailed description

                    report.push(entry);
                }
            }
            return report;
        }
    };
}

function getText(html){
    return html.substring(html.lastIndexOf(">")+1);
}

if (typeof FRP.Parser.Isracard == 'undefined') {
    var isracardId = '<META CONTENT="text/html" HTTP-EQUIV="Content-Type" charset="iso-8859-8"></META><META CONTENT="no-cache" HTTP-EQUIV="Pragma"></META><META CONTENT="0" HTTP-EQUIV="expires"></META>';
    FRP.Parser.Isracard = {
        id: isracardId  ,
        valid: function (rawData) {
            if (this.id.substring(1,this.id.length) == rawData.substring(1,this.id.length))
                {
                return  true;
            }
            else {
                return false;
            }

        },
        parse: function (rawData) {
            var report = [];
            var rawReport = rawData.split("
");
            var firsttime = true;
            for (var i=4; i<rawReport.length; i++){
                var rawEntry = rawReport[i].split("	");
                switch (rawEntry.length)
                {
                    case 7:
                        //Israeli transactions
                        var entry = new ReportEntry();
                        entry.date = textToDate(getText(rawEntry[0]));
                        if (entry.date == undefined) break; //This line is just informative and will not help in out report.
                        entry.name = getText(rawEntry[1]);
                        // rawEntry[2]; //Total amount of the deal
                        var tempMoney = textToFloat(getText(rawEntry[3]));
                        entry.debit = tempMoney > 0 ? tempMoney : 0;
                        entry.credit = tempMoney < 0 ? (tempMoney*(-1)) : 0;
                        // rawEntry[4]; //Transaction id
                        entry.description = getText(rawEntry[5]);
                        report.push(entry);
                        break;
                    case 10:
                        //International Transactions
                        if (firsttime) { firsttime=false;  break;} //skip international deal headers
                        var entry = new ReportEntry();
                        entry.date = textToDate(getText(rawEntry[0]));
                        if (entry.date == undefined) break; //This line is just informative and will not help in out report.
                        // rawEntry[1]; //Date of buying
                        entry.name = getText(rawEntry[2]);
                        // rawEntry[3]; //City code
                        // rawEntry[4]; //Original currency
                        var tempMoney = textToFloat(getText(rawEntry[5]));
                        entry.debit = tempMoney > 0 ? tempMoney : 0;
                        entry.credit = tempMoney < 0 ? (tempMoney*(-1)) : 0;
                        // rawEntry[6]; //Currency to bill
                        // rawEntry[7]; //Total in the [6] currency
                        // rawEntry[8]; //Transaction id
                        entry.description = ""; //No description in international deals
                        report.push(entry);
                        break;
                    default:
                        break;
                }

                }
            return report;

        }
    }
}
}).call(FRP);<|endoftext|>


<commit_before>'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.addColumn(
      'Users',
      'role_id',
      {
        type: Sequelize.INTEGER
      }
    );
  },

  down: function (queryInterface, Sequelize) {
    return queryInterface.removeColumn(
      'Users',
      'rold_id'
    );
  }
};
<commit_msg>updates migration for postgres compatibility<commit_after>'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.addColumn(
      'users',
      'role_id',
      {
        type: Sequelize.INTEGER
      }
    );
  },

  down: function (queryInterface, Sequelize) {
    return queryInterface.removeColumn(
      'users',
      'rold_id'
    );
  }
};
<|endoftext|>


<commit_before><commit_msg>:card_file_box: Update `jquery.Thailand.js`<commit_after><|endoftext|>