-
Notifications
You must be signed in to change notification settings - Fork 0
/
getphotos.js
75 lines (63 loc) · 1.98 KB
/
getphotos.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
import fs from 'fs';
import http from 'http';
import https from 'https';
import sharp from 'sharp';
/**
* Downloads file from remote HTTP[S] host and puts its contents to the
* specified location.
*/
async function download(url, filePath) {
const proto = !url.charAt(4).localeCompare('s') ? https : http;
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
let fileInfo = null;
const request = proto.get(url, response => {
if (response.statusCode !== 200) {
fs.unlink(filePath, () => {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
});
return;
}
fileInfo = {
mime: response.headers['content-type'],
size: parseInt(response.headers['content-length'], 10),
};
response.pipe(file);
});
// The destination stream is ended by the time it's called
file.on('finish', () => resolve(fileInfo));
request.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
file.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
request.end();
});
}
let wards = JSON.parse(
fs.readFileSync('./public/councillors.json')
);
wards.forEach(ward => {
ward.councillors.forEach(councillor => {
const url = councillor.photo;
const filename = councillor.name.replace(/[^a-z0-9]/gi, '-').toLowerCase() + '.jpg';
councillor.photo = 'images/' + filename;
download(url, './src/photos/' + filename)
.then(() => {
const image = sharp('./src/photos/' + filename);
image
.metadata()
.then(function(metadata) {
const size = Math.min(metadata.width, metadata.height);
return image.resize({
width: size,
height: size,
fit: 'cover'
})
.toFile('./public/images/' + filename);
});
})
})
});
fs.writeFileSync('public/councillors.json', JSON.stringify(wards));