Skip to content

Instantly share code, notes, and snippets.

@vdwijngaert
Created November 30, 2015 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdwijngaert/d0e8b922eee536b9116f to your computer and use it in GitHub Desktop.
Save vdwijngaert/d0e8b922eee536b9116f to your computer and use it in GitHub Desktop.
/*!
* 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