diff --git a/app/data/index.mjs b/app/data/index.mjs new file mode 100644 index 0000000..1cdf4bf --- /dev/null +++ b/app/data/index.mjs @@ -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'); +} diff --git a/app/data/updaters/DataUpdater.mjs b/app/data/updaters/DataUpdater.mjs new file mode 100644 index 0000000..e7edf27 --- /dev/null +++ b/app/data/updaters/DataUpdater.mjs @@ -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); + } +} diff --git a/app/data/updaters/StageScheduleUpdater.mjs b/app/data/updaters/StageScheduleUpdater.mjs new file mode 100644 index 0000000..494de31 --- /dev/null +++ b/app/data/updaters/StageScheduleUpdater.mjs @@ -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(); + } +} diff --git a/app/index.mjs b/app/index.mjs index f662e12..0789a65 100644 --- a/app/index.mjs +++ b/app/index.mjs @@ -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]; diff --git a/app/splatnet/SplatNet3Client.mjs b/app/splatnet/SplatNet3Client.mjs index 9e0439e..3cb3328 100644 --- a/app/splatnet/SplatNet3Client.mjs +++ b/app/splatnet/SplatNet3Client.mjs @@ -110,4 +110,10 @@ export default class SplatNet3Client return await this.getGraphQL(body); } + + // Specific queries + + getStageScheduleData() { + return this.getGraphQLPersistedQuery(1, '10e1d424391e78d21670227550b3509f'); + } } diff --git a/package.json b/package.json index b92edef..6b9273b 100644 --- a/package.json +++ b/package.json @@ -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",