Fixed marketSearch (fixes #112)

This commit is contained in:
Alexander Corn 2016-06-27 16:56:16 -04:00
parent 17e5b94782
commit 60e0e63a92

View File

@ -27,53 +27,55 @@ SteamCommunity.prototype.marketSearch = function(options, callback) {
qs.count = 100; qs.count = 100;
qs.sort_column = 'price'; qs.sort_column = 'price';
qs.sort_dir = 'asc'; qs.sort_dir = 'asc';
performSearch.call(this, this.httpRequest, qs, [], callback);
};
function performSearch(request, qs, results, callback) {
var self = this; var self = this;
request({ var results = [];
"uri": "https://steamcommunity.com/market/search/render/", performSearch();
"qs": qs,
"headers": { function performSearch() {
"referer": "https://steamcommunity.com/market/search" self.httpRequest({
}, "uri": "https://steamcommunity.com/market/search/render/",
"json": true "qs": qs,
}, function(err, response, body) { "headers": {
if (err) { "referer": "https://steamcommunity.com/market/search"
callback(err); },
return; "json": true
} }, function(err, response, body) {
if (err) {
if(!body.success) { callback(err);
callback(new Error("Success is not true")); return;
return; }
}
if(!body.success) {
if(!body.results_html) { callback(new Error("Success is not true"));
callback(new Error("No results_html in response")); return;
return; }
}
if(!body.results_html) {
var $ = Cheerio.load(body.results_html); callback(new Error("No results_html in response"));
if($('.market_listing_table_message').length > 0) { return;
callback(new Error($('.market_listing_table_message').text())); }
return;
} var $ = Cheerio.load(body.results_html);
if($('.market_listing_table_message').length > 0) {
var rows = $('.market_listing_row_link'); callback(new Error($('.market_listing_table_message').text()));
for(var i = 0; i < rows.length; i++) { return;
results.push(new CMarketSearchResult($(rows[i]))); }
}
var rows = $('.market_listing_row_link');
if(body.start + body.pagesize >= body.total_count) { for(var i = 0; i < rows.length; i++) {
callback(null, results); results.push(new CMarketSearchResult($(rows[i])));
} else { }
qs.start += body.pagesize;
performSearch.call(self, request, qs, results, callback); if(body.start + body.pagesize >= body.total_count) {
} callback(null, results);
}, "steamcommunity"); } else {
} qs.start += body.pagesize;
performSearch();
}
}, "steamcommunity");
}
};
function CMarketSearchResult(row) { function CMarketSearchResult(row) {
var match = row.attr('href').match(/\/market\/listings\/(\d+)\/(.+)/); var match = row.attr('href').match(/\/market\/listings\/(\d+)\/(.+)/);