forked from AaronO/pretty-earth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 1.35 KB
/
index.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
// Requires
var fs = require('fs');
var path = require('path');
var request = require('request');
var base64 = require('base64');
// Globals
var OUT_DIR = path.join(__dirname, 'pictures');
var imageIds = require('./imageIds.json');
function writePhoto(photo) {
var filename = [
photo.geocode.country,
photo.geocode.administrative_area_level_1,
photo.id,
]
.filter(Boolean)
.map(function(str) {
return str.replace(/S+/g, '-').toLowerCase();
}).join('-') + '.jpg';
var buffer = new Buffer(photo.dataUri.split(",")[1], 'base64');
fs.writeFile(
path.join(OUT_DIR, filename),
buffer,
function(err) {
if(err) {
console.error("Failed to write", photo.id, "because of", err);
}
console.log("Wrote", photo.id);
}
);
}
imageIds.forEach(function(id) {
var url = "https://www.gstatic.com/prettyearth/"+id+".json";
var options = {
url: url,
json: true,
};
request.get(options, function(err, body, parsed) {
if(err) {
console.error("Failed to get [", id, "] because of", err);
return;
}
try {
writePhoto(parsed);
} catch(errx) {
console.error("Failed to write [", id, "] because of", errx);
return;
}
});
});