Created
November 27, 2012 08:31
-
-
Save jdpaton/4153131 to your computer and use it in GitHub Desktop.
Nexus 4 watcher ... email alerts version
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
var email = require('emailjs'); | |
var cheerio = require('cheerio'); | |
var request = require('request'); | |
var url8GB = 'https://play.google.com/store/devices/details?id=nexus_4_8gb'; | |
var url16GB = 'https://play.google.com/store/devices/details?id=nexus_4_16gb'; | |
var defaultState = 'UNKNOWN'; | |
var trackingState8GB = defaultState; | |
var trackingState16GB = defaultState; | |
var trackingInterval = 30000; | |
var soldOut = 'SOLD_OUT'; | |
var cannotMatchRule = 'NO_MATCH'; | |
var commErr = 'COMM_ERR'; | |
var tripBawlsOn = [soldOut, cannotMatchRule]; | |
var emailServer = email.server.connect({ | |
user: "[email protected]", | |
password: "spaceweed", | |
host: "smtp.gmail.com", | |
ssl: true | |
}); | |
function emailStateChanged(text, cb) { | |
emailServer.send({ | |
text: text, | |
from: "N4 Alerter <[email protected]>", | |
to: "your <[email protected]", | |
subject: "[N4 WATCHER] : State changed" | |
}, cb); | |
} | |
function findStatus($, cb) { | |
var csElem = $("span.hardware-price-description").first(); | |
if (csElem.length < 1) { | |
return cb(cannotMatchRule); | |
} else if (csElem.text().toLowerCase() == "sold out") { | |
return cb(soldOut); | |
} else { | |
return cb(cannotMatchRule); | |
} | |
} | |
function makeRequest(reqURL, cb) { | |
var state = defaultState; | |
request(reqURL, function(error, response, body) { | |
console.log('Loading ' + reqURL); | |
if (!error && response.statusCode == 200) { | |
$ = cheerio.load(body); | |
findStatus($, function(status) { | |
if (status == soldOut) { | |
state = status; | |
} | |
}); | |
} else { | |
state = commErr; | |
} | |
cb(state); | |
}); | |
} | |
function track() { | |
makeRequest(url8GB, function(currentStatus) { | |
if (trackingState8GB != currentStatus) { | |
emailStateChanged("ALERT: 8GB Watcher state has changed from " + trackingState8GB + " to " + currentStatus, function(err, message) { | |
console.log(err, message); | |
}); | |
} | |
trackingState8GB = currentStatus; | |
console.log('8GB Status: ' + currentStatus); | |
}); | |
makeRequest(url16GB, function(currentStatus) { | |
if (trackingState16GB != currentStatus) { | |
emailStateChanged("ALERT: 16GB Watcher state has changed from " + trackingState16GB + " to " + currentStatus, function(err, message) { | |
console.log(err, message); | |
}); | |
} | |
trackingState16GB = currentStatus; | |
console.log('16GB Status: ' + currentStatus); | |
}); | |
} | |
setInterval(function() { | |
track(); | |
}, 30000); | |
track(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace the email portions with your own SMTP server, specify whom to send to on line 41.
Install deps:
npm install emailjs cheerio request
Don't forget to setup an inbox rule to flag emails with subject
[N4 WATCHER]
as prio.Hopefully some of us will get a N4 before christmas.