-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
118 lines (101 loc) · 3.15 KB
/
application.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
const rp = require('request-promise-native');
const fs = require('fs');
var directory_url = "https://spaceapi.fixme.ch/directory.json";
function spaceinfoToGeojsonItem(spaceinfo) {
var name = "Unknown";
if (typeof spaceinfo.space !== undefined && spaceinfo.space) {
name = spaceinfo.space;
}
var url = "";
if (typeof spaceinfo.url !== undefined && spaceinfo.url) {
url = spaceinfo.url;
}
var email = "";
if (typeof spaceinfo.contact !== undefined && spaceinfo.contact) {
if (typeof spaceinfo.contact.email !== undefined && spaceinfo.contact.email) {
email = spaceinfo.contact.email;
}
}
var latitude = 0;
var longitude = 0;
var address = "";
if (typeof spaceinfo.location !== undefined && spaceinfo.location) {
if (typeof spaceinfo.location.lat !== undefined && spaceinfo.location.lat) {
latitude = spaceinfo.location.lat;
}
if (typeof spaceinfo.location.lon !== undefined && spaceinfo.location.lon) {
longitude = spaceinfo.location.lon;
}
if (typeof spaceinfo.location.address !== undefined && spaceinfo.location.address) {
address = spaceinfo.location.address;
}
}
var msg = "";
var symbol = "hs";
if (typeof spaceinfo.state !== undefined && spaceinfo.state) {
if (typeof spaceinfo.state.message !== undefined && spaceinfo.state.message) {
msg = spaceinfo.state.message;
}
if (typeof spaceinfo.state.open !== undefined) {
//console.log(spaceinfo.state.open);
if (typeof spaceinfo.state.open === 'boolean') {
if (spaceinfo.state.open) {
symbol = "hs-open";
} else {
symbol = "hs-closed";
}
} else if (typeof spaceinfo.state.open === 'string') {
if (spaceinfo.state.open=="open") {
symbol = "hs-open";
} else if (spaceinfo.state.open=="closed") {
symbol = "hs-closed";
} else {
console.log("Unknown space state string: "+spaceinfo.state.open);
}
} else {
console.log("Unknown space state type: ",typeof spaceinfo.state.open, spaceinfo.state.open);
}
}
}
var output = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [longitude, latitude]
},
"properties": {
"marker-symbol": symbol,
"name": name.replace(/(?:\r\n|\r|\n)/g, ' '),
"url": url.replace(/(?:\r\n|\r|\n)/g, ' '),
"address": address.replace(/(?:\r\n|\r|\n)/g, ' '),
"email": email.replace(/(?:\r\n|\r|\n)/g, ' '),
"description": msg.replace(/(?:\r\n|\r|\n)/g, ' ')
}
};
return output;
}
rp({uri: directory_url, json: true}).then((directory) => {
var geojson_items = [];
var promises = [];
for(var hackerspace in directory){
var hs_url = directory[hackerspace];
promises.push(rp({uri: hs_url, json: true, timeout: 2000}).then((spaceinfo) => {
geojson_items.push(spaceinfoToGeojsonItem(spaceinfo));
}).catch((error) => {
//console.log(error);
}));
}
Promise.all(promises).then( (values) => {
var geojson = {
"type": "FeatureCollection",
"features": geojson_items
};
fs.writeFile('output.geojson', JSON.stringify(geojson), (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
});
}).catch((error) => {
console.log("Error: Could not fetch the directory!");
console.log(error);
});