Change updater to use separate accounts for each region

This commit is contained in:
Unknown 2017-11-09 16:15:29 -08:00
parent 87c42e27f6
commit 590705f355
3 changed files with 54 additions and 51 deletions

View File

@ -1,5 +1,7 @@
# Nintendo SplatNet 2 "iksm_session" value
NINTENDO_SESSION_ID=
# Nintendo SplatNet 2 "iksm_session" values
NINTENDO_SESSION_ID_NA=
NINTENDO_SESSION_ID_EU=
NINTENDO_SESSION_ID_JP=
# (Optional) User agent string for SplatNet requests
SPLATNET_USER_AGENT=

View File

@ -6,60 +6,64 @@ const axios = require('axios');
// SplatNet2 API
const userAgent = process.env.SPLATNET_USER_AGENT;
const splatnetBaseUrl = 'https://app.splatoon2.nintendo.net';
const api = axios.create({
baseURL: `${splatnetBaseUrl}/api/`,
headers: {
'User-Agent': userAgent,
'Cookie': `iksm_session=${process.env.NINTENDO_SESSION_ID}`,
},
});
async function getSchedules() {
let response = await api.get('schedules');
function createApiClient(sessionId) {
return axios.create({
baseURL: `${splatnetBaseUrl}/api/`,
headers: {
'User-Agent': userAgent,
'Cookie': `iksm_session=${sessionId}`,
},
});
}
const api = {
NA: createApiClient(process.env.NINTENDO_SESSION_ID_NA),
EU: createApiClient(process.env.NINTENDO_SESSION_ID_EU),
JP: createApiClient(process.env.NINTENDO_SESSION_ID_JP),
}
async function getSchedules(region = 'NA') {
let response = await api[region].get('schedules');
return response.data;
}
async function getCoopSchedules() {
let response = await api.get('coop_schedules');
async function getCoopSchedules(region = 'NA') {
let response = await api[region].get('coop_schedules');
return response.data;
}
async function getTimeline() {
let response = await api.get('timeline');
async function getTimeline(region = 'NA') {
let response = await api[region].get('timeline');
return response.data;
}
async function getNAFestivals() {
let response = await api.get('festivals/active');
async function getActiveFestivals(region = 'NA') {
let response = await api[region].get('festivals/active');
return response.data;
}
function getManualFestivals(region) {
let filePath = path.resolve('manual-festivals.json');
if (!fs.existsSync(filePath))
return { festivals: [] };
let regionalFestivals = JSON.parse(fs.readFileSync(filePath));
if (!regionalFestivals || !regionalFestivals[region])
return { festivals: [] };
return { festivals: regionalFestivals[region] };
}
async function getEUFestivals() {
return getManualFestivals('eu');
}
async function getJPFestivals() {
return getManualFestivals('jp');
}
async function getMerchandises() {
let response = await api.get('onlineshop/merchandises');
async function getPastFestivals(region = 'NA') {
let response = await api[region].get('festivals/pasts');
return response.data;
}
async function getLeagueMatchRanking(year, month, day, hour, type = 'T', region = 'ALL') {
async function getCombinedFestivals(region = 'NA') {
let active = await getActiveFestivals(region);
let past = await getPastFestivals(region);
return {
festivals: active.festivals.concat(past.festivals),
results: past.results,
};
}
async function getMerchandises(region = 'NA') {
let response = await api[region].get('onlineshop/merchandises');
return response.data;
}
async function getLeagueMatchRanking(year, month, day, hour, type = 'T', region = 'ALL', apiRegion = 'NA') {
// Hour should be in multiples of 2, e.g., 00, 02, 04, ..., 22.
// Type should be 'T' (team) or 'P' (pair).
// Region should be 'ALL', 'JP', 'US', or 'EU'.
@ -73,13 +77,13 @@ async function getLeagueMatchRanking(year, month, day, hour, type = 'T', region
day = ('0' + day).substr(-2);
hour = ('0' + hour).substr(-2);
let response = await api.get(`league_match_ranking/${year}${month}${day}${hour}${type}/${region}`);
let response = await api[apiRegion].get(`league_match_ranking/${year}${month}${day}${hour}${type}/${region}`);
return response.data;
}
async function getResults(id = null) {
async function getResults(id = null, region = 'NA') {
let url = (id) ? `results/${id}` : 'results';
let response = await api.get(url);
let response = await api[region].get(url);
return response.data;
}
@ -92,9 +96,7 @@ module.exports = {
getSchedules,
getCoopSchedules,
getTimeline,
getNAFestivals,
getEUFestivals,
getJPFestivals,
getCombinedFestivals,
getMerchandises,
getLeagueMatchRanking,
getResults,

View File

@ -94,9 +94,9 @@ async function updateFestivals() {
filename: `${dataPath}/festivals.json`,
request: async function() {
return {
na: await splatnet.getNAFestivals(),
eu: await splatnet.getEUFestivals(),
jp: await splatnet.getJPFestivals(),
na: await splatnet.getCombinedFestivals('NA'),
eu: await splatnet.getCombinedFestivals('EU'),
jp: await splatnet.getCombinedFestivals('JP'),
};
}(),
});
@ -104,8 +104,7 @@ async function updateFestivals() {
// Download banner/stage images
if (data) {
for (let region of ['na', 'eu', 'jp']) {
let festival = data[region].festivals[0];
if (festival) {
for (let festival of data[region].festivals) {
await maybeDownloadImage(festival.images.alpha);
await maybeDownloadImage(festival.images.bravo);
await maybeDownloadImage(festival.images.panel);