Advertisement
cyniczny

forman.js

Feb 10th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nodemailer = require('nodemailer');
  2. const request = require('request');
  3. const cp = require('child_process');
  4.  
  5. const rick = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  6. const forman = 'http://www.cs.put.poznan.pl/pformanowicz/students.html';
  7.  
  8. function getLogString()
  9. {
  10.   var date = new Date();
  11.   return '[' + date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()+'] ';
  12. }
  13.  
  14. var transporter = nodemailer.createTransport({
  15.         host: 'smtp.gmail.com',
  16.         port: 587,
  17.         secure: false,
  18.         auth: {
  19.             user: 'username',
  20.             pass: 'password'
  21.         }
  22.     });
  23.  
  24.     var mailOptions = {
  25.         from: '"It\'s Piotr Formanowicz!" <michal.sw12@gmail.com>',
  26.         to: 'address',
  27.         subject: 'Wyniki egzaminu z matemayki dyskretnej',
  28.         html: `<a href="${forman}">${forman}</a>`
  29.     };
  30.  
  31.     function notify()
  32.     {
  33.       transporter.sendMail(mailOptions, (error, info) => {
  34.           if (error) {
  35.               return console.log(error);
  36.           }
  37.           console.log(getLogString()+`Mail sent to ${mailOptions.to}`);
  38.  
  39.       });
  40.     }
  41.  
  42.  
  43.     const options = {
  44.     url: forman,
  45.     method: 'HEAD'
  46.     };
  47.  
  48.     console.log(getLogString() + "forman.js started");
  49.  
  50.     var lastModified;
  51.     console.log(getLogString() + "Establishing connection...");
  52.     request(options, (err, res, body)=>{
  53.         if(err) {console.log(getLogString()+ 'Could not establish a connection, exiting...'); process.exit(1);};
  54.         lastModified = res.headers['last-modified'];
  55.         console.log(getLogString() +'Site last modified on ' + lastModified);
  56.     });
  57.  
  58.  
  59.  
  60.     var id = setInterval(()=>{
  61.           var tmp;
  62.           request(options, (err, res, body)=>{
  63.             if(err) return console.log(getLogString() + ' Connection lost, trying again...');
  64.               tmp = res.headers['last-modified'];
  65.  
  66.               if(lastModified != tmp)
  67.               {
  68.                 notify();
  69.                 cp.spawn('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', [rick, forman]);
  70.                 clearInterval(id);
  71.                 console.log(getLogString()+"Exiting...");
  72.               }
  73.           });
  74.  
  75.     }, 5000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement