Skip to content

Instantly share code, notes, and snippets.

@DDynamic
Last active August 8, 2020 23:26
Show Gist options
  • Save DDynamic/f68153810f3b35469e4a81dd32b2aefc to your computer and use it in GitHub Desktop.
Save DDynamic/f68153810f3b35469e4a81dd32b2aefc to your computer and use it in GitHub Desktop.
Politics and War GraphQL API Samples
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);
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