<commit_before>var basicScraper = require('./basicScraperController');
var getExternalUrl = require('./urlController').getExternalUrl;
var CronJob = require('cron').CronJob;
var CronJobManager = require('cron-job-manager');
var secret = require('../../config.js');
var db = require('../db.js');
var Sequelize = require('sequelize');
var compare = require('../imgCompare.js').compare;
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: 'mailgun',
    auth: secret.auth
});


var manager = new CronJobManager();

module.exports = {
  startAllCron: function() {
    console.log('starting all cronjobs');
    db.User.findAll()
    .then(function(allUsers) {
      for (var i = 0; i < allUsers.length; i++){
        allUsers[i].getUrls()
        .then(function(url) {
          for (var j=0; j<url.length; j++){
             var userUrl = url[j].UserUrl;
             var active = userUrl.status;
             var url = url[j].url;
             if (active) {
               console.log('watching ' + url + ' for ' + userUrl.email)
               module.exports.addCron(userUrl, url);
             }; // if (active)
          }; // for loop iterating over each url for a user
        }); // .then(function(url){
      }; // or (var i = 0; i < allUsers.length; i++){
    }); // .then(function(allUsers) {
  },

  addCron: function(UserUrl, url) {
    UserUrl.status = true;
    var userUrl = UserUrl;
    var key = UserUrl.url_id.toString() + UserUrl.user_id.toString();
    console.log('Starting cronJob', key);
    var action = UserUrl.compare || 'image';

    // hours
    // var freq = '* * */' + UserUrl.frequency + ' * * *';

    // minutes
    // var freq = '* */' + UserUrl.frequency + ' * * * *';

    var freq = '* * * 1 * *';

    // FOR TEST PURPOSES ONLY seconds
    // var freq = '*/' + UserUrl.frequency + ' * * * * *';

    if (manager.exists(key)) {
      manager.deleteJob(key);
    };
    manager.add(key, freq, function() {
      if (UserUrl.status) {
        console.log('checking', url, 'for', UserUrl.email);
         var oldImg = UserUrl.cropImage;
         var email = UserUrl.email;
         var params = {
          h: UserUrl.cropHeight,
          w: UserUrl.cropWidth,
          x: UserUrl.cropOriginX,
          y: UserUrl.cropOriginY
        };

        // TODO: 
        // check userUrl if comparing screenshot or ocr values?
        // if (UserUrl.) {
          // compareUtils.compareOCR(value, value, value);
        // }
        // compares screenshot, sends email if there is a difference in image

        compareUtils.compareScreenShot(UserUrl, url, email, params, oldImg);
      } else {
        manager.stop()
      }
    });
    manager.start(key);
  },

  startCron: function(user_id, url_id) {
    var key = url_id.toString() + user_id.toString();
    if (manager.exists(key)) {
      console.log('Starting cronjob', key);
      manager.start(key);
    } else {
      console.log('error, cronjob', key, ' does not exist');
    }
  },

  stopCron: function(user_id, url_id) {
    var key = url_id.toString() + user_id.toString();
    console.log('Stopping cronJob', key);
    manager.stop(key);
  },

  deleteCron: function(user_id, url_id) {
    var key = url_id.toString() + user_id.toString();
    console.log('Deleting cronJob', key);
    manager.deleteJob(key);
  },
};

