-
Notifications
You must be signed in to change notification settings - Fork 3
/
FuzWorks.JS
416 lines (361 loc) · 12.4 KB
/
FuzWorks.JS
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//JITA SELL
function testfuzAPI()
{
let ids = [
16239,
34
];
return fuzzApiPriceDataJitaSell(ids);
}
/**
* Generic API function to get a list of minimal prices for an array of type_id's
* @param {range} A vertical range of type_ids.
* @param {market_hub_id} market region ID, Defaults to Jita:60003760
* @param {order_type} sell or buy
* @param {order_level} min,max,average,mean
* @return minSell for each type_id. This can be configured differently.
* @customfunction
* Author: unknown
* Modified by CJ Kilman 11/19/2021 Added Configuration options and a Safe request buffer to avoide overloading the service get requesgt method
*/
function fuzzApiPriceDataJitaSell(type_ids, market_hub = 60003760, order_type = null ,order_level=null)
{
if (!type_ids) throw 'type_ids is required';
if(!Array.isArray(type_ids)) type_ids = [type_ids];
type_ids = type_ids.filter(Number) ;
if(order_type == null && order_level == null){
order_type = "sell";
order_level = "min";
}
if(order_type == null && order_level.toLowerCase() == "max") order_type = "buy";
if(order_type == null && order_level.toLowerCase() == "min") order_type= "sell";
if(order_type.toLowerCase() == "buy" && order_level == null) order_level = "max";
if(order_type.toLowerCase() == "sell" && order_level == null) order_level = "min";
order_type = order_type.toLowerCase(); order_level = order_level.toLowerCase();
let price_data;
var result=[];
// Capture overflow buffer
price_data = postFetch(type_ids,market_hub,"station")
for(var i=0 ; i < type_ids.length ; i++)
{
try
{
let value = parseFloat(price_data[type_ids[i]][order_type][order_level]);
if(isNumber(value))
result = result.concat(value);
else
result = result.concat("");
}
catch(error) // Value not on market, Leave Blank cell
{
result = result.concat("");
}
}
return result;
}
/**
* Fuzz market API for the given types
*
* @param {range} range A vertical range of type_ids.
* @param {string} string Jita, Amarr, Dodixie, Rens, Hek, Defaults to Jita.
* @param {string} string sell or buy. Defaults to sell.
* @param {string} string min, max, or avg. Defaults to min.
* @return result for each type_id. This can be configured differently.
* @customfunction
* Author: unknown
* Modified by CJ Kilman 11/19/2021 Added configuration options and a Safe request buffer to avoid overloading the service get request method
* Modified by Snowdevil / Highfly Chastot 12/16/2021 Added functionality for choosing hub, type, and level. Little refactoring, could use more.
*/
function fuzzPriceDataByHub(type_ids, market_hub = "Jita", order_type = "sell", order_level = null)
{
// Safety net
if (!type_ids) throw 'type_ids is required';
// Select hub ID, can ONLY use major trade hubs with this API
try{
market_hub = market_hub.toLowerCase();
}catch{}
switch (market_hub) {
case 'amarr':
market_hub = 60008494;
break;
case 'dodixie':
market_hub = 60011866;
break;
case 'rens':
market_hub = 60004588;
break;
case 'hek':
market_hub = 60005686;
break;
case 'jita':
market_hub = 60003760;
break;
default:
}
//deal with defaults on most used order types
if(order_level==null)
{
switch(order_type.toLowerCase()){
case 'buy':
order_level = "max";
break;
case 'sell':
default:
order_level= "min";
}
}
// result
return fuzzApiPriceDataJitaSell(type_ids,market_hub,order_type,order_level);
}
/**
* MarketStat API for the given types
*
* @param {range} range A vertical range of type_ids.
* @param {string} string location type Market Range. Aceptable values Station, System, Region
* @param {string} string location Id
* @param {string} string sell or buy. Defaults to sell.
* @param {string} string min, max, or avg. Defaults to min.
* @return result for each type_id. This can be configured differently.
* @customfunction
*/
function marketStatData(type_ids, location_type, location_id, order_type = null, order_level = null) {
if (!type_ids) throw 'type_ids is required';
if(!Array.isArray(type_ids)) type_ids = [type_ids];
type_ids = type_ids.filter(Number) ;
if(order_type == null && order_level == null){
order_type = "sell";
order_level = "min";
}
if(order_type == null && order_level.toLowerCase() == "max") order_type = "buy";
if(order_type == null && order_level.toLowerCase() == "min") order_type= "sell";
if(order_type.toLowerCase() == "buy" && order_level == null) order_level = "max";
if(order_type.toLowerCase() == "sell" && order_level == null) order_level = "min";
order_type = order_type.toLowerCase(); order_level = order_level.toLowerCase();
// Configuration Section
switch(location_type.toLowerCase()){
case "region":
case "system":
case "station":
location_type = location_type.toLowerCase();
break;
default:
throw new Error("Location Undefined");
}
let price_data;
var result=[];
// Capture overflow buffer
price_data = postFetch(type_ids,location_id,location_type)
for(var i=0 ; i < type_ids.length ; i++)
{
try
{
let value = parseFloat(price_data[type_ids[i]][order_type][order_level]);
if(isNumber(value))
result = result.concat(value);
else
result = result.concat("");
}
catch(error) // Value not on market, Leave Blank cell
{
result = result.concat("");
}
}
return result;
}
/**
*Pull Prce data from Fuzworks using Post request method
*
* @param {*} type_ids
* @param {*} location_id
* @param {string} [location_type="station"] station, region, or system
* @return {price_data} array
*/
function postFetch(type_ids,location_id,location_type="station")
{
if (!type_ids) throw 'type_ids is required';
if(!Array.isArray(type_ids)) type_ids = [type_ids];
type_ids = type_ids.filter(Number) ;
/*
* Configuration Section
*/
if(location_type !='region' && location_type !='station' && location_type !='system')
throw new Error("Invalid location_type, must be either 'region' or 'station' or 'system'")
const service_url = "https://market.fuzzwork.co.uk/aggregates/";
let data ={};
switch(location_type)
{
case "system":
{
data = {
"system" : location_id,
"types" : type_ids.join(",")
};
break;
}
case "region":
{
data = {
"region" : location_id,
"types" : type_ids.join(",")
};
break;
}
case "station" :
{
data = {
"station" : location_id,
"types" : type_ids.join(",")
};
break;
}
default:
throw new Error("Invalid location_type.")
}
const payload = JSON.stringify(data);
const options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : payload
};
var response = UrlFetchApp.fetch(service_url, options);
if(response.getResponseCode() != 200)
throw new Error("Fuzworks broke with error code:" + response.getResponseCode())
return JSON.parse(response.getContentText());
}
/**
* All current Market Stats API for the given types
*
* @param {range} range A vertical range of type_ids.
* @param {string} string location type Market Range. Aceptable values Station, System, Region
* @param {string} string location Id
* @param {string} show_colums Show column headers. default:true
* @return result for each type_id.
* @customfunction
*/
function allMarketStats(type_ids, location_type, location_id,show_colums=true) {
if (!type_ids) throw 'type_ids is required';
if(!Array.isArray(type_ids)) type_ids = [type_ids];
type_ids = type_ids.filter(Number) ;
// Configuration Section
switch(location_type.toLowerCase()){
case "region":
case "system":
case "station":
location_type = location_type.toLowerCase();
break;
default:
throw new Error("Location Undefined");
}
let price_data;
var result=[[]];
if(show_colums)
result=[[
"buy_weightedAverage",
"buy_max",
"buy_min",
"buy_stddev",
"buy_median",
"buy_volume",
"buy_order_count",
"buy_percentile",
"sell_weightedAverage",
"sell_max",
"sell_min",
"sell_stddev",
"sell_median",
"sell_volume",
"sell_order_count",
"sell_percentile"
]];
// Capture overflow buffer
price_data = postFetch(type_ids,location_id,location_type);
for(var i=0 ; i < type_ids.length ; i++)
{
var row =[];
let value = "";
let order_type = "buy";
value = parseFloat(price_data[type_ids[i]][order_type]["weightedAverage"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["max"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["min"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["stddev"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["median"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["volume"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseInt(price_data[type_ids[i]][order_type]["orderCount"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["percentile"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
order_type = "sell";
value = parseFloat(price_data[type_ids[i]][order_type]["weightedAverage"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["max"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["min"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["stddev"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["median"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["volume"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseInt(price_data[type_ids[i]][order_type]["orderCount"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
value = parseFloat(price_data[type_ids[i]][order_type]["percentile"]);
if(isNumber(value))
row = row.concat(value);
else
row = row.concat("");
result = result.concat([row]);
}
return result;
}