-
Notifications
You must be signed in to change notification settings - Fork 6
/
amazon.js
executable file
·34 lines (28 loc) · 896 Bytes
/
amazon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var req = require('request'),
exec = require('child_process').exec,
fs = require('fs'),
items = require('./items.json'),
ctr = 0;
function getItem() {
req.get(items[ctr].url, function(err, res, body) {
if (err) return false;
var avail = body.search(/Ships from and sold by .*Amazon\.com/) !== -1;
// only send a message on initial product availability
if (avail && !items[ctr].instock) {
exec('osascript imessage.scpt "' + items[ctr].name + ' in stock"');
}
items[ctr].instock = avail;
console.log('[%s] %s %s', new Date().toISOString(), items[ctr].name, avail ? 'in stock' : 'unavailable');
ctr++;
if (ctr < items.length) {
getItem();
} else {
fs.writeFile('items.json', JSON.stringify(items, null, ' '));
}
});
}
getItem();
setInterval(function() {
ctr = 0;
getItem();
}, 120000);