var compareUtils = {
  compareOCR: function() {
    // TODO: take values that are input to it, pass it through compare ocr functions? Should be in basicScroperController?
  },

  compareScreenShot: function(UserUrl, website, email, params, oldImg) {
    // gets screenshot of website
    basicScraper.getScreenshot(website, UserUrl.user_id, function(img1, email) {
      // crops new image so we can compare the the old cropped image
      basicScraper.cropImg(img1, params, true, function(newImg) {
        // checks for difference in pictures
        console.log('img1 in cropImg', img1);
        compare(oldImg, newImg, function (equal, oldImg, newImg){
          if (!equal){
            // set status to false since we are stopping the cronjob
            UserUrl.status = false;
            // stop cronjob
            module.exports.stopCron(UserUrl.user_id, UserUrl.url_id)
            // if images are not equal, send an email
            compareUtils.sendEmail(website, email, oldImg, newImg);
          }; // if (!equal){
        }); // compare(img1, img2, function (equal){
      }); // basicScraper.cropImg(img1, params, true, function(img2) {
    }, email); // basicScraper.getScreenshot()
  }, // compareScreenShot: function(UserUrl, website, email, params, oldImg) {

  sendEmail: function(website, email, oldImg, newImg) {
    console.log('change detected on', website, 'sending email to ', email);
    // parameters for email<commit_msg>[Refactor] Broke down compareScreenShot into two functions: compareScreenShot and getNewCroppedImage for easier readability and more modularity<commit_after>var basicScraper = require('./basicScraperController');
var getExternalUrl = require('./urlController').getExternalUrl;
var compare = require('../imgCompare.js').compare;
var CronJob = require('cron').CronJob;
var CronJobManager = require('cron-job-manager');
var db = require('../db.js');
var nodemailer = require('nodemailer');
var ocr = require('./ocr');
var secret = require('../../config.js');
var Sequelize = require('sequelize');
var transporter = nodemailer.createTransport({
    service: 'mailgun',
    auth: secret.auth
});


var manager = new CronJobManager();

module.exports = {
  startAllCron: function() {
    console.log('starting all cronjobs');
    db.User.findAll()
    .then(function(allUsers) {
      for (var i = 0; i < allUsers.length; i++){
        allUsers[i].getUrls()
        .then(function(url) {
          for (var j=0; j<url.length; j++){
             var userUrl = url[j].UserUrl;
             var active = userUrl.status;
             var url = url[j].url;
             if (active) {
               console.log('watching ' + url + ' for ' + userUrl.email)
               module.exports.addCron(userUrl, url);
             }; // if (active)
          }; // for loop iterating over each url for a user
        }); // .then(function(url){
      }; // or (var i = 0; i < allUsers.length; i++){
    }); // .then(function(allUsers) {
  },

  addCron: function(UserUrl, url) {
    UserUrl.status = true;
    var userUrl = UserUrl;
    var key = UserUrl.url_id.toString() + UserUrl.user_id.toString();
    console.log('Starting cronJob', key);
    var action = UserUrl.compare || 'image';

    // hours
    // var freq = '* * */' + UserUrl.frequency + ' * * *';

    // minutes
    // var freq = '* */' + UserUrl.frequency + ' * * * *';

    // var freq = '* * * 1 * *';

    // FOR TEST PURPOSES ONLY seconds
    var freq = '*/' + UserUrl.frequency + ' * * * * *';

    if (manager.exists(key)) {
      manager.deleteJob(key);
    };
    manager.add(key, freq, function() {
      if (UserUrl.status) {
        console.log('checking', url, 'for', UserUrl.email);
         var oldImg = UserUrl.cropImage;
         var email = UserUrl.email;
         var params = {
          h: UserUrl.cropHeight,
          w: UserUrl.cropWidth,
          x: UserUrl.cropOriginX,
          y: UserUrl.cropOriginY
        };

        // TODO: 
        // check userUrl if comparing screenshot or ocr values?
        // if (UserUrl.) {
          // compareUtils.compareOCR(value, value, value);
        // }
        // compares screenshot, sends email if there is a difference in image
        console.log('the compare value is', UserUrl.compare);
        if (UserUrl.compare === 'text') {
          compareUtils.compareOCR();
        } else if (UserUrl.compare === 'image') {
          compareUtils.compareScreenShot(UserUrl, url, email, params, oldImg);
        } else {
          compareUtils.compareScreenShot(UserUrl, url, email, params, oldImg);
        }
      } else {
        manager.stop()
      }
    });
    manager.start(key);
  },

  startCron: function(user_id, url_id) {
    var key = url_id.toString() + user_id.toString();
    if (manager.exists(key)) {
      console.log('Starting cronjob', key);
      manager.start(key);
    } else {
      console.log('error, cronjob', key, ' does not exist');
    }
  },

  stopCron: function(user_id, url_id) {
    var key = url_id.toString() + user_id.toString();
    console.log('Stopping cronJob', key);
    manager.stop(key);
  },

  deleteCron: function(user_id, url_id) {
    var key = url_id.toString() + user_id.toString();
    console.log('Deleting cronJob', key);
    manager.deleteJob(key);
  },
};

var compareUtils = {
  compareOCR: function(UserUrl, website, email, params, oldImg) {
    // TODO: take values that are input to it, pass it through compare ocr functions? Should be in basicScroperController?
    compareUtils.getNewCroppedImage(UserUrl, website, email, params, oldImg, function(oldImg, newImg) {
        ocr.converImageToText(newImg, function(newImg) {
        }); // ocr.converImageToText(newImg, function(newImg) {
    }); //compareUtils.getNewCroppedImage(UserUrl, website, email, params, oldImg, function(oldImg, newImg) {

  },

  getNewCroppedImage: function(UserUrl, website, email, params, oldImg, cb) {
    basicScraper.getScreenshot(website, UserUrl.user_id, function(img1, email) {
      // crops new image so we can compare the the old cropped image
      basicScraper.cropImg(img1, params, true, function(newImg) {
        // checks for difference in pictures
        console.log('img1 in cropImg', img1);
        cb(oldImg, newImg)
      }); // basicScraper.cropImg(img1, params, true, function(img2) {
    }, email); // basicScraper.getScreenshot()
  },

  compareScreenShot: function(UserUrl, website, email, params, oldImg) {
    compareUtils.getNewCroppedImage(UserUrl, website, email, params, oldImg, function(oldImg, newImg) {
      // checks for difference in pictures
      compare(oldImg, newImg, function (equal, oldImg, newImg){
        if (!equal){
          // set status to false since we are stopping the cronjob
          UserUrl.status = false;
          // stop cronjob
          module.exports.stopCron(UserUrl.user_id, UserUrl.url_id)
          // if images are not equal, send an email
          compareUtils.sendEmail(website, email, oldImg, newImg);
        }; // if (!equal){
      }); // compare(img1, img2, function (equal){
    });
  }, // compareScreenShot: function(UserUrl, website, email, params, oldImg) {

  sendEmail: function(website, email, oldImg, newImg) {
    console.log('change detected on', website, 'sending email to ', email);
    // parameters for email
{'fraction_non_alphanumeric': 0.14357965720708063, 'fraction_numerical': 0.00224782242202866, 'mean_word_length': 2.6037799527505907, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3206905', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3272, 'n_words': 813}