From 5c2894a7d43bd7d7d1cf46b1fa2ab03e56303bf6 Mon Sep 17 00:00:00 2001 From: Mikhail Konovalov Date: Sun, 27 Dec 2015 16:51:11 +0300 Subject: [PATCH 1/3] bugfix market-item price and add currency option for .getMarketItem(appid, hashName, currency, cb) --- classes/CMarketItem.js | 120 ++++++++++++++++++++++++++++++++++------- 1 file changed, 101 insertions(+), 19 deletions(-) diff --git a/classes/CMarketItem.js b/classes/CMarketItem.js index cd14194..52b595b 100644 --- a/classes/CMarketItem.js +++ b/classes/CMarketItem.js @@ -1,7 +1,12 @@ var SteamCommunity = require('../index.js'); 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; this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) { 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.")); 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: 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 +41,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,7 +71,20 @@ function CMarketItem(community, body, $) { // 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.lowestPrice = 0; @@ -81,15 +103,28 @@ function CMarketItem(community, body, $) { // 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) { throw new Error("Cannot update price for non-commodity item"); } + + options = options || {}; + var currency = options.currency; - // TODO: Currency option maybe? var self = this; this._community.request({ - "uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=1&item_nameid=" + this.commodityID, + "uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=" + currency + "&item_nameid=" + this.commodityID, "json": true, }, function(err, response, body) { 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¤cy=" + 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); + }); +}; From eae78aab933576cfcd4fa2604a64d3540005ccf6 Mon Sep 17 00:00:00 2001 From: Mikhail Konovalov Date: Mon, 28 Dec 2015 15:58:31 +0300 Subject: [PATCH 2/3] remove options argument for updatePrice method --- classes/CMarketItem.js | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/classes/CMarketItem.js b/classes/CMarketItem.js index 52b595b..76b88d1 100644 --- a/classes/CMarketItem.js +++ b/classes/CMarketItem.js @@ -2,7 +2,6 @@ var SteamCommunity = require('../index.js'); var Cheerio = require('cheerio'); SteamCommunity.prototype.getMarketItem = function(appid, hashName, currency, callback) { - // steamCommunity.getMarketItem(appID, hashName, function(){...}); if (typeof currency == "function") { callback = currency; currency = 1; @@ -20,7 +19,7 @@ SteamCommunity.prototype.getMarketItem = function(appid, hashName, currency, cal } var item = new CMarketItem(appid, hashName, self, body, $); - item.updatePrice({currency: currency}, function(err) { + item.updatePrice(currency, function(err) { if(err) { callback(err); } else { @@ -103,25 +102,19 @@ function CMarketItem(appid, hashName, community, body, $) { // TODO: Buying listings and placing buy orders } -CMarketItem.prototype.updatePrice = function (options, callback) { - options = options || {}; - options.currency = options.currency || 1; - +CMarketItem.prototype.updatePrice = function (currency, callback) { if (this.commodity) { - this.updatePriceForCommodity(options, callback); + this.updatePriceForCommodity(currency, callback); } else { - this.updatePriceForNonCommodity(options, callback); + this.updatePriceForNonCommodity(currency, callback); } }; -CMarketItem.prototype.updatePriceForCommodity = function(options, callback) { +CMarketItem.prototype.updatePriceForCommodity = function(currency, callback) { if(!this.commodity) { throw new Error("Cannot update price for non-commodity item"); } - options = options || {}; - var currency = options.currency; - var self = this; this._community.request({ "uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=" + currency + "&item_nameid=" + this.commodityID, @@ -160,14 +153,11 @@ CMarketItem.prototype.updatePriceForCommodity = function(options, callback) { }); }; -CMarketItem.prototype.updatePriceForNonCommodity = function (options, callback) { +CMarketItem.prototype.updatePriceForNonCommodity = function (currency, 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/" + From 26f27e2698045796dd0b7706da0bdef5e59fd52c Mon Sep 17 00:00:00 2001 From: mikxail Date: Tue, 5 Jan 2016 21:58:49 +0300 Subject: [PATCH 3/3] remove unused price calculating block and fix lowestPrice value --- classes/CMarketItem.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/classes/CMarketItem.js b/classes/CMarketItem.js index 76b88d1..edf7e26 100644 --- a/classes/CMarketItem.js +++ b/classes/CMarketItem.js @@ -86,19 +86,6 @@ function CMarketItem(appid, hashName, community, body, $) { 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); - } - } - // TODO: Buying listings and placing buy orders } @@ -187,7 +174,7 @@ CMarketItem.prototype.updatePriceForNonCommodity = function (currency, callback) for (var i = 0; i < match.length; i++) { lowestPrice = parseFloat($(match[i]).text().replace(",", ".").replace(/[^\d.]/g, '')); if (!isNaN(lowestPrice)) { - self.lowestPrice = lowestPrice * 100; + self.lowestPrice = lowestPrice; break; } }