mirror of
https://github.com/DoctorMcKay/node-steamcommunity.git
synced 2025-03-29 09:40:11 +08:00
Updated getInventoryHistory for new pagination scheme
This commit is contained in:
parent
79543187f0
commit
3afb393f58
@ -5,15 +5,40 @@ var request = require('request');
|
|||||||
var Cheerio = require('cheerio');
|
var Cheerio = require('cheerio');
|
||||||
var Async = require('async');
|
var Async = require('async');
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Inventory history in a nutshell.
|
||||||
|
*
|
||||||
|
* There are no more page numbers. Now you have to request after_time and optionally after_trade.
|
||||||
|
* Without "prev" set, you will request 30 trades that were completed FURTHER IN THE PAST than after_time (and optionally after_trade)
|
||||||
|
* With "prev" set, you will request 30 trades that were completed MORE RECENTLY than after_time (and optionally after_trade)
|
||||||
|
*/
|
||||||
|
|
||||||
SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
|
SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
callback = options;
|
callback = options;
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.direction = options.direction || "past";
|
||||||
|
|
||||||
options.page = options.page || 1;
|
var qs = "?l=english";
|
||||||
|
if (options.startTime) {
|
||||||
|
if (options.startTime instanceof Date) {
|
||||||
|
options.startTime = Math.floor(options.startTime.getTime() / 1000);
|
||||||
|
}
|
||||||
|
|
||||||
this.httpRequest("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) {
|
qs += "&after_time=" + options.startTime;
|
||||||
|
|
||||||
|
if (options.startTrade) {
|
||||||
|
qs += "&after_trade=" + options.startTrade;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.direction == "future") {
|
||||||
|
qs += "&prev=1";
|
||||||
|
}
|
||||||
|
|
||||||
|
this._myProfile("inventoryhistory" + qs, null, function(err, response, body) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
@ -23,31 +48,46 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
|
|||||||
var vanityURLs = [];
|
var vanityURLs = [];
|
||||||
|
|
||||||
var $ = Cheerio.load(body);
|
var $ = Cheerio.load(body);
|
||||||
var html = $('.inventory_history_pagingrow').html();
|
if (!$('.inventory_history_pagingrow').html()) {
|
||||||
if (!html) {
|
|
||||||
callback("Malformed page: no paging row found");
|
callback("Malformed page: no paging row found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var match = html.match(/(\d+) - (\d+) of (\d+) History Items/);
|
|
||||||
|
|
||||||
output.first = parseInt(match[1], 10);
|
|
||||||
output.last = parseInt(match[2], 10);
|
|
||||||
output.totalTrades = parseInt(match[3], 10);
|
|
||||||
|
|
||||||
// Load the inventory item data
|
// Load the inventory item data
|
||||||
var match2 = body.match(/var g_rgHistoryInventory = (.*);/);
|
var match2 = body.match(/var g_rgHistoryInventory = (.*);/);
|
||||||
if (!match2) {
|
if (!match2) {
|
||||||
callback(new Error("Malformed page: no trade found"));
|
callback(new Error("Malformed page: no trade found"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var historyInventory = JSON.parse(match2[1]);
|
|
||||||
|
try {
|
||||||
|
var historyInventory = JSON.parse(match2[1]);
|
||||||
|
} catch (ex) {
|
||||||
|
callback(new Error("Malformed page: no well-formed trade data found"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var i;
|
||||||
|
|
||||||
|
// See if we've got paging buttons
|
||||||
|
var $paging = $('.inventory_history_nextbtn .pagebtn:not(.disabled)');
|
||||||
|
var href;
|
||||||
|
for (i = 0; i < $paging.length; i++) {
|
||||||
|
href = $paging[i].attribs.href;
|
||||||
|
if (href.match(/prev=1/)) {
|
||||||
|
output.firstTradeTime = new Date(href.match(/after_time=(\d+)/)[1] * 1000);
|
||||||
|
output.firstTradeID = href.match(/after_trade=(\d+)/)[1];
|
||||||
|
} else {
|
||||||
|
output.lastTradeTime = new Date(href.match(/after_time=(\d+)/)[1] * 1000);
|
||||||
|
output.lastTradeID = href.match(/after_trade=(\d+)/)[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
output.trades = [];
|
output.trades = [];
|
||||||
var trades = $('.tradehistoryrow');
|
var trades = $('.tradehistoryrow');
|
||||||
|
|
||||||
var item, trade, profileLink, items, j, econItem, timeMatch, time;
|
var item, trade, profileLink, items, j, econItem, timeMatch, time;
|
||||||
for (var i = 0; i < trades.length; i++) {
|
for (i = 0; i < trades.length; i++) {
|
||||||
item = $(trades[i]);
|
item = $(trades[i]);
|
||||||
trade = {};
|
trade = {};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user