-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathabout-locations-parser.js
135 lines (110 loc) · 3.25 KB
/
about-locations-parser.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
/*
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
function extractLocations(headerRow) {
const headerCells = headerRow.querySelectorAll("th");
const locations = [];
for (const cell of headerCells) {
const text = cell.textContent.trim();
if(text != "Products") {
// extract "europe-west1" from "Belgium (europe-west1) Low CO2"
const match = text.match(/\((.*)\)/);
if(match) {
const region = match[1];
locations.push(region);
} else {
console.error(`Cannot extract region for ${text}`);
}
}
}
return locations;
}
function extractProductAvailability(row) {
const tHeader = row.querySelector("th");
if (!tHeader) {
console.error('Cannot extract product name');
return;
}
let product = tHeader.textContent.trim();
// if product ends with a digit, remove it. (These are footnotes)
while(product.match(/.*\d/) || product.match(/.*,/)) {
product = product.slice(0, -1).trim();
}
const availability = [];
const cells = row.querySelectorAll("td");
if(cells.length === 0) {
return;
}
for (const cell of cells) {
// check if cell contains a <span> with aria-label="available"
const available = cell.querySelector("span[aria-label='available']");
if (available) {
availability.push(true);
} else {
availability.push(false);
}
}
return {product, availability};
}
function extractDataForContinent(continentDOM, data) {
const rows = continentDOM.querySelectorAll("tr");
const locations = extractLocations(rows[0]);
for (let i = 1; i < rows.length; i++) {
const productAvailability = extractProductAvailability(rows[i]);
if(productAvailability) {
let {product, availability} = productAvailability;
if(!data[product]) {
data[product] = {};
}
for (let l = 0; l < locations.length; l++) {
data[product][locations[l]] = availability[l];
}
}
}
}
/*
return:
{
"product1": {
"region1": true,
"region2": false,
...
},
...
}
*/
function parseAboutLocations(document) {
const data = {};
const continents = document.querySelectorAll(".cloud-table");
// The last tab is "multi region"
for (let c = 0; c < continents.length - 1; c++) {
extractDataForContinent(continents[c], data);
}
return data;
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function getLocationsJSONFile() {
const parsed = parseAboutLocations(document);
console.log(parsed);
const json = JSON.stringify(parsed, null, 2);
//console.log(json);
download("products.json", json);
}
getLocationsJSONFile();