SHARE
TWEET

api get multiple stats

a guest Feb 7th, 2018 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* GW2 API query tool for items - note this is a hideously large query, be kind to the api servers and don't use this more than you have to! */
  2. // Function to convert an object variable into a table
  3. function makeTableHTML(data, itemstats) {
  4.     var result = '<table class="table mech1" style="margin-bottom:0px;"><tr><th>ID</th><th>Name</th><th>Level</th><th>Rarity</th><th>Prefixes</th></tr>';
  5.     $.each(data, function(index, val) {
  6.         var id = val['id'];
  7.         var name = val['name'];
  8.         var level = val['level'];
  9.         var rarity = val['rarity'];
  10.         var details = {};
  11.         var stats = [];
  12.         var prefixnamesstring = '';
  13.  
  14.         if (val.hasOwnProperty('details')){
  15.             details = val['details'];
  16.             if (details.hasOwnProperty('stat_choices')){
  17.                 stats = details['stat_choices'];
  18.                 var prefixnames = [];
  19.                 $.each(stats, function(i,prefixid) {
  20.                     if (itemstats.hasOwnProperty(prefixid)) {
  21.                         prefixnames.push(itemstats[prefixid]['name']);
  22.                     } else {
  23.                         prefixnames.push('UNKOWNPREFIX'+prefixid);
  24.                     }
  25.                    
  26.                 });
  27.                 prefixnames.sort(function(a,b) {
  28.                     return a > b;
  29.                 });
  30.                 prefixnamesstring = prefixnames.join(', ');
  31.             }
  32.         }
  33.         if (stats.length < 2) {
  34.             return;
  35.         } else {
  36.             result += '<tr><td>'+id+'</td><td>'+name+'</td><td>'+level+'</td><td>'+rarity+'</td><td>'+JSON.stringify(stats)+'</td><td>'+prefixnamesstring+'</td></tr>';
  37.         }
  38.     });
  39.     result += '</table>';
  40.     return result;
  41. }
  42.  
  43. // Fetch all the possible itemsIDs
  44. (function fetchAPIData() {
  45.     $.getJSON('https://api.guildwars2.com/v2/itemstats?ids=all').done(function (itemstatstemp) {
  46.         var itemstats = {};
  47.         $.each(itemstatstemp, function(k,v) {
  48.             // v.name = v.name.replace("'s","");
  49.             itemstats[v.id] = v;
  50.         });
  51.  
  52.     $.getJSON('https://api.guildwars2.com/v2/items').done(function (ids) {
  53.         // var ids = [30704,30689];
  54.         console.log('Number of items: ' + ids.length);
  55.  
  56.         // Query API until all the ids have been requested
  57.         var promises = [], maxsize = 200;
  58.         for (var i=0; i<ids.length; i+=maxsize) {
  59.             var current_ids = ids.slice(i,i+maxsize).join(',');
  60.             var promise = $.getJSON('https://api.guildwars2.com/v2/items?lang=en&ids='+current_ids);
  61.             promises.push(promise);
  62.         }
  63.  
  64.         // Wait until all the GET requests have finished
  65.         $.when.apply($,promises).done(function() {
  66.             // Ensure data is an object
  67.             var newarguments = {};
  68.             if ( promises.length > 1 ) {
  69.                 newarguments = arguments;
  70.             } else {
  71.                 newarguments[0] = arguments;
  72.             }
  73.  
  74.             // Concatenate data into data
  75.             var data = {}
  76.             $.each(newarguments, function(index, element){
  77.                 $.each(element[0], function(key, val){
  78.                     data[val['id']] = val;
  79.                 });
  80.             });
  81.  
  82.             // Write to console
  83.             // console.log(data);
  84.  
  85.             // Write to document
  86.             $('#apidata').html(makeTableHTML(data, itemstats));
  87.         });
  88.     });
  89.     });
  90. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top