Skip to content

Instantly share code, notes, and snippets.

@tkgstrator
Last active February 4, 2021 11:14
Salmonia for GAS
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.")
}
@tkgstrator
Copy link
Author

tkgstrator commented Sep 24, 2020

  1. Get iksm_session and api_token using by Salmonia
  2. Open GAS and New project
  3. Copy and paste the above script
  4. Save it
  5. Open File -> Project properties -> Script properties
  6. Set the value of properties
Property Value
IKSM_SESSION ["YOUR IKSM_SESSION"]
API_TOKEN YOUR API_TOKEN
JOB_NUM ["0"]
  1. Open Edit -> Current project's trigger
  2. Click Add trigger
  3. Minutes based timer -> Every 1 minute -> Save
  4. Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment