mirror of
https://git.roussel.pro/public-website/singapore_rain_radar.git
synced 2026-02-09 02:20:17 +01:00
updated image downloader to accept 3XX codes
This commit is contained in:
@@ -28,24 +28,28 @@ function downloadImage(time_string, filepath) {
|
||||
}
|
||||
client.get(options, (res) => {
|
||||
console.log(`Downloading ${time_string}`);
|
||||
if (res.statusCode === 200) {
|
||||
if (res.statusCode >= 200 && res.statusCode < 400) {
|
||||
res.pipe(fs.createWriteStream(filepath))
|
||||
.on('error', reject)
|
||||
.once('close', () => resolve(filepath));
|
||||
} else {
|
||||
// Consume response data to free up memory
|
||||
console.log(`Failed to download ${time_string} with status code ${res.statusCode}`);
|
||||
res.resume();
|
||||
reject(new Error(res.statusCode));
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function initImages(count) {
|
||||
let files = fs.readdirSync('images/');
|
||||
for (let i = count; i >= 0; i--) {
|
||||
let time_string = getTimeString(i);
|
||||
let filename = `images/${time_string}.png`;
|
||||
if (files.includes(`${time_string}.png`)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
await downloadImage(time_string, filename)
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user