Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Adds a new button (RELOAD) to the menu which executes the getAccessToken-function
- // and thus triggers a reload of the iLv-function
- function onOpen() {
- var sheet = SpreadsheetApp.getActiveSpreadsheet();
- var entries = [{
- name : "Update Now!",
- functionName : "getAccessToken"
- }];
- sheet.addMenu("↻ RELOAD", entries);
- };
- // Writes the current date into cell 'Z1'
- // Requests a new access_token and writes it into cell 'Z2'
- // Access tokens expire after 24 hours.
- function getAccessToken() {
- var apiusername = 'c937ee086498497696e5af033fd1932f';
- var apisecretkey = '83qMA17Ohk4RtZAhgMYZ6Z1SrS7wTcw5';
- var url ='https://eu.battle.net/oauth/token';
- var options = {
- method: 'post',
- headers : {"Authorization" : " Basic " + Utilities.base64Encode(apiusername + ":" + apisecretkey)},
- payload: {
- "grant_type": "client_credentials",
- "scope": "basic+user"
- }
- };
- var response = UrlFetchApp.fetch(url, options);
- var access_token = JSON.parse(response.toString()).access_token;
- SpreadsheetApp.getActiveSpreadsheet().getRange('Z2').setValue(access_token);
- SpreadsheetApp.getActiveSpreadsheet().getRange('Z1').setValue(new Date().toString());
- }
- // iLv returns the itemlevel and azerite-level of the WoW-Character in the cell to the left
- // date: changes with every push of the RELOAD-Button, thus forcing a reload - otherwise unused
- // realmName: optional, defaults to 'eredar', should be given if toon is on another realm
- // count: controls number of request-attempts (should not be set manually)
- function iLv(date, realmName, count) {
- // load toonName from the left cell
- var sheet = SpreadsheetApp.getActiveSpreadsheet();
- var leftCellNum = sheet.getActiveRange().offset(0, -1).getA1Notation();
- var toonName = sheet.getRange(leftCellNum).getValue();
- // ignore empty fields
- if (toonName == "") return "";
- // default Realm is Onyxia
- if (!realmName) realmName = "blackhand";
- // prevent spam
- Utilities.sleep(Math.floor((Math.random() * 5000) + 1000));
- // remove unwanted characters
- toonName = toonName.replace(/[\u200B-\u200D\uFEFF]/g, '');
- realmName = realmName.replace(/[\u200B-\u200D\uFEFF]/g, '');
- // OAuth authentication token, required for the api call
- // written into Z2 by getAccessToken(), which is called by the update-Button
- var access_token = SpreadsheetApp.getActiveSpreadsheet().getRange('Z2').getValue();
- try { // make the api call
- var toonJSON = UrlFetchApp.fetch("https://eu.api.blizzard.com/wow/character/"+realmName+"/"+toonName+"?fields=items&locale=en_EU&access_token="+access_token);
- var toon = JSON.parse(toonJSON.toString());
- return ""+toon.items.averageItemLevelEquipped+" - "+(toon.items.neck.azeriteItem.azeriteLevel+(toon.items.neck.azeriteItem.azeriteExperience/toon.items.neck.azeriteItem.azeriteExperienceRemaining)).toFixed(1);
- } catch (err) { // if failed, try again
- if (!count) count = 0;
- if (count > 4) return "TODO"; // quit after 4 failed attempts
- else return iLv(date, realmName, count+1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement