Last active
August 29, 2015 14:18
-
-
Save ivansabik/bc4ed7f2cc4ad118eb8d to your computer and use it in GitHub Desktop.
Script Node.js para checar si ya se publicó la convocatoria del premio CNSF 2015 y enviar mail de alerta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HTTP a http://www.cnsf.gob.mx/Eventos/Paginas/Premios_2015.aspx | |
// Si el header de respuesta es distinto de 200, ignorar | |
// Si es 200 enviar mail | |
var nodemailer = require('nodemailer'); | |
var smtpTransport = require('nodemailer-smtp-transport'); | |
var request = require('request'); | |
var URL = 'http://www.cnsf.gob.mx/Eventos/Paginas/Premios_2015.aspx'; | |
var USER = ''; | |
var PASS = ''; | |
var transporter = nodemailer.createTransport(smtpTransport({ | |
host: 'smtp.gmail.com', | |
port: 587, | |
auth: { | |
user: USER, | |
pass: PASS | |
} | |
})); | |
var message = { | |
from: USER, | |
to: USER, | |
subject: 'Convocatorioa premio CNSF 2015 publicada!', | |
html: 'Consulta en la <a href="' + URL + '">página</a>' | |
}; | |
// HTTP request a url and send mail if status is 200 | |
var tryNumber = 1; | |
setInterval(function() { | |
console.log('-------------------------'); | |
console.log(new Date().toString()); | |
console.log('Iniciando cnsf.js...'); | |
if(tryNumber > 10) process.exit(); | |
request(URL, function (err, res, body){ | |
console.log('Intento ' + tryNumber); | |
if(res) { | |
if(res.statusCode === 200) { | |
console.log('Enviando mail'); | |
transporter.sendMail(message, function(error, info) { | |
console.log('Enviado!'); | |
console.log('Respuesta "%s"', info.response); | |
}); | |
} else { | |
console.log(new Date().toString()); | |
console.log('Pagina ' + URL + ' responde "' + res.statusCode +'"'); | |
} | |
process.exit(); | |
} | |
if(err.code === 'ENOTFOUND') { | |
console.log(new Date().toString()); | |
console.log('No hay internets interwebs'); | |
console.log(err); | |
tryNumber++; | |
} | |
}); | |
}, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment