Last active
February 4, 2021 11:14
Salmonia for GAS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Salmonia() { | |
const iksmSessions= JSON.parse(PropertiesService.getScriptProperties().getProperty('IKSM_SESSION')) | |
const apiToken = PropertiesService.getScriptProperties().getProperty('API_TOKEN') | |
let jobNum = JSON.parse(PropertiesService.getScriptProperties().getProperty('JOB_NUM')) | |
iksmSessions.forEach(function(iksm_session, index) { | |
const present = getJobNumFromSplatNet2(iksm_session) | |
const preview = Math.max(parseInt(jobNum[index]), present - 49) | |
Logger.log(iksm_session, present, preview) | |
if(present != preview) { | |
for(let job_num = preview; job_num <= present; ++job_num) { | |
Logger.log(`${iksm_session}: ${job_num}`) | |
const result = getResultFromSplatNet2(iksm_session, job_num) | |
Utilities.sleep(5000) | |
setResultToSalmonStats(result, apiToken) | |
} | |
} else { | |
Logger.log(`${iksm_session}: No new results`) | |
} | |
jobNum[index] = present.toString() | |
PropertiesService.getScriptProperties().setProperty("JOB_NUM", JSON.stringify(jobNum)) | |
}) | |
} | |
function getJobNumFromSplatNet2(iksm_session) { | |
const url = "https://app.splatoon2.nintendo.net/api/coop_results" | |
const cookie =`iksm_session=${iksm_session}` | |
const options = { headers: { cookie }} | |
const response = UrlFetchApp.fetch(url, options); | |
if (response.getResponseCode() != 200) | |
throw new Error("Invalid/Expired iksm_session.") | |
const json = JSON.parse(response.getContentText()); | |
return Math.round(json["summary"]["card"]["job_num"]) | |
} | |
function getResultFromSplatNet2(iksm_session, job_num) { | |
const url = `https://app.splatoon2.nintendo.net/api/coop_results/${job_num}` | |
const cookie =`iksm_session=${iksm_session}` | |
const options = { headers: { cookie }} | |
return JSON.parse(UrlFetchApp.fetch(url, options).getContentText()) | |
} | |
function setResultToSalmonStats(result, apiToken) { | |
url = "https://salmon-stats-api.yuki.games/api/results" | |
const headers = { | |
"method": "post", | |
"contentType": "application/json", | |
"payload": JSON.stringify({results: [result]}), | |
"headers": {"Authorization" : `Bearer ${apiToken}`}, | |
"muteHttpExceptions": true | |
} | |
const response = UrlFetchApp.fetch(url, headers) | |
if (response.getResponseCode() != 200) | |
throw new Error("Invalid api-token.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New project
File
->Project properties
->Script properties
Edit
->Current project's trigger
Add trigger
Minutes based timer
->Every 1 minute
->Save