Last active
January 1, 2016 16:02
-
-
Save sturadnidge/0587caa05d9a41ef7b42 to your computer and use it in GitHub Desktop.
Watches an item in the Steam market place and highlights it if price drops below desired value
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
'use strict'; | |
// npm install colors moment optimist | |
// builtins | |
var http = require('http'), | |
// 3rd party | |
colors = require('colors/safe'), | |
moment = require('moment'); | |
var currency = 'USD'; // can be USD, GBP or EUR | |
var argv = require('optimist') | |
.usage('\nWatch steam market items.\n\nUsage: $0') | |
.demand(['a', 'c', 'i', 'n', 'p', 'w']) | |
.default({a: 730, i: 60, n: 'Fire Serpent', 'c': 'BS', w: 'AK-47'}) | |
.describe({'a':'a steam application id', | |
'c':'a weapon condition (e.g. MW)', | |
'i':'an interval in seconds', | |
'n':'a skin name', | |
'p':'a price in ' + currency, | |
'q':'quiet - only alert when price drops below target', | |
's':'StatTrak\u2122', | |
't':'Souvenir ("tournament" quality)', | |
'u':'\u2605 ("unusual" quality - e.g. knives)', | |
'w':'a weapon name'}) | |
.argv; | |
// globals | |
var checkInterval = (parseInt(argv.i) * 1000), | |
condition = '', | |
currencyValue = ''; | |
// | |
// main | |
// | |
switch (currency) { | |
case 'USD': | |
currencyValue = '1'; | |
break; | |
case 'GBP': | |
currencyValue = '2'; | |
break; | |
case 'EUR': | |
currencyValue = '3'; | |
break; | |
default: | |
currencyValue = '1'; | |
} | |
switch (argv.c) { | |
case 'BS': | |
condition = '(Battle-Scarred)'; | |
break; | |
case 'FN': | |
condition = '(Factory New)'; | |
break; | |
case 'FT': | |
condition = '(Field-Tested)'; | |
break; | |
case 'MW': | |
condition = '(Minimal Wear)'; | |
break; | |
case 'WW': | |
condition = '(Well-Worn)'; | |
break; | |
default: | |
condition = '(Factory New)'; | |
break; | |
} | |
var s = argv.s ? 'StatTrak\u2122 ' : ''; | |
var t = argv.t ? 'Souvenir ' : ''; | |
var u = argv.u ? '\u2605 ' : ''; | |
var market_hash_name = t + u + s + argv.w + ' | ' + argv.n + ' ' + condition; | |
console.log('\nchecking item ' + colors.green(market_hash_name) + ' every ' + argv.i + ' seconds.\n'); | |
checkItem(market_hash_name); | |
setInterval(function() { checkItem(market_hash_name); }, checkInterval); | |
// | |
// functions | |
// | |
function checkItem(item) { | |
var qs = '?currency=' + currencyValue + '&appid=' + argv.a + '&market_hash_name=' + encodeURI(item); | |
var marketData = '', | |
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' | |
}, | |
host: 'steamcommunity.com', | |
path: '/market/priceoverview/' + qs | |
}; | |
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 market down?'); | |
return; | |
} | |
res.on('data', function (chunk) { | |
marketData += chunk; | |
}).on('end', function () { | |
var d = JSON.parse(marketData); | |
if (!d.lowest_price) { | |
console.log('no listing'); | |
} else { | |
var price = getPrice(d.lowest_price), | |
median = getPrice(d.median_price), | |
max = parseFloat(argv.p); | |
if ( price < max ) { | |
console.log(colors.green('\u2192 ' + price.toFixed(2)) + ' ' + moment().format('HH:mm:ss')); | |
} else { | |
if (!argv.q) { | |
console.log(price.toFixed(2) + ' (' + median.toFixed(2) + ')'); | |
} | |
} | |
} | |
}); | |
}); | |
req.on('error', function (err) { | |
console.log('error: ' + err.message); | |
}); | |
req.write('\n'); | |
req.end(); | |
} | |
function getPrice(s) { | |
if (currency === 'USD') { | |
// s is something like "$314.60" | |
return parseFloat(s.slice(1)); | |
} else { | |
// s is something like "$314.60" | |
return parseFloat(s.split(';')[1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixed getPrice()... different format for USD!