Reformat inventoryhistory.js

This commit is contained in:
Alexander Corn 2016-08-31 21:48:17 -04:00
parent bb917af86e
commit 3139aec463

View File

@ -6,7 +6,7 @@ var Cheerio = require('cheerio');
var Async = require('async'); var Async = require('async');
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 = {};
} }
@ -14,7 +14,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
options.page = options.page || 1; options.page = options.page || 1;
this.httpRequest("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) { this.httpRequest("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) {
if(err) { if (err) {
callback(err); callback(err);
return; return;
} }
@ -24,7 +24,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
var $ = Cheerio.load(body); var $ = Cheerio.load(body);
var html = $('.inventory_history_pagingrow').html(); var html = $('.inventory_history_pagingrow').html();
if(!html) { if (!html) {
callback("Malformed page: no paging row found"); callback("Malformed page: no paging row found");
return; return;
} }
@ -37,7 +37,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
// 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;
} }
@ -47,18 +47,18 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
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 (var i = 0; i < trades.length; i++) {
item = $(trades[i]); item = $(trades[i]);
trade = {}; trade = {};
trade.onHold = !!item.find('span:nth-of-type(2)').text().match(/Trade on Hold/i); trade.onHold = !!item.find('span:nth-of-type(2)').text().match(/Trade on Hold/i);
timeMatch = item.find('.tradehistory_timestamp').html().match(/(\d+):(\d+)(am|pm)/); timeMatch = item.find('.tradehistory_timestamp').html().match(/(\d+):(\d+)(am|pm)/);
if(timeMatch[1] == 12 && timeMatch[3] == 'am') { if (timeMatch[1] == 12 && timeMatch[3] == 'am') {
timeMatch[1] = 0; timeMatch[1] = 0;
} }
if(timeMatch[1] < 12 && timeMatch[3] == 'pm') { if (timeMatch[1] < 12 && timeMatch[3] == 'pm') {
timeMatch[1] = parseInt(timeMatch[1], 10) + 12; timeMatch[1] = parseInt(timeMatch[1], 10) + 12;
} }
@ -72,21 +72,21 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
trade.itemsGiven = []; trade.itemsGiven = [];
profileLink = item.find('.tradehistory_event_description a').attr('href'); profileLink = item.find('.tradehistory_event_description a').attr('href');
if(profileLink.indexOf('/profiles/') != -1) { if (profileLink.indexOf('/profiles/') != -1) {
trade.partnerSteamID = new SteamID(profileLink.match(/(\d+)$/)[1]); trade.partnerSteamID = new SteamID(profileLink.match(/(\d+)$/)[1]);
} else { } else {
trade.partnerVanityURL = profileLink.match(/\/([^\/]+)$/)[1]; trade.partnerVanityURL = profileLink.match(/\/([^\/]+)$/)[1];
if(options.resolveVanityURLs && vanityURLs.indexOf(trade.partnerVanityURL) == -1) { if (options.resolveVanityURLs && vanityURLs.indexOf(trade.partnerVanityURL) == -1) {
vanityURLs.push(trade.partnerVanityURL); vanityURLs.push(trade.partnerVanityURL);
} }
} }
items = item.find('.history_item'); items = item.find('.history_item');
for(j = 0; j < items.length; j++) { for (j = 0; j < items.length; j++) {
match = body.match(new RegExp("HistoryPageCreateItemHover\\( '" + $(items[j]).attr('id') + "', (\\d+), '(\\d+)', '(\\d+|class_\\d+_instance_\\d+|class_\\d+)', '(\\d+)' \\);")); match = body.match(new RegExp("HistoryPageCreateItemHover\\( '" + $(items[j]).attr('id') + "', (\\d+), '(\\d+)', '(\\d+|class_\\d+_instance_\\d+|class_\\d+)', '(\\d+)' \\);"));
econItem = historyInventory[match[1]][match[2]][match[3]]; econItem = historyInventory[match[1]][match[2]][match[3]];
if($(items[j]).attr('id').indexOf('received') != -1) { if ($(items[j]).attr('id').indexOf('received') != -1) {
trade.itemsReceived.push(new CEconItem(econItem)); trade.itemsReceived.push(new CEconItem(econItem));
} else { } else {
trade.itemsGiven.push(new CEconItem(econItem)); trade.itemsGiven.push(new CEconItem(econItem));
@ -96,21 +96,21 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
output.trades.push(trade); output.trades.push(trade);
} }
if(options.resolveVanityURLs) { if (options.resolveVanityURLs) {
Async.map(vanityURLs, resolveVanityURL, function(err, results) { Async.map(vanityURLs, resolveVanityURL, function(err, results) {
if(err) { if (err) {
callback(err); callback(err);
return; return;
} }
for(i = 0; i < output.trades.length; i++) { for (i = 0; i < output.trades.length; i++) {
if(output.trades[i].partnerSteamID || !output.trades[i].partnerVanityURL) { if (output.trades[i].partnerSteamID || !output.trades[i].partnerVanityURL) {
continue; continue;
} }
// Find the vanity URL // Find the vanity URL
for(j = 0; j < results.length; j++) { for (j = 0; j < results.length; j++) {
if(results[j].vanityURL == output.trades[i].partnerVanityURL) { if (results[j].vanityURL == output.trades[i].partnerVanityURL) {
output.trades[i].partnerSteamID = new SteamID(results[j].steamID); output.trades[i].partnerSteamID = new SteamID(results[j].steamID);
break; break;
} }
@ -127,13 +127,13 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
function resolveVanityURL(vanityURL, callback) { function resolveVanityURL(vanityURL, callback) {
request("https://steamcommunity.com/id/" + vanityURL + "/?xml=1", function(err, response, body) { request("https://steamcommunity.com/id/" + vanityURL + "/?xml=1", function(err, response, body) {
if(err) { if (err) {
callback(err); callback(err);
return; return;
} }
var match = body.match(/<steamID64>(\d+)<\/steamID64>/); var match = body.match(/<steamID64>(\d+)<\/steamID64>/);
if(!match || !match[1]) { if (!match || !match[1]) {
callback(new Error("Couldn't find Steam ID")); callback(new Error("Couldn't find Steam ID"));
return; return;
} }