<commit_before>var basicScraper = require('./basicScraperController');
var getExternalUrl = require('./urlController').getExternalUrl;
var CronJob = require('cron').CronJob;
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 CronJob = require('cron').CronJob;

module.exports = {
  newCron: function(UserUrl) {
    console.log('we have a new cron!', UserUrl)
    var freq = '* * */' + UserUrl.frequency + '* * *';

    var job = new CronJob({
      cronTime: freq,
      onTick: function() {
                  console.log('checking', UserUrl.url, 'for changes');
        //            var oldImg = UserUrl.cropImage;
        //            var email = url[j].UserUrl.email;
        //            var website = url[j].url
        //            var params = {
        //             h: url[j].UserUrl.cropHeight,
        //             w: url[j].UserUrl.cropWidth,
        //             x: url[j].UserUrl.cropOriginX,
        //             y: url[j].UserUrl.cropOriginY
        //           }

        //           // get the server to render the page with params coordinates
        //           basicScraper.getScreenshot(url[j].url, url[j].id, function(img1, email) {
        //             basicScraper.cropImg(img1, params, true, function(newImg) {
        //               console.log('old image path', oldImg);
        //               console.log('new image path', newImg);


        //               compare(oldImg, newImg, function (equal){

        //                 if (!equal){

        //                   console.log('change detected on', website, 'sending email to ', email);

        //                   var mailOptions = {
        //                       from: "The Blank Arrays <<EMAIL>@<PASSWORD> <EMAIL>>", // sender address
        //                       // currently accessing only one user email
        //                       to: email, // list of receivers
        //                       subject: 'We found some tubular changes!', // Subject line
        //                       text: 'Hi there! It looks like we found a change on '+ website + '!', // plaintext body
        //                       html: "<span>The Scrapinit team found a change on " + website +"!</span>",
        //                           // html: '<b>Hello world </b>' // html body
        //                   };

        //                   // Send email function
        //                   transporter.sendMail(mailOptions, function(error, info){
        //                       if(error){
        //                           console.log(error);
        //                       }else{
        //                           console.log('Message sent: ' + info.response);
        //                       } //  else statemenet  
        //                   }); //  transporter.sendMail(mailOptions, function(error, info){
        //                 }; // if (!equal){
        //               }) // compare(img1, img2, function (equal){
        //             }); // basicScraper.cropImg(img1, params, true, function(img2) {
        // }, email); // basicScraper.getScreenshot()
      },
      start: false,
      timeZone: "America/Los_Angeles"
    });
    job.start();

  }
}

// for faster testing
// var schedule = '*/30 * * * * *';

//To run every 3 seconds do */3; every 5 min do * */5 *
// var schedule = '*/5 * * * * *';

<commit_msg>[Feat] addCron runs a cronjob when called<commit_after>var basicScraper = require('./basicScraperController');
var getExternalUrl = require('./urlController').getExternalUrl;
var CronJob = require('cron').CronJob;
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 CronJob = require('cron').CronJob;

module.exports = {
  addCron: function(UserUrl, url) {
    var userUrl = UserUrl;
    // minutes
    var freq = '* */' + UserUrl.frequency + ' * * * *';
    // seconds
    // var freq = '*/' + UserUrl.frequency + ' * * * * *';
    var job = new CronJob({
      cronTime: freq,
      onTick: function() {
        console.log('checking', url, 'for changes');
         var oldImg = UserUrl.cropImage;
         var email = UserUrl.email;
         var website = url
         var params = {
          h: UserUrl.cropHeight,
          w: UserUrl.cropWidth,
          x: UserUrl.cropOriginX,
          y: UserUrl.cropOriginY
        }

        // get the server to render the page with params coordinates
        basicScraper.getScreenshot(website, UserUrl.user_id, function(img1, email) {
          basicScraper.cropImg(img1, params, true, function(newImg) {
            console.log('old image path', oldImg);
            console.log('new image path', newImg);


            compare(oldImg, newImg, function (equal){

              if (!equal){

                console.log('change detected on', website, 'sending email to ', email);

                var mailOptions = {
                    from: "The Blank Arrays <<EMAIL>@<EMAIL>>", // sender address
                    // currently accessing only one user email
                    to: email, // list of receivers
                    subject: 'We found some tubular changes!', // Subject line
                    text: 'Hi there! It looks like we found a change on '+ website + '!', // plaintext body
                    html: "<span>The Scrapinit team found a change on " + website +"!</span>",
                        // html: '<b>Hello world </b>' // html body
                };

                // Send email function
                transporter.sendMail(mailOptions, function(error, info){
                    if(error){
                        console.log(error);
                    }else{
                        console.log('Message sent: ' + info.response);
                    } //  else statemenet  
                }); //  transporter.sendMail(mailOptions, function(error, info){
              }; // if (!equal){
            }) // compare(img1, img2, function (equal){
          }); // basicScraper.cropImg(img1, params, true, function(img2) {
        }, email); // basicScraper.getScreenshot()
      },
      start: false,
      timeZone: "America/Los_Angeles"
    });
    job.start(UserUrl);
  }
}


{'fraction_non_alphanumeric': 0.14037997587454765, 'fraction_numerical': 0.002864897466827503, 'mean_word_length': 1.546257197696737, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5648972', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1784, 'n_words': 449}