mirror of
https://git.roussel.pro/public-website/singapore_rain_radar.git
synced 2026-02-09 02:20:17 +01:00
make sure that init downloads exactly the last count available images
This commit is contained in:
@@ -62,21 +62,26 @@ function downloadImage(time_string, filepath) {
|
|||||||
|
|
||||||
export async function initImages(count) {
|
export async function initImages(count) {
|
||||||
let files = fs.readdirSync('images/');
|
let files = fs.readdirSync('images/');
|
||||||
for (let i = count; i >= 0; i--) {
|
let downloaded = 0;
|
||||||
|
let i = 0;
|
||||||
|
while(downloaded < count) {
|
||||||
let time_string = getTimeString(i);
|
let time_string = getTimeString(i);
|
||||||
let filename = `images/${time_string}.png`;
|
let filename = `images/${time_string}.png`;
|
||||||
if (files.includes(`${time_string}.png`)) {
|
if (files.includes(`${time_string}.png`)) {
|
||||||
|
downloaded++;
|
||||||
|
i++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await downloadImage(time_string, filename)
|
await downloadImage(time_string, filename);
|
||||||
|
downloaded++;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
//Response 302, we've reached the latest available image
|
//Response 302, we've reached the latest available image
|
||||||
if (error.message === '302') {
|
if (error.message === '302') {
|
||||||
console.log('Could not download image after ' + time_string);
|
console.log('Could not download image ' + time_string);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { initImages, updateImages } from './downloader.mjs';
|
import { initImages, updateImages } from './downloader.mjs';
|
||||||
|
|
||||||
initImages(10).then(() => {
|
const IMAGE_COUNT = 5;
|
||||||
|
|
||||||
|
initImages(IMAGE_COUNT).then(() => {
|
||||||
console.log('Downloaded initial images successfully');
|
console.log('Downloaded initial images successfully');
|
||||||
});
|
});
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
updateImages(10).then(() => {
|
updateImages(IMAGE_COUNT).then(() => {
|
||||||
console.log('Updated images successfully');
|
console.log('Updated images successfully');
|
||||||
});
|
});
|
||||||
}, 5 * 60 * 1000);
|
}, 5 * 60 * 1000);
|
||||||
|
|||||||
Reference in New Issue
Block a user