-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers.js
84 lines (71 loc) · 2.3 KB
/
helpers.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
/* ----------- Data processing functions ----------- */
function isISIN(id) {
// ISIN have 12 characters
// Not the most robust way (TODO: improve it)
return id.length == 12;
}
function stripCharacters_(text) {
return text.trim().replace(/\n/g, '').replace(/\t/g, '');
}
function processNav(nav) {
nav = stripCharacters_(nav);
nav = nav.replace(',', '.');
if (!isNaN(parseFloat(nav)) && isFinite(nav))
return parseFloat(nav);
else
throw new Error("NAV is not available for this asset and source. Please try another data source.");
}
function processDate(date) {
date = stripCharacters_(date);
return date;
}
function processChange(change) {
change = stripCharacters_(change);
change = change.replace(',', '.').replace('%', '');
if (!isNaN(parseFloat(change)) && isFinite(change))
return parseFloat(change)/100;
else
throw new Error("Last change is not available for this asset and source. Please try another data source.");
}
function processCurrency(currency) {
currency = stripCharacters_(currency);
return currency;
}
function processExpenses(expenses) {
expenses = stripCharacters_(expenses);
expenses = expenses.replace(',', '.').replace('%', '');
if (!isNaN(parseFloat(expenses)) && isFinite(expenses))
return parseFloat(expenses)/100;
else
throw new Error("Expenses ratio is not available for this asset and source. Please try another data source.");
}
function processCategory(category) {
category = stripCharacters_(category);
return category;
}
function processSource(source) {
source = stripCharacters_(source);
return source;
}
/* -------- Fetching cached/non-cached pages -------- */
function fetchURL(url, cacheid = null) {
const cache = CacheService.getScriptCache();
if (cacheid != null) {
const cached = cache.get(cacheid);
if (cached != null) {
return cached;
}
}
const fetch = UrlFetchApp.fetch(url);
if (fetch.getResponseCode() == 200 && fetch.getContent().length > 0) {
const body = fetch.getContentText();
const $ = Cheerio.load(body);
const trimmed = $("body").html();
if (cacheid != null) {
cache.put(cacheid, trimmed, 7200);
}
return trimmed;
} else {
throw new Error("Wrong combination of asset identifier and source. Please check the accepted ones at the documentation.");
}
}