Advertisement
Lanona

Untitled

Mar 28th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. // Adds a new button (RELOAD) to the menu which executes the getAccessToken-function
  2. // and thus triggers a reload of the iLv-function
  3. function onOpen() {
  4. var sheet = SpreadsheetApp.getActiveSpreadsheet();
  5. var entries = [{
  6. name : "Update Now!",
  7. functionName : "getAccessToken"
  8. }];
  9. sheet.addMenu("↻ RELOAD", entries);
  10. };
  11.  
  12.  
  13. // Writes the current date into cell 'Z1'
  14. // Requests a new access_token and writes it into cell 'Z2'
  15. // Access tokens expire after 24 hours.
  16. function getAccessToken() {
  17. var apiusername = 'c937ee086498497696e5af033fd1932f';
  18. var apisecretkey = '83qMA17Ohk4RtZAhgMYZ6Z1SrS7wTcw5';
  19. var url ='https://eu.battle.net/oauth/token';
  20. var options = {
  21. method: 'post',
  22. headers : {"Authorization" : " Basic " + Utilities.base64Encode(apiusername + ":" + apisecretkey)},
  23. payload: {
  24. "grant_type": "client_credentials",
  25. "scope": "basic+user"
  26. }
  27. };
  28. var response = UrlFetchApp.fetch(url, options);
  29. var access_token = JSON.parse(response.toString()).access_token;
  30.  
  31. SpreadsheetApp.getActiveSpreadsheet().getRange('Z2').setValue(access_token);
  32. SpreadsheetApp.getActiveSpreadsheet().getRange('Z1').setValue(new Date().toString());
  33. }
  34.  
  35. // iLv returns the itemlevel and azerite-level of the WoW-Character in the cell to the left
  36. // date: changes with every push of the RELOAD-Button, thus forcing a reload - otherwise unused
  37. // realmName: optional, defaults to 'eredar', should be given if toon is on another realm
  38. // count: controls number of request-attempts (should not be set manually)
  39. function iLv(date, realmName, count) {
  40.  
  41. // load toonName from the left cell
  42. var sheet = SpreadsheetApp.getActiveSpreadsheet();
  43. var leftCellNum = sheet.getActiveRange().offset(0, -1).getA1Notation();
  44. var toonName = sheet.getRange(leftCellNum).getValue();
  45.  
  46. // ignore empty fields
  47. if (toonName == "") return "";
  48.  
  49. // default Realm is Onyxia
  50. if (!realmName) realmName = "blackhand";
  51.  
  52. // prevent spam
  53. Utilities.sleep(Math.floor((Math.random() * 5000) + 1000));
  54.  
  55. // remove unwanted characters
  56. toonName = toonName.replace(/[\u200B-\u200D\uFEFF]/g, '');
  57. realmName = realmName.replace(/[\u200B-\u200D\uFEFF]/g, '');
  58.  
  59. // OAuth authentication token, required for the api call
  60. // written into Z2 by getAccessToken(), which is called by the update-Button
  61. var access_token = SpreadsheetApp.getActiveSpreadsheet().getRange('Z2').getValue();
  62.  
  63. try { // make the api call
  64. var toonJSON = UrlFetchApp.fetch("https://eu.api.blizzard.com/wow/character/"+realmName+"/"+toonName+"?fields=items&locale=en_EU&access_token="+access_token);
  65. var toon = JSON.parse(toonJSON.toString());
  66. return ""+toon.items.averageItemLevelEquipped+" - "+(toon.items.neck.azeriteItem.azeriteLevel+(toon.items.neck.azeriteItem.azeriteExperience/toon.items.neck.azeriteItem.azeriteExperienceRemaining)).toFixed(1);
  67.  
  68. } catch (err) { // if failed, try again
  69. if (!count) count = 0;
  70. if (count > 4) return "TODO"; // quit after 4 failed attempts
  71. else return iLv(date, realmName, count+1);
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement