Skip to content

Instantly share code, notes, and snippets.

@sturadnidge
Created March 13, 2015 11:07
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 sturadnidge/320ddb842c7da9dc7744 to your computer and use it in GitHub Desktop.
Save sturadnidge/320ddb842c7da9dc7744 to your computer and use it in GitHub Desktop.
Watch for discounts on Steam games!
'use strict';
// npm install cheerio colors moment optimist
// builtins
var http = require('http'),
// 3rd party
cheerio = require('cheerio'),
colors = require('colors/safe'),
moment = require('moment'),
argv = require('optimist')
.usage('\nWatch steam sale items.\n\nUsage: $0')
.demand(['g', 'i', 'p'])
.default({g: 730, i: 60, p: 50})
.describe({'b':'check a bundle, not an individual game',
'g':'a game / bundle id',
'i':'an interval in seconds',
'p':'a target discount percentage',
'q':'quiet - only alert when percentage drops below target'})
.argv;
// globals
var checkInterval = (parseInt(argv.i) * 1000),
condition = '';
//
// main
//
console.log('\nchecking item ' + colors.yellow(argv.g) + ' every ' + argv.i + ' seconds.\n');
checkItem(argv.g);
setInterval(function() { checkItem(argv.g) }, checkInterval);
//
// functions
//
function checkItem(itemId) {
var type = 'app';
if (argv.b) {
var type = 'sub';
}
var html = '',
options = {
headers: {
'user-agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'
},
hostname: 'store.steampowered.com',
path: '/' + type + '/' + itemId
};
var req = http.request(options, function (res) {
if (res.statusCode == '500' ) {
console.log('item does not exist');
return;
};
if (res.statusCode != '200') {
console.log('bad http response, is steam store down?');
return;
};
res.on('data', function (chunk) {
html += chunk;
}).on('end', function () {
// now that we've finished reading the page
var $ = cheerio.load(html),
itemName = '',
discountPercent = '';
if (argv.b) {
itemName = $('.pageheader').html(),
discountPercent = $('div.game_purchase_action:nth-child(4) > div:nth-child(1) > div:nth-child(1)').html();
} else {
itemName = $('.apphub_AppName').html(),
discountPercent = $('div.game_purchase_action:nth-child(5) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)').html();
}
if (!discountPercent) {
console.log(colors.red(itemName + ' is not currently on sale'));
} else {
if (Math.abs(parseInt(discountPercent)) > argv.p) {
console.log(colors.green(itemName + ' currently on sale at ' + discountPercent + ' retail!!!'));
} else {
if (!argv.q) {
console.log(colors.yellow(itemName) + ' currently on sale at ' + discountPercent + ' retail');
}
}
}
});
});
req.on('error', function (err) {
console.log('error: ' + err.message);
});
req.write('\n');
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment