This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.gs
159 lines (140 loc) · 3.91 KB
/
code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function apiFetch(cacheKey, key, url){
var cache = CacheService.getUserCache();
cacheKey = key + cacheKey;
var data = cache.get(cacheKey);
if(!data){
var opt = null;
if(key){
opt = {
'headers': {
'Authorization': 'Bearer ' + key
}
};
}
data = UrlFetchApp.fetch('https://api.guildwars2.com/v2/' + url, opt);
data = data.getContentText();
cache.put(cacheKey, data);
}
return JSON.parse(data);
}
function publicApiFetch(cacheKey, url, prop){
var cache = CacheService.getScriptCache();
var data = cache.get(cacheKey);
if(!data){
data = UrlFetchApp.fetch('https://api.guildwars2.com/v1/' + url, prop);
data = data.getContentText();
cache.put(cacheKey, data);
}
return JSON.parse(data);
}
function getCharacterInfo(key, name){
return apiFetch('toon-' + name, key, 'characters/' + name);
}
function getCharacterCraft(key, name, craft){
var character = getCharacterInfo(key, name);
craft = craft.toLowerCase();
for(var i = 0; i < character.crafting.length; i++){
var skill = character.crafting[i];
if(skill.discipline.toLowerCase() == craft){
return skill;
}
}
return null;
}
/**
* Get crafting level of a character
*
* @param {String} key API Key
* @param {String} name Character name
* @param {String} craft Crafting name (armorsmith/artificer/chef/huntsman/jeweler/leatherworker/tailor/weaponsmith/scribe)
* @return {Number} Current level of that craft, or 0
* @customfunction
*/
function gw2Craft(key, name, craft){
var crafts = ['armorsmith', 'artificer', 'chef', 'huntsman', 'jeweler', 'leatherworker', 'tailor', 'weaponsmith', 'scribe'];
if(craft == null){
var out = crafts.map(function(skill){
var result = gw2Craft(key, name, skill);
if(result == 0){
return '';
}
return result;
});
return [out];
}
var craft = getCharacterCraft(key, name, craft);
return craft ? craft.rating : 0;
}
/**
* Get character prop
*
* @param {String} key API Key
* @param {String} name Character name
* @param {String} prop Property name (name/race/gender/profession/level/deaths)
* @customfunction
*/
function gw2Prop(key, name, prop){
var character = getCharacterInfo(key, name);
return character ? character[prop] : 0;
}
/**
* Get character prop
*
* @param {String} key API Key
* @param {String} name Character name
* @return {Date} Birthday
* @customfunction
*/
function gw2Birthday(key, name){
var character = getCharacterInfo(key, name);
var dates = character.created.replace(/T.*$/, '').split('-');
return character ? new Date(parseInt(dates[0], 10), parseInt(dates[1], 10) - 1, parseInt(dates[2], 10)) : null;
}
/**
* Get character list
*
* @customfunction
*/
function gw2Characters(key){
return apiFetch('toons', key, 'characters').sort();
}
/**
* Get guild name
*
* @param {String} id Guild ID (can be retrieved by gw2Prop(.., .., 'guild')
* @return {String} Guild name
* @customfunction
*/
function gw2GuildName(id){
var guild = publicApiFetch('guild-' + id, 'guild_details?guild_id=' + id, {"muteHttpExceptions": true});
return guild ? guild.guild_name : null;
}
/**
* Get guild tag
*
* @param {String} id Guild ID (can be retrieved by gw2Prop(.., .., 'guild')
* @return {String} Guild tag
* @customfunction
*/
function gw2GuildTag(id){
var guild = publicApiFetch('guild-' + id, 'guild_details?guild_id=' + id, {"muteHttpExceptions": true});
return guild ? guild.tag : null;
}
/**
* Get gw2w2w embed link for guild logo
*
* @param {String} name Full guild name
* @param {Integer} size Optional. Image size. Default is 256
* @return {String} Image url for use with =image(..)
* @customfunction
*/
function gw2GuildLogo(name, size){
if(!size){
size = 256;
}
if(!name){
return null;
}
name = name.replace(/ /g, '-').toLowerCase();
return 'http://guilds.gw2w2w.com/guilds/' + name + '/' + size + '.svg';
}