Skip to content

Instantly share code, notes, and snippets.

@rizowski
Last active December 11, 2015 16:05
Show Gist options
  • Save rizowski/90a47edda2cf2643dc41 to your computer and use it in GitHub Desktop.
Save rizowski/90a47edda2cf2643dc41 to your computer and use it in GitHub Desktop.
Google Script Examples
/**
* Gets the highscores from the poorly constructed Runescape api. This is a members only feature.
* [level, xp, rank]
* @param {string} username
* @param {boolean} oldSchoolServer
* @return {object} returns user[][]
*/
getHighScores: function(username, old) {
var url;
var data;
var array;
var endResult = [];
var inserted = false;
var cache = CacheService.getPublicCache();
var user = cache[username];
if (user != null) {
return user;
}
if (typeof(old) !== "boolean") {
throw "Must have a true false operator";
}
url = old ? "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=" : "http://hiscore.runescape.com/index_lite.ws?player=";
data = getURLData(url, username);
array = data.split('\n');
endResult = [];
inserted = false;
for (var i = 0; i < skills.length; i++) {
var items = array[i].split(',');
if (i < 26) {
endResult[i] = [this.skills[i], items[0], items[1], items[2]];
} else {
if (!inserted) {
endResult[i] = ["Game", "Rank", "Score"];
inserted = true;
}
endResult[i + 1] = [this.skills[i], items[0], items[1]];
}
}
cache.put(username, endResult, 3600);
return endResult;
}
/**
* Takes in a price and adds extra zeros or turns the string into a number
* Input that it can parse: $305 $5,000 2.5k 3.2m 1.3b
* @param {string} payload
* @return {number} Returns the formatted number
*/
function getKPrice_(payload) {
var pattern = new RegExp(/\d*\.?,?\d*[kmb]?/);
var dotPattern = new RegExp(/\d*\.?\d*/);
var price = new String(pattern.exec(payload));
var actual = null;
if (price.match(",")) {
pattern = new RegExp(/\d*\,?\d*/);
actual = new String(pattern.exec(price));
var array = actual.split(",");
var thousands = array[0];
thousands = thousands * 1000;
var hundreds = array[1] * 1;
actual = thousands + hundreds;
} else if (price.match("k")) {
actual = dotPattern.exec(price);
actual = actual * 1000;
} else if (price.match("m")) {
actual = dotPattern.exec(price);
actual = actual * 1000000;
} else if (price.match("b")) {
actual = dotPattern.exec(price);
actual = actual * 1000000000;
} else {
actual = price * 1;
}
return actual;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment