node-steamcommunity/classes/CMarketSearchResult.js

90 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-02-14 13:29:21 +08:00
const Cheerio = require('cheerio');
const SteamCommunity = require('../index.js');
2015-07-11 03:46:29 +08:00
SteamCommunity.prototype.marketSearch = function(options, callback) {
2021-07-29 14:55:56 +08:00
let qs = {};
2021-07-29 14:55:56 +08:00
if (typeof options === 'string') {
2015-07-11 03:46:29 +08:00
qs.query = options;
} else {
qs.query = options.query || '';
qs.appid = options.appid;
qs.search_descriptions = options.searchDescriptions ? 1 : 0;
2021-07-29 14:55:56 +08:00
if (qs.appid) {
for (let i in options) {
if (['query', 'appid', 'searchDescriptions'].indexOf(i) != -1) {
2015-07-11 03:46:29 +08:00
continue;
}
2015-07-11 03:46:29 +08:00
// This is a tag
qs['category_' + qs.appid + '_' + i + '[]'] = 'tag_' + options[i];
}
}
}
2015-07-11 03:46:29 +08:00
qs.start = 0;
qs.count = 100;
qs.sort_column = 'price';
qs.sort_dir = 'asc';
2015-07-11 03:46:29 +08:00
2021-07-29 14:55:56 +08:00
let results = [];
const performSearch = () => {
this.httpRequest({
uri: 'https://steamcommunity.com/market/search/render/',
qs: qs,
headers: {
referer: 'https://steamcommunity.com/market/search'
2016-06-28 04:56:16 +08:00
},
2021-07-29 14:55:56 +08:00
json: true
2021-07-29 15:20:14 +08:00
}, (err, response, body) => {
2016-06-28 04:56:16 +08:00
if (err) {
callback(err);
return;
}
2021-07-29 14:55:56 +08:00
if (!body.success) {
callback(new Error('Success is not true'));
2016-06-28 04:56:16 +08:00
return;
}
2021-07-29 14:55:56 +08:00
if (!body.results_html) {
callback(new Error('No results_html in response'));
2016-06-28 04:56:16 +08:00
return;
}
2021-07-29 14:55:56 +08:00
let $ = Cheerio.load(body.results_html);
let $errorMsg = $('.market_listing_table_message');
if ($errorMsg.length > 0) {
callback(new Error($errorMsg.text()));
2016-06-28 04:56:16 +08:00
return;
}
2021-07-29 14:55:56 +08:00
let rows = $('.market_listing_row_link');
for (let i = 0; i < rows.length; i++) {
2016-06-28 04:56:16 +08:00
results.push(new CMarketSearchResult($(rows[i])));
}
2021-07-29 14:55:56 +08:00
if (body.start + body.pagesize >= body.total_count) {
2016-06-28 04:56:16 +08:00
callback(null, results);
} else {
qs.start += body.pagesize;
performSearch();
}
2021-07-29 14:55:56 +08:00
}, 'steamcommunity');
};
performSearch();
2016-06-28 04:56:16 +08:00
};
2015-07-11 03:46:29 +08:00
function CMarketSearchResult(row) {
2021-07-29 14:55:56 +08:00
let match = row.attr('href').match(/\/market\/listings\/(\d+)\/([^?/]+)/);
2015-07-11 03:46:29 +08:00
this.appid = parseInt(match[1], 10);
this.market_hash_name = decodeURIComponent(match[2]);
2021-07-29 14:55:56 +08:00
this.image = ((row.find('.market_listing_item_img').attr('src') || '').match(/^https?:\/\/[^/]+\/economy\/image\/[^/]+\//) || [])[0];
this.price = parseInt(row.find('.market_listing_their_price .market_table_value span.normal_price').text().replace(/[^\d]+/g, ''), 10);
2015-07-11 03:46:29 +08:00
this.quantity = parseInt(row.find('.market_listing_num_listings_qty').text().replace(/[^\d]+/g, ''), 10);
}