persistently call sheets api to try and fix ratelimiting issues

This commit is contained in:
Jill 2024-02-29 13:39:58 +03:00
parent bc98cc6eaa
commit f6336babdb
4 changed files with 43 additions and 21 deletions

11
ids.js
View File

@ -1,4 +1,5 @@
import { GoogleSpreadsheet } from 'google-spreadsheet';
import { persistently } from './persistance.js';
const IDS_ID = '15ehtAIpCR8s04qIb8zij9sTpUdGJbmAE_LDcfVA3tcU';
const IDS_REGULAR_LEVELS_ID = 1309758655;
@ -39,7 +40,7 @@ const colors = {
async function fetchRegularCells(sheet) {
console.log('fetching cells');
// we need this for formatting :)
await sheet.loadCells('A:B');
await persistently(async () => await sheet.loadCells('A:B'));
let levels = [];
let tiers = [];
@ -62,9 +63,9 @@ async function fetchRegularCells(sheet) {
rowID++;
}
const creators = await sheet.getCellsInRange('C2:C');
const skillsets = await sheet.getCellsInRange('E2:E');
const descriptions = await sheet.getCellsInRange('F2:F');
const creators = await persistently(async () => await sheet.getCellsInRange('C2:C'));
const skillsets = await persistently(async () => await sheet.getCellsInRange('E2:E'));
const descriptions = await persistently(async () => await sheet.getCellsInRange('F2:F'));
return { levels, tiers, broken, creators, skillsets, descriptions };
}
@ -97,7 +98,7 @@ let doc;
export async function loadSpreadsheet() {
doc = new GoogleSpreadsheet(IDS_ID, { apiKey: process.env.API_KEY });
await doc.loadInfo(); // loads document properties and worksheets
await persistently(async () => await doc.loadInfo()); // loads document properties and worksheets
console.log('loaded IDS spreadsheet');
}

View File

@ -27,6 +27,7 @@ function getAllLevels() {
async function exists(f) {
try {
await fs.stat(f);
console.log(f);
return true;
} catch {
return false;

33
nlw.js
View File

@ -1,4 +1,5 @@
import { GoogleSpreadsheet } from 'google-spreadsheet';
import { persistently } from './persistance.js';
const NLW_ID = '1YxUE2kkvhT2E6AjnkvTf-o8iu_shSLbuFkEFcZOvieA';
const NLW_REGULAR_LEVELS_ID = 0;
@ -36,7 +37,7 @@ async function fetchRegularCells(sheet) {
console.log('fetching regular cells');
// for brokenness
await sheet.loadCells('A:A');
await persistently(async () => await sheet.loadCells('A:A'));
let broken = [];
@ -55,29 +56,29 @@ async function fetchRegularCells(sheet) {
}
}
const levels = await sheet.getCellsInRange('B:B');
const creators = await sheet.getCellsInRange('C:C');
const skillsets = await sheet.getCellsInRange('E:E');
const enjoyments = await sheet.getCellsInRange('F:F');
const descriptions = await sheet.getCellsInRange('G:G');
const levels = await persistently(async () => await sheet.getCellsInRange('B:B'));
const creators = await persistently(async () => await sheet.getCellsInRange('C:C'));
const skillsets = await persistently(async () => await sheet.getCellsInRange('E:E'));
const enjoyments = await persistently(async () => await sheet.getCellsInRange('F:F'));
const descriptions = await persistently(async () => await sheet.getCellsInRange('G:G'));
return { levels, broken, creators, skillsets, enjoyments, descriptions };
}
async function fetchPlatformerCells(sheet) {
console.log('fetching platformer cells');
const levels = await sheet.getCellsInRange('A:A');
const creators = await sheet.getCellsInRange('B:B');
const skillsets = await sheet.getCellsInRange('D:D');
const enjoyments = await sheet.getCellsInRange('E:E');
const descriptions = await sheet.getCellsInRange('F:F');
const levels = await persistently(async () => await sheet.getCellsInRange('A:A'));
const creators = await persistently(async () => await sheet.getCellsInRange('B:B'));
const skillsets = await persistently(async () => await sheet.getCellsInRange('D:D'));
const enjoyments = await persistently(async () => await sheet.getCellsInRange('E:E'));
const descriptions = await persistently(async () => await sheet.getCellsInRange('F:F'));
return { levels, broken: Array(levels.length).fill('take a guess'), creators, skillsets, enjoyments, descriptions };
}
async function fetchPendingCells(sheet) {
console.log('fetching pending cells');
const levels = await sheet.getCellsInRange('B:B');
const creators = await sheet.getCellsInRange('C:C');
const skillsets = await sheet.getCellsInRange('E:E');
const levels = await persistently(async () => await sheet.getCellsInRange('B:B'));
const creators = await persistently(async () => await sheet.getCellsInRange('C:C'));
const skillsets = await persistently(async () => await sheet.getCellsInRange('E:E'));
const enjoyments = [];
const descriptions = await sheet.getCellsInRange('F:F');
const descriptions = await persistently(async () => await sheet.getCellsInRange('F:F'));
return { levels, broken: Array(levels.length).fill(null), creators, skillsets, enjoyments, descriptions };
}
@ -131,7 +132,7 @@ let doc;
async function loadSpreadsheet() {
doc = new GoogleSpreadsheet(NLW_ID, { apiKey: process.env.API_KEY });
await doc.loadInfo(); // loads document properties and worksheets
await persistently(async () => await doc.loadInfo()); // loads document properties and worksheets
console.log('loaded NLW spreadsheet');
}

19
persistance.js Normal file
View File

@ -0,0 +1,19 @@
let retryPeriod = 1_000;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export async function persistently(func) {
try {
let res = await func();
retryPeriod = 1_000;
return res;
} catch(err) {
console.error(err);
await sleep(retryPeriod);
retryPeriod *= 2;
retryPeriod = Math.min(64_000, retryPeriod);
return persistently(func);
}
}