This repository has been archived by the owner on Aug 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEvepriasal.gs
140 lines (122 loc) · 5.01 KB
/
Evepriasal.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
/*====================================================================================================================================*
Evepraisal Tools by Kevin McDonald
====================================================================================================================================
Version: 1.1.0
Project Page: https://github.com/evepraisal/evepraisal-google-sheets
Copyright: (c) 2018 by Kevin McDonald
------------------------------------------------------------------------------------------------------------------------------------
A library for importing evepraisal data into Google spreadsheets. Functions include:
EVEPRAISAL_TOTAL For getting totals of an existing appraisal
EVEPRAISAL_ITEM For getting the current price of an item
For bug reports see https://github.com/evepraisal/evepraisal-google-sheets
------------------------------------------------------------------------------------------------------------------------------------
Changelog:
1.0.0 Initial release
1.1.0 Handle urlfetch limit more gracefully by trying to return a long-term cache copy.
Allow names for EVEPRAISAL_ITEM as well as type IDs.
*====================================================================================================================================*/
/**
* @fileoverview Provides the custom functions EVEPRAISAL_ITEM and EVEPRAISAL_TOTAL
* @OnlyCurrentDoc
*/
function onInstall() {
onOpen();
}
// This method adds a custom menu item to run the script
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu('Evepraisal',
[{ name: 'Use in this spreadsheet', functionName: 'use' }]);
}
/**
* Show available functions
*/
function use() {
var title = 'Evepraisal Functions';
var message = 'The EVEPRAISAL_TOTAL and EVEPRAISAL_ITEM functions are now available in ' +
'this spreadsheet. More information is available in the function help ' +
'box that appears when you start using them in a forumula.';
var ui = SpreadsheetApp.getUi();
ui.alert(title, message, ui.ButtonSet.OK);
}
function fetchUrl(url, timeout, longTimeout) {
if (timeout == null) {
timeout = 300;
}
var cache = CacheService.getScriptCache();
var cached = cache.get(url);
if (cached != null) {
return cached;
}
try {
var jsondata = UrlFetchApp.fetch(url).getContentText();
cache.put(url, jsondata, timeout);
if (longTimeout != null) {
cache.put("long|" + url, jsondata, longTimeout);
}
return jsondata;
} catch (ex) {
// If we hit our UrlFetchApp limit then try to pull from the long cache
if ((/Service invoked too many times for one day/ig).test(ex.toString())) {
var cached = cache.get("long|" + url);
if (cached != null) {
return cached;
}
}
throw ex;
}
}
/**
* Imports the total buy or sell price of an Evepraisal given the appraisal id. For example: =EVEPRAISAL_TOTAL("gp5av", "buy")
*
* @param {string} appraisal_id the alphanumeric id of the appraisal. E.G. "gp5av".
* @param {string} order_type The options are: "buy" or "sell". The default is "sell".
* @return a single value.
* @customfunction
**/
function EVEPRAISAL_TOTAL(appraisal_id, order_type) {
if (appraisal_id == null) {
throw "required parameter 'appraisal_id' not given"
}
if (order_type == null) {
order_type = "sell";
}
var jsondata = fetchUrl("https://evepraisal.com/a/" + encodeURIComponent(appraisal_id) + ".json", 86400, null);
var object = JSON.parse(jsondata);
return object["totals"][order_type];
}
/**
* Imports the price of an Eve Online item from Evepraisal by item id.
* @param {number} item_id the numeric id or name of an eve online item. This ID is shown when searching for an item on Evepraisal. E.G. 587 for rifter.
* @param {string} market the market to price an item in. Options are "universe", "jita", "amarr", "dodixie", "hek", "rens". The default is "jita".
* @param {string} order_type the order type. The options are: "buy" or "sell". The default is "sell".
* @param {string} attribute the attribute to use when . Options are "avg", "max", "median", "min", "percentile", "stddev", "volume", "order_count". The default is "min" for sell and "max" for buy.
* @return {number}
* @customfunction
**/
function EVEPRAISAL_ITEM(item_id, market, order_type, attribute) {
if (item_id == null) {
item_id = 34; // because why not show some tritanium as a default
}
if (market == null) {
market = "jita";
}
if (order_type == null) {
order_type = "sell";
}
if (attribute == null) {
if (order_type == "sell") {
attribute = "min";
} else {
attribute = "max"
}
}
var jsondata = fetchUrl("https://evepraisal.com/item/" + encodeURIComponent(item_id) + ".json", 300, 86400);
var object = JSON.parse(jsondata);
for (i in object["summaries"]) {
if (object["summaries"][i]["market_name"] == market) {
return object["summaries"][i]["prices"][order_type][attribute];
}
}
throw "market "+market+" not found";
}