Skip to content

Instantly share code, notes, and snippets.

@jdpaton
Created November 27, 2012 08:31
Show Gist options
  • Save jdpaton/4153131 to your computer and use it in GitHub Desktop.
Save jdpaton/4153131 to your computer and use it in GitHub Desktop.
Nexus 4 watcher ... email alerts version
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: "trippin@bawls.com",
password: "spaceweed",
host: "smtp.gmail.com",
ssl: true
});
function emailStateChanged(text, cb) {
emailServer.send({
text: text,
from: "N4 Alerter <n4alert@holycow.com>",
to: "your <email@googlemail.com",
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();
@jdpaton
Copy link
Author

jdpaton commented Nov 27, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment