Advertisement
RemcoE33

FFXIV API

Sep 19th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Created by RemcoE33
  3. 19 september 2021
  4. https://www.reddit.com/r/sheets/comments/pqw55q/how_can_i_import_data_from_other_sites_into_my/
  5. */
  6.  
  7.  
  8. function onOpen(e) {
  9.   SpreadsheetApp.getUi().createMenu('FFXIV')
  10.     .addItem('Refresh prices', 'FFXIV')
  11.     .addItem('Show History', 'createChart')
  12.     .addToUi();
  13. }
  14.  
  15. function FFXIV(worldName = 'famfrit', headers = true) {
  16.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  17.   const sheet = ss.getSheetByName('RemcoE33');
  18.   const ids = sheet.getRange(2, 2, sheet.getLastRow() - 1).getValues().flat();
  19.   const output = [];
  20.  
  21.   ids.forEach((id, index) => {
  22.     const object = getApiData(worldName, id);
  23.  
  24.     if (index == 0 && headers) {
  25.       output.push(Object.keys(object))
  26.     }
  27.  
  28.     output.push(Object.values(object))
  29.   })
  30.  
  31.   const rowStart = (headers) ? 1 : 2;
  32.   sheet.getRange(2,3,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
  33.   sheet.getRange(rowStart, 3, output.length, output[0].length).setValues(output);
  34.  
  35. }
  36.  
  37. function getApiData(world, id) {
  38.   const response = UrlFetchApp.fetch(`https://universalis.app/api/${world}/${id}`, { muteHttpExceptions: true });
  39.  
  40.   if (response.getResponseCode() == 429) {
  41.     Utilities.sleep(500);
  42.     getApiData(world, id)
  43.   } else {
  44.     const data = JSON.parse(response.getContentText());
  45.     const object = {
  46.       'World Id': data.worldID,
  47.       'Last Upload': new Date(data.lastUploadTime).toLocaleDateString(),
  48.       'Regular Sale Velocity': data.regularSaleVelocity,
  49.       'NQ Sale Velocity': data.nqSaleVelocity,
  50.       'HQ Sale Velocity': data.hqSaleVelocity,
  51.       'Average Price': data.averagePrice,
  52.       'Average Price NQ': data.averagePriceNQ,
  53.       'Average Price HQ': data.averagePriceHQ,
  54.       'Min price': data.minPrice,
  55.       'Min price NQ': data.minPriceNQ,
  56.       'Min price HQ': data.minPriceHQ,
  57.       'Max price': data.maxPrice,
  58.       'Max price NQ': data.maxPriceNQ,
  59.       'MAx price HQ': data.maxPriceHQ
  60.     }
  61.     return object;
  62.   }
  63. }
  64.  
  65.  
  66. function getChartData() {
  67.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  68.   const activeSheet = ss.getActiveSheet();
  69.   const cell = activeSheet.getActiveCell()
  70.   const [ title, id, world ] = activeSheet.getRange(cell.getRow(), 1, 1, 3).getValues().flat();
  71.   const response = UrlFetchApp.fetch(`https://universalis.app/api/history/${world}/${id}`)
  72.   const json = JSON.parse(response.getContentText());
  73.   const data = [];
  74.   const labels = [];
  75.  
  76.   json.entries.forEach(entrie => {
  77.     data.push(entrie.pricePerUnit)
  78.     labels.push(Utilities.formatDate(new Date(entrie.timestamp * 1000), Session.getScriptTimeZone(), 'yyyy-MM-dd'))
  79.   })
  80.   return { title, data, labels };
  81. }
  82.  
  83. function createChart() {
  84.   const html = HtmlService.createHtmlOutputFromFile("chart");
  85.   html.setHeight(500).setWidth(800);
  86.   SpreadsheetApp.getUi().showDialog(html);
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement