Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function clean_data(obj) {
- for (var key in obj) {
- // Delete null, undefined, "", " "
- if (obj[key] === null || obj[key] === undefined || obj[key] === "" || obj[key] === " ") {
- delete obj[key];
- }
- // Delete empty object
- // Note : typeof Array is also object
- if (typeof obj[key] === 'object' && Object.keys(obj[key]).length <= 0) {
- delete obj[key];
- }
- // If non empty object call function again
- if(typeof obj[key] === 'object'){
- clean_data(obj[key]);
- }
- }
- return obj;
- };
- function getPrice(itemId,apiKey) {
- var selections = ['itemmarket', 'bazaar'];
- var url = 'https://api.torn.com/market/'+ itemId+ '?selections=' + selections.join(',') + '&key='+ apiKey;
- var options = {
- 'method' : 'post',
- 'contentType': 'application/json',
- 'muteHttpExceptions': true
- };
- var res = UrlFetchApp.fetch(url, options);
- var data = JSON.parse(res.getContentText());
- var values = [];
- for (y = 0; y < selections.length; ++y) {
- if(!data[selections[y]]) {clean_data(data[selections[y]]);}
- else {
- var entries = data[selections[y]];
- var ids = Object.keys(entries);
- for(i = 0; i < 1; i++) {
- values.push(entries[ids[i]].cost);
- }
- }
- }
- if (values.length > 0) {
- return Math.min.apply(null, values);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment