mirror of
https://github.com/DoctorMcKay/node-steamcommunity.git
synced 2026-09-01 04:28:02 +08:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b6bed5527 | ||
|
|
6def665b64 | ||
|
|
4f618c0a27 | ||
|
|
19fe951d96 | ||
|
|
a348112ccb | ||
|
|
dff2727f85 | ||
|
|
1fd12dd4d0 | ||
|
|
31093da1a8 |
131
classes/CMarketItem.js
Normal file
131
classes/CMarketItem.js
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
var SteamCommunity = require('../index.js');
|
||||||
|
var Cheerio = require('cheerio');
|
||||||
|
|
||||||
|
SteamCommunity.prototype.getMarketItem = function(appid, hashName, callback) {
|
||||||
|
var self = this;
|
||||||
|
this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
|
||||||
|
if(err || response.statusCode != 200) {
|
||||||
|
callback(err ? err.message : "HTTP error " + response.statusCode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var $ = Cheerio.load(body);
|
||||||
|
if($('.market_listing_table_message') && $('.market_listing_table_message').text().trim() == 'There are no listings for this item.') {
|
||||||
|
callback('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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function CMarketItem(community, body, $) {
|
||||||
|
this._community = community;
|
||||||
|
this._$ = $;
|
||||||
|
|
||||||
|
this._country = "US";
|
||||||
|
var match = body.match(/var g_strCountryCode = "([^"]+)";/);
|
||||||
|
if(match) {
|
||||||
|
this._country = match[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
this.commodity = false;
|
||||||
|
var match = body.match(/Market_LoadOrderSpread\(\s*(\d+)\s*\);/);
|
||||||
|
if(match) {
|
||||||
|
this.commodity = true;
|
||||||
|
this.commodityID = parseInt(match[1], 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.medianSalePrices = null;
|
||||||
|
match = body.match(/var line1=([^;]+);/);
|
||||||
|
if(match) {
|
||||||
|
try {
|
||||||
|
this.medianSalePrices = JSON.parse(match[1]);
|
||||||
|
this.medianSalePrices = this.medianSalePrices.map(function(item) {
|
||||||
|
return {
|
||||||
|
"hour": new Date(item[0]),
|
||||||
|
"price": item[1],
|
||||||
|
"quantity": parseInt(item[2], 10)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
} catch(e) {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Buying listings and placing buy orders
|
||||||
|
}
|
||||||
|
|
||||||
|
CMarketItem.prototype.updatePrice = function(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¤cy=1&item_nameid=" + this.commodityID,
|
||||||
|
"json": true,
|
||||||
|
}, function(err, response, body) {
|
||||||
|
if(err || response.statusCode != 200) {
|
||||||
|
if(callback) {
|
||||||
|
callback(err ? err.message : "HTTP error " + response.statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(body.success != 1) {
|
||||||
|
if(callback) {
|
||||||
|
callback("Error " + body.success);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var match = (body.sell_order_summary || '').match(/<span class="market_commodity_orders_header_promote">(\d+)<\/span>/);
|
||||||
|
if(match) {
|
||||||
|
self.quantity = parseInt(match[1], 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.buyQuantity = 0;
|
||||||
|
match = (body.buy_order_summary || '').match(/<span class="market_commodity_orders_header_promote">(\d+)<\/span>/);
|
||||||
|
if(match) {
|
||||||
|
self.buyQuantity = parseInt(match[1], 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.lowestPrice = parseInt(body.lowest_sell_order, 10);
|
||||||
|
self.highestBuyOrder = parseInt(body.highest_buy_order, 10);
|
||||||
|
|
||||||
|
// TODO: The tables?
|
||||||
|
if(callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
83
classes/CMarketSearchResult.js
Normal file
83
classes/CMarketSearchResult.js
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
var SteamCommunity = require('../index.js');
|
||||||
|
var Cheerio = require('cheerio');
|
||||||
|
|
||||||
|
SteamCommunity.prototype.marketSearch = function(options, callback) {
|
||||||
|
var qs = {};
|
||||||
|
|
||||||
|
if(typeof options === 'string') {
|
||||||
|
qs.query = options;
|
||||||
|
} else {
|
||||||
|
qs.query = options.query || '';
|
||||||
|
qs.appid = options.appid;
|
||||||
|
qs.search_descriptions = options.searchDescriptions ? 1 : 0;
|
||||||
|
|
||||||
|
if(qs.appid) {
|
||||||
|
for(var i in options) {
|
||||||
|
if(['query', 'appid', 'searchDescriptions'].indexOf(i) != -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a tag
|
||||||
|
qs['category_' + qs.appid + '_' + i + '[]'] = 'tag_' + options[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qs.start = 0;
|
||||||
|
qs.count = 100;
|
||||||
|
performSearch(this.request, qs, [], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
function performSearch(request, qs, results, callback) {
|
||||||
|
request({
|
||||||
|
"uri": "https://steamcommunity.com/market/search/render/",
|
||||||
|
"qs": qs,
|
||||||
|
"headers": {
|
||||||
|
"referer": "https://steamcommunity.com/market/search"
|
||||||
|
},
|
||||||
|
"json": true
|
||||||
|
}, function(err, response, body) {
|
||||||
|
if(err || response.statusCode != 200) {
|
||||||
|
callback(err ? err.message : "HTTP error " + response.statusCode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!body.success) {
|
||||||
|
callback("Success is not true");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!body.results_html) {
|
||||||
|
callback("No results_html in response");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var $ = Cheerio.load(body.results_html);
|
||||||
|
if($('.market_listing_table_message').length > 0) {
|
||||||
|
callback($('.market_listing_table_message').text());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows = $('.market_listing_row_link');
|
||||||
|
for(var i = 0; i < rows.length; i++) {
|
||||||
|
results.push(new CMarketSearchResult($(rows[i])));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(body.start + body.pagesize >= body.total_count) {
|
||||||
|
callback(null, results);
|
||||||
|
} else {
|
||||||
|
qs.start += body.pagesize;
|
||||||
|
performSearch(request, qs, results, callback);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function CMarketSearchResult(row) {
|
||||||
|
var match = row.attr('href').match(/\/market\/listings\/(\d+)\/(.+)/);
|
||||||
|
|
||||||
|
this.appid = parseInt(match[1], 10);
|
||||||
|
this.market_hash_name = decodeURIComponent(match[2]);
|
||||||
|
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').text().replace(/[^\d]+/g, ''), 10);
|
||||||
|
this.quantity = parseInt(row.find('.market_listing_num_listings_qty').text().replace(/[^\d]+/g, ''), 10);
|
||||||
|
}
|
||||||
@@ -28,12 +28,13 @@ SteamCommunity.PersonaStateFlag = {
|
|||||||
"OnlineUsingBigPicture": 1024
|
"OnlineUsingBigPicture": 1024
|
||||||
};
|
};
|
||||||
|
|
||||||
SteamCommunity.prototype.chatLogon = function(interval) {
|
SteamCommunity.prototype.chatLogon = function(interval, uiMode) {
|
||||||
if(this.chatState == SteamCommunity.ChatState.LoggingOn || this.chatState == SteamCommunity.ChatState.LoggedOn) {
|
if(this.chatState == SteamCommunity.ChatState.LoggingOn || this.chatState == SteamCommunity.ChatState.LoggedOn) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
interval = interval || 500;
|
interval = interval || 500;
|
||||||
|
uiMode = uiMode || "web";
|
||||||
|
|
||||||
this.emit('debug', 'Requesting chat WebAPI token');
|
this.emit('debug', 'Requesting chat WebAPI token');
|
||||||
this.chatState = SteamCommunity.ChatState.LoggingOn;
|
this.chatState = SteamCommunity.ChatState.LoggingOn;
|
||||||
@@ -58,7 +59,7 @@ SteamCommunity.prototype.chatLogon = function(interval) {
|
|||||||
self.request.post({
|
self.request.post({
|
||||||
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1",
|
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1",
|
||||||
"form": {
|
"form": {
|
||||||
"ui_mode": "web",
|
"ui_mode": uiMode,
|
||||||
"access_token": match[0]
|
"access_token": match[0]
|
||||||
},
|
},
|
||||||
"json": true
|
"json": true
|
||||||
|
|||||||
2
index.js
2
index.js
@@ -260,6 +260,8 @@ SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
require('./classes/CMarketItem.js');
|
||||||
|
require('./classes/CMarketSearchResult.js');
|
||||||
require('./classes/CSteamGroup.js');
|
require('./classes/CSteamGroup.js');
|
||||||
require('./classes/CSteamUser.js');
|
require('./classes/CSteamUser.js');
|
||||||
require('./components/chat.js');
|
require('./components/chat.js');
|
||||||
|
|||||||
11
package.json
11
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "steamcommunity",
|
"name": "steamcommunity",
|
||||||
"version": "2.2.1",
|
"version": "2.5.0",
|
||||||
"description": "Provides an interface for logging into and interacting with the Steam Community website",
|
"description": "Provides an interface for logging into and interacting with the Steam Community website",
|
||||||
"keywords": ["steam", "steam community"],
|
"keywords": ["steam", "steam community"],
|
||||||
"homepage": "https://github.com/DoctorMcKay/node-steamcommunity",
|
"homepage": "https://github.com/DoctorMcKay/node-steamcommunity",
|
||||||
@@ -13,9 +13,10 @@
|
|||||||
"url": "https://github.com/DoctorMcKay/node-steamcommunity.git"
|
"url": "https://github.com/DoctorMcKay/node-steamcommunity.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"request": "2.51.x",
|
"request": "^2.58.0",
|
||||||
"node-bignumber": "1.2.x",
|
"node-bignumber": "^1.2.1",
|
||||||
"steamid": "0.1.x",
|
"steamid": "^0.3.0",
|
||||||
"xml2js": "0.4.x"
|
"xml2js": "^0.4.9",
|
||||||
|
"cheerio": "^0.19.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user