Created
November 30, 2015 16:27
-
-
Save vdwijngaert/d0e8b922eee536b9116f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/*! | |
* ProdChecker | |
* | |
* Copyright(c) Koen Van den Wijngaert | |
*/ | |
/*jshint node: true */ | |
'use strict'; | |
var http = require('http'); | |
var https = require('https'); | |
var qs = require('querystring'); | |
var url = require('url'); | |
var ProdChecker = function() { | |
this.PROD_URL = 'https://www.afuture.nl//productview.php?productID=4120438'; | |
this.PROD_NAME = 'Sharkoon SilentStorm SFX Gold 500W'; | |
this.CHECK_STRING = '>0 bij Afuture<'; | |
this.USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36'; | |
this.CHECK_INTERVAL = 30000; | |
this.DEVICE = ''; // device identifier | |
this.API_KEY = ''; // pushbullet api key | |
this.start(); | |
}; | |
ProdChecker.prototype = { | |
interval: 0, | |
start: function() { | |
var _this = this; | |
setInterval(function() { | |
_this.check(); | |
}, this.CHECK_INTERVAL); | |
this.check(); | |
}, | |
sendNotification: function() { | |
console.log('In stock!'); | |
var options = { | |
method: 'POST', | |
hostname: 'api.pushbullet.com', | |
port: null, | |
path: '/v2/pushes', | |
headers: { | |
'access-token': this.API_KEY, | |
'cache-control': 'no-cache', | |
'content-type': 'application/x-www-form-urlencoded', | |
}, | |
}; | |
var req = https.request(options, function(res) { | |
res.on('end', function() { | |
process.exit(0); | |
}); | |
}); | |
req.write( | |
qs.stringify( | |
{ | |
device_iden: this.DEVICE, | |
type: 'note', | |
title: 'Hey lekker beest', | |
body: 'Goed nieuws! De ' + this.PROD_NAME + ' is terug op voorraad!!1!', | |
} | |
) | |
); | |
req.end(); | |
clearInterval(this.interval); | |
}, check: function() { | |
var _this = this; | |
var parsed = url.parse(this.PROD_URL); | |
var options = { | |
host: parsed.host, | |
port: 80, | |
path: parsed.path, | |
headers: { | |
'User-Agent': this.USER_AGENT, | |
}, | |
}; | |
http.get(options, function(res) { | |
var content = ''; | |
res.on('data', function(chunk) { | |
content += chunk; | |
}); | |
res.on('end', function() { | |
if (content.indexOf(_this.CHECK_STRING) === -1) { | |
_this.sendNotification(); | |
} else { | |
console.log('Not in stock =('); | |
} | |
}); | |
}).on('error', function(e) { | |
console.log('Got error: ' + e.message); | |
}); | |
}, | |
}; | |
module.exports = new ProdChecker(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment