Created
September 9, 2016 04:35
-
-
Save Skylark95/860de98b50f50e98e5ee0cc2e6d5c495 to your computer and use it in GitHub Desktop.
BundleStars Promotion Steam Links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Name: BundleStars Promotion Steam Links | |
* Description: Retrieves markdown formmated steam links for each product in a BundleStars promotion | |
* Author: Derek <[email protected]> | |
* Last Update: 2016/09/08 | |
* License: MIT | |
*/ | |
var promotion = ''; // paste promotion slug here | |
// XMLHttpRequest | |
function get(url, callback) { | |
var httpRequest = new XMLHttpRequest(); | |
httpRequest.onreadystatechange = function() { | |
if (httpRequest.readyState === XMLHttpRequest.DONE) { | |
if (httpRequest.status === 200) { | |
callback.call(this, httpRequest.responseText); | |
} else { | |
console.error("Request Failed: " + url); | |
} | |
} | |
}; | |
httpRequest.open('GET', url); | |
httpRequest.send(); | |
} | |
// get promotion | |
get('https://www.bundlestars.com/api/promotions/' + promotion, function(promotionsData) { | |
var promotions = JSON.parse(promotionsData), | |
output = "", | |
count = 0; | |
// loop through products | |
promotions[0].products.forEach(function(product) { | |
get('https://www.bundlestars.com/api/products/' + product.slug, function(productData) { | |
var product = JSON.parse(productData); | |
output += "[" + product.name + "](http://store.steampowered.com/app/" + product.steam.id + ")\n"; | |
count++; | |
}); | |
}); | |
// wait for full response, or timeout after 10 seconds | |
var times = 0, | |
maxTimes = 50; | |
interval = setInterval(function() { | |
if (count === promotions[0].products.length) { | |
console.log(output); | |
clearInterval(interval); | |
} else if (times === maxTimes) { | |
clearInterval(interval); | |
} | |
}, 200); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment