Last active
August 8, 2020 23:26
-
-
Save DDynamic/f68153810f3b35469e4a81dd32b2aefc to your computer and use it in GitHub Desktop.
Politics and War GraphQL API Samples
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
const https = require('https'); | |
const options = { | |
host: 'api-test.politicsandwar.com', | |
path: '/graphql?api_key=YOURAPIKEY', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
}; | |
const body = { | |
query: ` | |
{ | |
alliances(id: [747]) { | |
id | |
bankrecs { | |
id | |
money | |
note | |
} | |
} | |
} | |
` | |
}; | |
let lastId = 12200011; // set this to the ID of the last bank transactions or 0 if you don't know it | |
setInterval(() => { | |
const req = https.request(options, (res) => { | |
let data = ""; | |
res.on('data', (chunk) => { | |
data += chunk; | |
}); | |
res.on('end', () => { | |
const bankrecs = JSON.parse(data).data.alliances[0].bankrecs; | |
const filteredBankrecs = bankrecs.filter((bankrec) => bankrec.id > lastId) | |
filteredBankrecs.forEach((bankrec) => console.log("Money transferred: $" + bankrec.money + " (" + bankrec.note + ")")); | |
if (filteredBankrecs[filteredBankrecs.length - 1]) { | |
lastId = filteredBankrecs[filteredBankrecs.length - 1].id; | |
} | |
}) | |
}); | |
req.write(JSON.stringify(body)); | |
req.end(); | |
}, 5000); |
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 onOpen() { | |
SpreadsheetApp.getUi().createMenu("Alliance Functions").addItem("Get Members", "getMembers").addToUi(); | |
} | |
function parseMember(member) { | |
return [member.id, member.nation_name, member.leader_name, member.score]; | |
} | |
function getMembers() { | |
const response = UrlFetchApp.fetch("https://api.politicsandwar.com/graphql?api_key=YOURAPIKEY", { | |
payload: { | |
query: ` | |
{ | |
nations(alliance_id: [747]) { | |
id | |
nation_name | |
leader_name | |
score | |
} | |
} | |
` | |
} | |
}); | |
const parsed = JSON.parse(response.getContentText()); | |
const members = parsed.data.nations.map(parseMember); | |
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
const sheet = spreadsheet.getSheetByName("Members"); | |
sheet.getRange("A2:D").clearContent(); | |
sheet.getRange("A2:D" + (members.length + 1)).setValues(members); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment