bugfix market-item price

and add currency option for .getMarketItem(appid, hashName, currency, cb)
This commit is contained in:
Mikhail Konovalov
2015-12-27 16:51:11 +03:00
parent 3bfe8c3713
commit 5c2894a7d4

View File

@@ -1,7 +1,12 @@
var SteamCommunity = require('../index.js'); var SteamCommunity = require('../index.js');
var Cheerio = require('cheerio'); var Cheerio = require('cheerio');
SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) { SteamCommunity.prototype.getMarketItem = function(appid, hashName, currency, callback) {
// steamCommunity.getMarketItem(appID, hashName, function(){...});
if (typeof currency == "function") {
callback = currency;
currency = 1;
}
var self = this; var self = this;
this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) { this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
if(self._checkHttpError(err, response, callback)) { if(self._checkHttpError(err, response, callback)) {
@@ -13,23 +18,21 @@ SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
callback(new Error("There are no listings for this item.")); callback(new Error("There are no listings for this item."));
return; return;
} }
var item = new CMarketItem(self, body, $); var item = new CMarketItem(appid, hashName, self, body, $);
if(item.commodity) { item.updatePrice({currency: currency}, function(err) {
item.updatePrice(function(err) { if(err) {
if(err) { callback(err);
callback(err); } else {
} else { callback(null, item);
callback(null, item); }
} });
});
} else {
callback(null, item);
}
}); });
}; };
function CMarketItem(community, body, $) { function CMarketItem(appid, hashName, community, body, $) {
this._appid = appid;
this._hashName = hashName;
this._community = community; this._community = community;
this._$ = $; this._$ = $;
@@ -38,6 +41,12 @@ function CMarketItem(community, body, $) {
if(match) { if(match) {
this._country = match[1]; this._country = match[1];
} }
this._language = "english";
match = body.match(/var g_strLanguage = "([^"]+)";/);
if(match) {
this._language = match[1];
}
this.commodity = false; this.commodity = false;
var match = body.match(/Market_LoadOrderSpread\(\s*(\d+)\s*\);/); var match = body.match(/Market_LoadOrderSpread\(\s*(\d+)\s*\);/);
@@ -62,7 +71,20 @@ function CMarketItem(community, body, $) {
// ignore // ignore
} }
} }
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.quantity = 0;
this.lowestPrice = 0; this.lowestPrice = 0;
@@ -81,15 +103,28 @@ function CMarketItem(community, body, $) {
// TODO: Buying listings and placing buy orders // TODO: Buying listings and placing buy orders
} }
CMarketItem.prototype.updatePrice = function(callback) { CMarketItem.prototype.updatePrice = function (options, callback) {
options = options || {};
options.currency = options.currency || 1;
if (this.commodity) {
this.updatePriceForCommodity(options, callback);
} else {
this.updatePriceForNonCommodity(options, callback);
}
};
CMarketItem.prototype.updatePriceForCommodity = function(options, callback) {
if(!this.commodity) { if(!this.commodity) {
throw new Error("Cannot update price for non-commodity item"); throw new Error("Cannot update price for non-commodity item");
} }
options = options || {};
var currency = options.currency;
// TODO: Currency option maybe?
var self = this; var self = this;
this._community.request({ 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, "json": true,
}, function(err, response, body) { }, function(err, response, body) {
if(self._community._checkHttpError(err, response, callback)) { if(self._community._checkHttpError(err, response, callback)) {
@@ -124,3 +159,50 @@ CMarketItem.prototype.updatePrice = function(callback) {
} }
}); });
}; };
CMarketItem.prototype.updatePriceForNonCommodity = function (options, callback) {
if(this.commodity) {
throw new Error("Cannot update price for commodity item");
}
options = options || {};
var currency = options.currency;
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 * 100;
break;
}
}
}
callback && callback(null);
});
};