Merge pull request #54 from Mikxail/bugfix-market-item-price

bugfix market-item price
This commit is contained in:
Alexander Corn 2016-02-29 16:44:17 -05:00
commit 7792969cae

View File

@ -1,7 +1,11 @@
var SteamCommunity = require('../index.js');
var Cheerio = require('cheerio');
SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
SteamCommunity.prototype.getMarketItem = function(appid, hashName, currency, callback) {
if (typeof currency == "function") {
callback = currency;
currency = 1;
}
var self = this;
this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
@ -13,23 +17,21 @@ SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
callback(new Error("There are no listings for this item."));
return;
}
var item = new CMarketItem(self, body, $);
if(item.commodity) {
item.updatePrice(function(err) {
if(err) {
callback(err);
} else {
callback(null, item);
}
});
} else {
callback(null, item);
}
var item = new CMarketItem(appid, hashName, self, body, $);
item.updatePrice(currency, function(err) {
if(err) {
callback(err);
} else {
callback(null, item);
}
});
});
};
function CMarketItem(community, body, $) {
function CMarketItem(appid, hashName, community, body, $) {
this._appid = appid;
this._hashName = hashName;
this._community = community;
this._$ = $;
@ -38,6 +40,12 @@ function CMarketItem(community, body, $) {
if(match) {
this._country = match[1];
}
this._language = "english";
match = body.match(/var g_strLanguage = "([^"]+)";/);
if(match) {
this._language = match[1];
}
this.commodity = false;
var match = body.match(/Market_LoadOrderSpread\(\s*(\d+)\s*\);/);
@ -62,34 +70,41 @@ function CMarketItem(community, body, $) {
// ignore
}
}
this.quantity = 0;
this.lowestPrice = 0;
if(!this.commodity) {
var total = $('#searchResults_total');
if(total) {
this.quantity = parseInt(total.text().replace(/[^\d]/g, '').trim(), 10);
}
var lowest = $('.market_listing_price.market_listing_price_with_fee');
if(lowest[0]) {
this.lowestPrice = parseInt($(lowest[0]).text().replace(/[^\d]/g, '').trim(), 10);
this.firstAsset = null;
this.assets = null;
match = body.match(/var g_rgAssets = (.*);/);
if (match) {
try {
this.assets = JSON.parse(match[1]);
this.assets = this.assets['730']['2'];
this.firstAsset = this.assets[Object.keys(this.assets)[0]];
} catch (e) {
// ignore
}
}
this.quantity = 0;
this.lowestPrice = 0;
// TODO: Buying listings and placing buy orders
}
CMarketItem.prototype.updatePrice = function(callback) {
CMarketItem.prototype.updatePrice = function (currency, callback) {
if (this.commodity) {
this.updatePriceForCommodity(currency, callback);
} else {
this.updatePriceForNonCommodity(currency, callback);
}
};
CMarketItem.prototype.updatePriceForCommodity = function(currency, callback) {
if(!this.commodity) {
throw new Error("Cannot update price for non-commodity item");
}
// TODO: Currency option maybe?
var self = this;
this._community.request({
"uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english&currency=1&item_nameid=" + this.commodityID,
"uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english&currency=" + currency + "&item_nameid=" + this.commodityID,
"json": true,
}, function(err, response, body) {
if(self._community._checkHttpError(err, response, callback)) {
@ -124,3 +139,47 @@ CMarketItem.prototype.updatePrice = function(callback) {
}
});
};
CMarketItem.prototype.updatePriceForNonCommodity = function (currency, callback) {
if(this.commodity) {
throw new Error("Cannot update price for commodity item");
}
var self = this;
this._community.request({
"uri": "https://steamcommunity.com/market/listings/" +
this._appid + "/" +
encodeURIComponent(this._hashName) +
"/render/?query=&start=0&count=10&country=US&language=english&currency=" + currency,
"json": true
}, function(err, response, body) {
if (self._community._checkHttpError(err, response, callback)) {
return;
}
if (body.success != 1) {
callback && callback(new Error("Error " + body.success));
return;
}
var match = body.total_count;
if (match) {
self.quantity = parseInt(match, 10);
}
var lowestPrice;
var $ = Cheerio.load(body.results_html);
match = $(".market_listing_price.market_listing_price_with_fee");
if (match) {
for (var i = 0; i < match.length; i++) {
lowestPrice = parseFloat($(match[i]).text().replace(",", ".").replace(/[^\d.]/g, ''));
if (!isNaN(lowestPrice)) {
self.lowestPrice = lowestPrice;
break;
}
}
}
callback && callback(null);
});
};