Skip to content

Instantly share code, notes, and snippets.

@kitsunet
Last active January 2, 2024 08:27
Show Gist options
  • Save kitsunet/282d32e2e8f51303ada1152e9bcc940d to your computer and use it in GitHub Desktop.
Save kitsunet/282d32e2e8f51303ada1152e9bcc940d to your computer and use it in GitHub Desktop.
Some Google Docs EVE Online code
/*
see https://www.fuzzwork.co.uk/2017/03/14/using-esi-google-sheets/ for install details.
Needs work, as it reauths for each call. As it can't store the access token or expiry in the sheet.
*/
function getSetup(charid) {
var config = {};
var documentProperties = PropertiesService.getDocumentProperties();
var characterData = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('char_' + charid);
var config = {
charid: charid,
clientid: characterData.getCell(1, 3).getValue(),
secret: characterData.getCell(1, 4).getValue(),
refreshtoken: characterData.getCell(1, 5).getValue(),
expires: documentProperties.getProperty('char_' + charid + '_oauth_expires'),
access_token: documentProperties.getProperty('char_' + charid + '_access_token')
};
return config;
}
function getAccessToken(config) {
if (Date.now() > config.expires) {
var url = 'https://login.eveonline.com/oauth/token?'
+ 'grant_type=refresh_token'
+ '&refresh_token=' + config.refreshtoken;
var code = Utilities.base64Encode(config.clientid + ':' + config.secret);
var headers = {
'Authorization': 'Basic ' + code,
'Content-Type': 'application/x-www-form-urlencoded',
};
var parameters = {
'method': 'post',
'headers': headers,
};
var response = UrlFetchApp.fetch(url, parameters).getContentText();
var json = JSON.parse(response);
var access_token = json['access_token'];
config.access_token = access_token;
config.expires = Date.now() + 1200000
var documentProperties = PropertiesService.getDocumentProperties();
documentProperties.setProperty('char_' + config.charid + '_access_token', access_token)
documentProperties.setProperty('char_' + config.charid + '_oauth_expires', config.expires)
}
return config;
}
function getLp(charid) {
var config = getSetup(charid);
config = getAccessToken(config);
var url = 'https://esi.tech.ccp.is/v1/characters/' + charid + '/loyalty/points/';
var parameters = { method: "get", headers: { 'Authorization': 'Bearer ' + config.access_token } };
var jsonFeed = UrlFetchApp.fetch(url, parameters).getContentText();
var json = JSON.parse(jsonFeed);
var loyalty = [];
loyalty.push(['Corporation', 'LP'])
if (json) {
for (i in json) {
var points = [
getCorporationName(json[i].corporation_id),
json[i].loyalty_points,
];
loyalty.push(points);
}
}
return loyalty;
}
function getCorporationName(corporation_id) {
var url = 'https://esi.tech.ccp.is/latest/corporations/' + corporation_id + '/';
var jsonFeed = UrlFetchApp.fetch(url).getContentText();
var json = JSON.parse(jsonFeed);
return json.name;
}
@philihp
Copy link

philihp commented Jan 2, 2024

@kitsunet
Copy link
Author

kitsunet commented Jan 2, 2024

I am pretty sure more stuff is broken by now :D but I fixed it :)

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