-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBOTIFY_ANALYSIS_LIST_URLS.gs
99 lines (88 loc) · 3.22 KB
/
BOTIFY_ANALYSIS_LIST_URLS.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
/**
* Return the requested fields of a given URL
* @param {String} apiToken Botify API token
* @param {String} username Username of the project owner
* @param {String} projectSlug Project's slug of the analysis
* @param {String} analysisSlug Analysis's slug
* @param {Range} fields Range of fields to fetch (ex A1:A4)
* @param {BQLFilter} filter [Optional] Filter of apply on urls
* @param {BQLSort} sort [Optional] Sort of apply on urls
* @param {Number} size [Optional] Number of urls to retrieve (max: 1000, default: 100)
* @param {Number} page [Optional] Number of urls to retrieve (default: 1)
* @param {Boolean} displayTotal [Optional] Display the number of urls matching the filter (default: false)
* @return {Array} The value of the fields
* @customfunction
*/
function BOTIFY_ANALYSIS_LIST_URLS(apiToken, username, projectSlug, analysisSlug, fields, filter, sort, size, page, displayTotal) {
// SUPPORT DEPRECATED format
if (typeof fields === "string") {
var tmp = filter;
filter = fields;
fields = tmp;
}
// PARAMS CHECKING
if (!apiToken) throw new Error("API Token is missing in parameters");
if (!username) throw new Error("username is missing in parameters");
if (!projectSlug) throw new Error("projectSlug is missing in parameters");
if (!analysisSlug) throw new Error("analysisSlug is missing in parameters");
if (!fields) throw new Error("fields list is missing in parameters");
if (typeof filter === "undefined") filter = "{}";
if (typeof sort === "undefined") sort = "[]";
if (typeof size === "undefined") size = 100;
if (typeof size > 1000) throw new Error("size parameter must be between 1 and 1000");
if (typeof page === "undefined") page = 1;
if (typeof displayTotal === "undefined") displayTotal = false;
var result = [];
// PREPARE INPUTS
if (fields.map) {
fields = fields[0].filter(function (v) { return !!v }); // remove empty fields
} else {
fields = [fields];
}
filter = JSON.parse(filter);
sort = JSON.parse(sort);
// FETCHING API
var apiurl = 'https://api.botify.com/v1/analyses/' + username + '/' + projectSlug + '/' + analysisSlug + '/urls?page=' + page + '&size=' + size;
var options = {
'method': 'post',
'headers': {
'Authorization': 'Token ' + apiToken,
'Content-type': 'application/json',
'X-Botify-Client': 'google-sheets',
},
'payload': JSON.stringify({
'fields': fields,
'filters': filter,
'sort': sort,
}),
};
var response = JSON.parse(UrlFetchApp.fetch(apiurl, options).getContentText());
if (displayTotal) {
result.push(['Total Urls', response.count]);
}
response.results.forEach(function(item) {
// Get requested fields
var row = [];
fields.forEach(function(field) {
var val = get(item, field);
row.push(val);
});
result.push(row);
});
if (!result.length) return "No result";
return result;
}
function get(obj, path){
path = path.split('.');
for (var i = 0; i < path.length; i++){
obj = obj[path[i]];
if (typeof obj === "undefined") {
return null;
}
};
return isObject(obj) ? JSON.stringify(obj) : obj;
};
function isObject(value) {
var type = typeof value;
return value != null && (type == 'object' || type == 'function');
}