Add data updater structure and a basic schedule updater

This commit is contained in:
Matt Isenhower 2022-09-10 13:02:39 -07:00
parent 231681bdac
commit f7dafefc31
6 changed files with 113 additions and 1 deletions

21
app/data/index.mjs Normal file
View File

@ -0,0 +1,21 @@
import StageScheduleUpdater from "./updaters/StageScheduleUpdater.mjs";
function updaters() {
return [
new StageScheduleUpdater,
];
}
export async function updateAll() {
console.info('Running all updaters...');
for (let updater of updaters()) {
try {
await updater.update();
} catch (e) {
console.error(e);
}
}
console.info('Done running updaters');
}

View File

@ -0,0 +1,71 @@
import fs from 'fs/promises';
import path from 'path';
import mkdirp from 'mkdirp';
import prefixedConsole from "../../common/prefixedConsole.mjs";
import SplatNet3Client from "../../splatnet/SplatNet3Client.mjs";
export default class DataUpdater
{
name = null;
filename = null;
outputDirectory = 'dist/data';
constructor() {
this.splatnet = new SplatNet3Client;
}
/** @type {Console} */
get console() {
this._console ??= prefixedConsole('Updater', this.name);
return this._console;
}
get path() {
return `${this.outputDirectory}/${this.filename}`;
}
async update() {
this.console.info('Updating data...');
// Retrieve the data
let data = await this.tryRequest(this.getData());
// Write the data to disk
await this.writeData(this.path, data);
this.console.info('Done');
}
getData() {
//
}
async tryRequest(promise) {
try {
return await promise;
} catch (e) {
this.console.error('Error handling request:', e);
throw e;
}
}
formatDataForWrite(data) {
// If we're running in debug mode, format the JSON output so it's easier to read
let debug = !!process.env.DEBUG;
let space = debug ? 2 : undefined;
return JSON.stringify(data, undefined, space);
}
async writeData(file, data) {
let s = this.formatDataForWrite(data);
await this.writeFile(file, s);
}
async writeFile(file, data) {
await mkdirp(path.dirname(file))
await fs.writeFile(file, data);
}
}

View File

@ -0,0 +1,11 @@
import DataUpdater from "./DataUpdater.mjs";
export default class StageScheduleUpdater extends DataUpdater
{
name = 'Schedules';
filename = 'schedules.json';
getData() {
return this.splatnet.getStageScheduleData();
}
}

View File

@ -2,6 +2,7 @@ import dotenv from 'dotenv';
import consoleStamp from 'console-stamp';
import cron from './cron.mjs';
import { sendTweets } from './twitter/index.mjs';
import { updateAll } from './data/index.mjs';
consoleStamp(console);
dotenv.config();
@ -9,6 +10,7 @@ dotenv.config();
const actions = {
cron,
twitter: sendTweets,
splatnet: updateAll,
}
const command = process.argv[2];

View File

@ -110,4 +110,10 @@ export default class SplatNet3Client
return await this.getGraphQL(body);
}
// Specific queries
getStageScheduleData() {
return this.getGraphQLPersistedQuery(1, '10e1d424391e78d21670227550b3509f');
}
}

View File

@ -7,7 +7,8 @@
"preview": "vite preview --port 5050",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
"cron": "node app/index.mjs cron",
"twitter": "node app/index.mjs twitter"
"twitter": "node app/index.mjs twitter",
"splatnet": "node app/index.mjs splatnet"
},
"dependencies": {
"console-stamp": "^3.0.6",