Guest User

Untitled

a guest
Mar 30th, 2017
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. function clean_data(obj) {
  2. for (var key in obj) {
  3. // Delete null, undefined, "", " "
  4. if (obj[key] === null || obj[key] === undefined || obj[key] === "" || obj[key] === " ") {
  5. delete obj[key];
  6. }
  7. // Delete empty object
  8. // Note : typeof Array is also object
  9. if (typeof obj[key] === 'object' && Object.keys(obj[key]).length <= 0) {
  10. delete obj[key];
  11. }
  12. // If non empty object call function again
  13. if(typeof obj[key] === 'object'){
  14. clean_data(obj[key]);
  15. }
  16. }
  17. return obj;
  18. };
  19.  
  20. function getPrice(itemId,apiKey) {
  21. var selections = ['itemmarket', 'bazaar'];
  22. var url = 'https://api.torn.com/market/'+ itemId+ '?selections=' + selections.join(',') + '&key='+ apiKey;
  23. var options = {
  24. 'method' : 'post',
  25. 'contentType': 'application/json',
  26. 'muteHttpExceptions': true
  27. };
  28. var res = UrlFetchApp.fetch(url, options);
  29. var data = JSON.parse(res.getContentText());
  30. var values = [];
  31.  
  32. for (y = 0; y < selections.length; ++y) {
  33. if(!data[selections[y]]) {clean_data(data[selections[y]]);}
  34. else {
  35. var entries = data[selections[y]];
  36. var ids = Object.keys(entries);
  37.  
  38. for(i = 0; i < 1; i++) {
  39. values.push(entries[ids[i]].cost);
  40. }
  41. }
  42. }
  43.  
  44. if (values.length > 0) {
  45. return Math.min.apply(null, values);
  46. }
  47. };
Advertisement
Add Comment
Please, Sign In to add comment