<commit_before>module.exports = SteamTradeOffers;

var request = require('request');
var cheerio = require('cheerio');
var Long = require('long');

require('util').inherits(SteamTradeOffers, require('events').EventEmitter);

function SteamTradeOffers() {
  require('events').EventEmitter.call(this);

  this._j = request.jar();
  this._request = request.defaults({jar:this._j});
}

SteamTradeOffers.prototype.setup = function(sessionID, webCookie, callback){
  var self = this;

  this.sessionID = sessionID;

  webCookie.forEach(function(name){
    setCookie(self, name);
  });

  getAPIKey(this, callback);
};

function getAPIKey(self, callback) {
  self._request.get({
    uri: 'http://steamcommunity.com/dev/apikey'
  }, function(error, response, body) {
    if (error || response.statusCode != 200) {
      self.emit('debug', 'retrieving apikey: ' + (error || response.statusCode));
      getAPIKey(self, callback);
    } else {
      var $ = cheerio.load(body);
      if($('#bodyContents_ex h2').html() == 'Your Steam Web API Key'){
        var key = $('#bodyContents_ex p').html().split(' ')[1];
        self.APIKey = key;
        if(typeof callback == 'function'){
         callback();
        }
      } else {
        self._request.post({
          uri: 'http://steamcommunity.com/dev/registerkey',
          form: {
            domain: 'localhost',
            agreeToTerms: 1
          }
        }, function(error, response, body) {
            getAPIKey(self, callback);
        }.bind(self));
      }
    }
  }.bind(self));
}

function setCookie(self, cookie) {
  self._j.add(request.cookie(cookie));
}

SteamTradeOffers.prototype.loadMyInventory = function(appid, contextid, callback) {
  var self = this;

  this._request.get({
    uri: 'http://steamcommunity.com/my/inventory/json/' + appid + '/' + contextid,
    json: true
  }, function(error, response, body) {
    if (error || response.statusCode != 200) {
      self.emit('debug', 'loading my inventory: ' + (error || response.statusCode));
      this.loadMyInventory(appid, contextid, callback);
    } else if (typeof body != 'object') {
      // no session
      if(typeof callback == 'function'){
       callback(new Error('No session'));
      }
    } else {
      if(typeof callback == 'function'){
        callback(null, mergeWithDescriptions(body.rgInventory, body.rgDescriptions, contextid)
        .concat(mergeWithDescriptions(body.rgCurrency, body.rgDescriptions, contextid)));
      }
    }
  }.bind(this));
};

SteamTradeOffers.prototype.loadPartnerInventory = function(partner, appid, contextid, callback) {
  var self = this;

  this._request.post({
    uri: 'http://steamcommunity.com/tradeoffer/new/partnerinventory/',
    json: true,
    headers: {
      referer: 'http://steamcommunity.com/tradeoffer/new/?partner=' + toAccountId(partner)
    },
    form: {
      sessionid: this.sessionID,
      partner: partner,
      appid: appid,
      contextid: contextid
    }
  }, function(error, response, body) {
    if (error || response.statusCode != 200) {
      self.emit('debug', 'loading partner inventory: ' + (error || response.statusCode));
      this.loadPartnerInventory(partner, appid, contextid, callback);
    } else if (typeof body != 'object') {
      // no session
      if(typeof callback == 'function'){
       callback(new Error('No session'));
      }
    } else {<commit_msg>properly handle empty object in loadInventory methods<commit_after>module.exports = SteamTradeOffers;

var request = require('request');
var cheerio = require('cheerio');
var Long = require('long');

require('util').inherits(SteamTradeOffers, require('events').EventEmitter);

function SteamTradeOffers() {
  require('events').EventEmitter.call(this);

  this._j = request.jar();
  this._request = request.defaults({jar:this._j});
}

SteamTradeOffers.prototype.setup = function(sessionID, webCookie, callback){
  var self = this;

  this.sessionID = sessionID;

  webCookie.forEach(function(name){
    setCookie(self, name);
  });

  getAPIKey(this, callback);
};

function getAPIKey(self, callback) {
  self._request.get({
    uri: 'http://steamcommunity.com/dev/apikey'
  }, function(error, response, body) {
    if (error || response.statusCode != 200) {
      self.emit('debug', 'retrieving apikey: ' + (error || response.statusCode));
      getAPIKey(self, callback);
    } else {
      var $ = cheerio.load(body);
      if($('#bodyContents_ex h2').html() == 'Your Steam Web API Key'){
        var key = $('#bodyContents_ex p').html().split(' ')[1];
        self.APIKey = key;
        if(typeof callback == 'function'){
         callback();
        }
      } else {
        self._request.post({
          uri: 'http://steamcommunity.com/dev/registerkey',
          form: {
            domain: 'localhost',
            agreeToTerms: 1
          }
        }, function(error, response, body) {
            getAPIKey(self, callback);
        }.bind(self));
      }
    }
  }.bind(self));
}

function setCookie(self, cookie) {
  self._j.add(request.cookie(cookie));
}

SteamTradeOffers.prototype.loadMyInventory = function(appid, contextid, callback) {
  var self = this;

  this._request.get({
    uri: 'http://steamcommunity.com/my/inventory/json/' + appid + '/' + contextid,
    json: true
  }, function(error, response, body) {
    if (error || response.statusCode != 200 || JSON.stringify(body) == '{}') {
      self.emit('debug', 'loading my inventory: ' + (error || response.statusCode != 200 ? response.statusCode : '{}'));
      this.loadMyInventory(appid, contextid, callback);
    } else if (typeof body != 'object') {
      // no session
      if(typeof callback == 'function'){
       callback(new Error('No session'));
      }
    } else {
      if(typeof callback == 'function'){
        callback(null, mergeWithDescriptions(body.rgInventory, body.rgDescriptions, contextid)
        .concat(mergeWithDescriptions(body.rgCurrency, body.rgDescriptions, contextid)));
      }
    }
  }.bind(this));
};

SteamTradeOffers.prototype.loadPartnerInventory = function(partner, appid, contextid, callback) {
  var self = this;

  this._request.post({
    uri: 'http://steamcommunity.com/tradeoffer/new/partnerinventory/',
    json: true,
    headers: {
      referer: 'http://steamcommunity.com/tradeoffer/new/?partner=' + toAccountId(partner)
    },
    form: {
      sessionid: this.sessionID,
      partner: partner,
      appid: appid,
      contextid: contextid
    }
  }, function(error, response, body) {
    if (error || response.statusCode != 200 || JSON.stringify(body) == '{}') {
      self.emit('debug', 'loading partner inventory: ' + (error || response.statusCode != 200 ? response.statusCode : '{}'));
      this.loadPartnerInventory(partner, appid, contextid, callback);
    } else if (typeof body != 'object') {
      // no session
      if(typeof callback == 'function'){
       callback(new Error('No session'));
      }
    } else {
{'fraction_non_alphanumeric': 0.15739406168924763, 'fraction_numerical': 0.004324012683770539, 'mean_word_length': 3.157579388855602, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5570888', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2143, 'n_words': 426}