make sure that init downloads exactly the last count available images

This commit is contained in:
Quentin Roussel
2024-08-18 13:48:56 +08:00
parent 3119f41941
commit 074bcf20d7
2 changed files with 13 additions and 6 deletions

View File

@@ -62,21 +62,26 @@ function downloadImage(time_string, filepath) {
export async function initImages(count) {
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 filename = `images/${time_string}.png`;
if (files.includes(`${time_string}.png`)) {
downloaded++;
i++;
continue;
}
try {
await downloadImage(time_string, filename)
await downloadImage(time_string, filename);
downloaded++;
} catch (error) {
//Response 302, we've reached the latest available image
if (error.message === '302') {
console.log('Could not download image after ' + time_string);
return;
console.log('Could not download image ' + time_string);
}
}
i++;
}
}

View File

@@ -1,11 +1,13 @@
import { initImages, updateImages } from './downloader.mjs';
initImages(10).then(() => {
const IMAGE_COUNT = 5;
initImages(IMAGE_COUNT).then(() => {
console.log('Downloaded initial images successfully');
});
setInterval(() => {
updateImages(10).then(() => {
updateImages(IMAGE_COUNT).then(() => {
console.log('Updated images successfully');
});
}, 5 * 60 * 1000);