<commit_before>﻿var http = require('http');
var colors = require('colors');
var request = require('request');
var Mailgun = require('mailgun').Mailgun;

var config = require('./config.json');
var mg = new Mailgun(config.email_key);

module.exports = { //publick variables and metods of module
  
  info_email : function (subject, emailТext)
  {
    this.sendEmail(config.info_email, subject, emailТext);
  },

  sendEmail : function (recipient, subject, emailТext)
  {
    var standartFrom = '<EMAIL>'

    mg.sendText(standartFrom, recipient, subject, emailТext, standartFrom, {},
    function(err) {
      if (err) this.e(err);
        else this.i('Mail is SEND to '+recipient+' subject: '+subject);
    });
  },

  info_sms : function (text)
  {
    var isSend = '???';
    var isVariableCorrect = (typeof process.env.sms_key !== 'undefined'
      && typeof process.env.phone !== 'undefined');

    if (isVariableCorrect) {
      httpSmsRequest(process.env.sms_key, process.env.phone, text);
      isSend = ' Sms SEND:'.green;
    }
    else isSend = ' Sms NOT send:'.red;
    
    console.log(iTime() + isSend + '\n\t' + text);
  },

  i : function (str) //i = info
  {
    console.log(iTime()+' '+str);
  },

  d : function (str) //d = debug
  {
    console.log(dTime()+' '+str);
  },

  e : function(str) //e = error
  {
    console.log( (dTime()+' '+str).red );
  },

  showCity : function(req)
  {
    var testIP = '192.168.127.12';
    getLocation(testIP, function(location) { //getLocation(getClientIP(req));
      var result = location.city.name_en + ' ';
      console.log(location.city.name_en);
      return result;
    });
    
  }

}; //end of module

//Private metods of module
function httpSmsRequest(key, phone, text)
{
  var sendSmsUrl = 'http://sms.ru/sms/send?api_id=' + key +
    '&to=' + phone +
    '&text=' + text.replace(' ', '+');

  http.get(sendSmsUrl, function(res) {
    console.log('Response sms: ' + res);
  });
}

function getClientIP (req)
{
  with(req)
    return (headers['x-forwarded-for'] || '').split(',')[0]
      || connection.remoteAddress;
}

function  getLocation (ip, callback)
{
  request({
    uri: 'http://api.sypexgeo.net/json/'+ip,
    method: 'GET',
    encoding: 'utf-8'
    }, function (err, res, body) {
      
      if (err) {
        this.e(err);
        return "error";
      } else {
        //console.log(body);
        callback(JSON.parse(body));
      }
  });
}

function iTime()
{
  var d = new Date();
  var s = d.getDate()+'-'+(d.getMonth()+1)+'-'+d.getFullYear();
  var t = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
  return '['+s+' '+t+']';
}

function dTime()
{
  var d = new Date();
  var s = d.getDate()+'-'+(d.getMonth()+1)+'-'+d.getFullYear();
  var t = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()+':'+d.getMilliseconds();
  return '['+s+' '+t+']';
}<commit_msg>add err tp callback in geo request<commit_after>﻿var http = require('http');
var colors = require('colors');
var request = require('request');
var Mailgun = require('mailgun').Mailgun;

var config = require('./config.json');
var mg = new Mailgun(config.email_key);

module.exports = { //publick variables and metods of module
  
  info_email : function (subject, emailТext)
  {
    this.sendEmail(config.info_email, subject, emailТext);
  },

  sendEmail : function (recipient, subject, emailТext)
  {
    var standartFrom = '<EMAIL>'

    mg.sendText(standartFrom, recipient, subject, emailТext, standartFrom, {},
    function(err) {
      if (err) this.e(err);
        else this.i('Mail is SEND to '+recipient+' subject: '+subject);
    });
  },

  info_sms : function (text)
  {
    var isSend = '???';
    var isVariableCorrect = (typeof process.env.sms_key !== 'undefined'
      && typeof process.env.phone !== 'undefined');

    if (isVariableCorrect) {
      httpSmsRequest(process.env.sms_key, process.env.phone, text);
      isSend = ' Sms SEND:'.green;
    }
    else isSend = ' Sms NOT send:'.red;
    
    console.log(iTime() + isSend + '\n\t' + text);
  },

  showCity : function(req)
  {
    var testIP = '192.168.127.12';
    getLocation(testIP, function(err, location) { //getLocation(getClientIP(req));
      if (err) return "error";
      var result = location.city.name_en + ' ';
      console.log(location.city.name_en);
      return result;
    });
  },

  i : function (str) //i = info
  {
    console.log(iTime()+' '+str);
  },

  d : function (str) //d = debug
  {
    console.log(dTime()+' '+str);
  },

  e : function(str) //e = error
  {
    console.log( (dTime()+' '+str).red );
  }

}; //end of module

//Private metods of module
function  getLocation (ip, callback)
{
  request({
    uri: 'http://api.sypexgeo.net/json/'+ip,
    method: 'GET',
    encoding: 'utf-8'
    }, function (err, res, body) {
      
      if (err) {
        callback(err)
      } else {
        //console.log(body);
        callback(null, JSON.parse(body));
      }
  });
}

function httpSmsRequest(key, phone, text)
{
  var sendSmsUrl = 'http://sms.ru/sms/send?api_id=' + key +
    '&to=' + phone +
    '&text=' + text.replace(' ', '+');

  http.get(sendSmsUrl, function(res) {
    console.log('Response sms: ' + res);
  });
}

function getClientIP (req)
{
  with(req)
    return (headers['x-forwarded-for'] || '').split(',')[0]
      || connection.remoteAddress;
}

function iTime()
{
  var d = new Date();
  var s = d.getDate()+'-'+(d.getMonth()+1)+'-'+d.getFullYear();
  var t = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
  return '['+s+' '+t+']';
}

function dTime()
{
  var d = new Date();
  var s = d.getDate()+'-'+(d.getMonth()+1)+'-'+d.getFullYear();
  var t = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()+':'+d.getMilliseconds();
  return '['+s+' '+t+']';
}
{'fraction_non_alphanumeric': 0.19304115088658413, 'fraction_numerical': 0.005018400802944129, 'mean_word_length': 2.601807228915663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3036106', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2130, 'n_words': 426}