diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4f9465d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: '2' + +services: + + app: + build: docker/app + volumes: + - ./:/app diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile new file mode 100644 index 0000000..e1140a3 --- /dev/null +++ b/docker/app/Dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu:16.04 + +# NodeJS / Yarn +RUN apt-get update \ + && apt-get install -y curl \ + && curl -sL https://deb.nodesource.com/setup_6.x | bash - \ + && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ + && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ + && apt-get update \ + && apt-get install -y nodejs yarn + +WORKDIR /app +CMD ["yarn", "start"] diff --git a/package.json b/package.json index 3364728..ceaded6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "watch": "npm run dev -- --watch", "build": "webpack -p --env production", "serve": "webpack-dev-server --content-base public/", - "splatnet": "node src/splatoon/update.js" + "splatnet": "node src/splatoon/update.js", + "start": "node src/splatoon/run.js" }, "dependencies": { "axios": "^0.16.2", @@ -16,6 +17,7 @@ "babel-preset-stage-2": "^6.24.1", "bulma": "^0.5.0", "clean-webpack-plugin": "^0.1.16", + "cron": "^1.2.1", "css-loader": "^0.28.4", "dotenv": "^4.0.0", "extract-text-webpack-plugin": "^3.0.0", diff --git a/readme.md b/readme.md index 4abf8ff..a8ca89f 100644 --- a/readme.md +++ b/readme.md @@ -12,8 +12,20 @@ yarn serve yarn build ``` +Or via Docker: + +```shell +docker-compose run --rm app yarn build +``` + ## Update SplatNet Data ```shell yarn splatnet ``` + +On a schedule via Docker: + +```shell +docker-compose up -d app +``` diff --git a/src/splatoon/run.js b/src/splatoon/run.js new file mode 100644 index 0000000..17039d3 --- /dev/null +++ b/src/splatoon/run.js @@ -0,0 +1,9 @@ +const CronJob = require('cron').CronJob; +const splatnet = require('./splatnet'); + +console.info('Starting periodic tasks...'); + +// Run every day at 2min 30sec after the hour +new CronJob('02 24 * * * *', () => { + splatnet.update(); +}, null, true); diff --git a/src/splatoon/splatnet.js b/src/splatoon/splatnet.js new file mode 100644 index 0000000..51d1a89 --- /dev/null +++ b/src/splatoon/splatnet.js @@ -0,0 +1,25 @@ +require('dotenv').config(); +const axios = require('axios'); +const path = require('path'); +const fs = require('fs'); +const mkdirp = require('mkdirp'); + +const dataPath = path.resolve('public/data'); + +// SplatNet2 API +const api = axios.create({ + baseURL: 'https://app.splatoon2.nintendo.net/api/', + headers: {'Cookie': `iksm_session=${process.env.NINTENDO_SESSION_ID}`}, +}); + +module.exports.update = function() { + // Make sure the data path exists + mkdirp(dataPath); + + // Update map schedules + console.info('Updating map schedules...'); + api.get('schedules').then(response => { + fs.writeFile(`${dataPath}/schedules.json`, JSON.stringify(response.data)); + console.info('Updated map schedules.'); + }); +} diff --git a/src/splatoon/update.js b/src/splatoon/update.js index eb044f9..6f0d198 100644 --- a/src/splatoon/update.js +++ b/src/splatoon/update.js @@ -1,22 +1,3 @@ -require('dotenv').config(); -const axios = require('axios'); -const path = require('path'); -const fs = require('fs'); -const mkdirp = require('mkdirp'); +const splatnet = require('./splatnet'); -// Make sure the data path exists -const dataPath = path.resolve('public/data'); -mkdirp(dataPath); - -// SplatNet2 API -const api = axios.create({ - baseURL: 'https://app.splatoon2.nintendo.net/api/', - headers: {'Cookie': `iksm_session=${process.env.NINTENDO_SESSION_ID}`}, -}); - -// Update map schedules -console.info('Updating map schedules...'); -api.get('schedules').then(response => { - fs.writeFile(`${dataPath}/schedules.json`, JSON.stringify(response.data)); - console.info('Updated map schedules.'); -}); +splatnet.update(); diff --git a/yarn.lock b/yarn.lock index 74d99ff..d993d2f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1384,6 +1384,12 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: safe-buffer "^5.0.1" sha.js "^2.4.8" +cron@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/cron/-/cron-1.2.1.tgz#3a86c09b41b8f261ac863a7cc85ea4735857eab2" + dependencies: + moment-timezone "^0.5.x" + cross-spawn@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-3.0.1.tgz#1256037ecb9f0c5f79e3d6ef135e30770184b982" @@ -3357,7 +3363,13 @@ mkdirp@0.5.1, mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdi dependencies: minimist "0.0.8" -moment@^2.18.1: +moment-timezone@^0.5.x: + version "0.5.13" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.13.tgz#99ce5c7d827262eb0f1f702044177f60745d7b90" + dependencies: + moment ">= 2.9.0" + +"moment@>= 2.9.0", moment@^2.18.1: version "2.18.1" resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"