mirror of
https://git.roussel.pro/public-website/singapore_rain_radar.git
synced 2026-02-09 02:20:17 +01:00
added function to update latest images
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
images/*.png
|
||||
34
generate.mjs
34
generate.mjs
@@ -1,3 +1,4 @@
|
||||
import { time } from 'console';
|
||||
import fs from 'fs';
|
||||
import client from 'https';
|
||||
|
||||
@@ -34,7 +35,7 @@ function downloadImage(time_string, filepath) {
|
||||
} else {
|
||||
// Consume response data to free up memory
|
||||
res.resume();
|
||||
reject(new Error(`Request Failed With a Status Code: ${res.statusCode}`));
|
||||
reject(new Error(res.statusCode));
|
||||
|
||||
}
|
||||
});
|
||||
@@ -49,7 +50,38 @@ export async function initImages(count) {
|
||||
await downloadImage(time_string, filename)
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateImages(count) {
|
||||
let time_strings = [];
|
||||
for(let i = count; i >= 0 ; i--) {
|
||||
time_strings.push(getTimeString(i));
|
||||
}
|
||||
let files = fs.readdirSync('images/');
|
||||
let image_count = files.length;
|
||||
|
||||
//Try to download new images
|
||||
for(let time_string of time_strings) {
|
||||
if (!files.includes(`${time_string}.png`)) {
|
||||
try {
|
||||
await downloadImage(time_string, `images/${time_string}.png`);
|
||||
image_count++;
|
||||
} catch (error) {
|
||||
console.log("No more images available");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Remove old files
|
||||
for(let file of files.sort().slice(0, image_count-count)) {
|
||||
console.log(`Deleting ${file}`);
|
||||
fs.unlinkSync(`images/${file}`);
|
||||
}
|
||||
}
|
||||
1
images/.gitignore
vendored
1
images/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
*.png
|
||||
@@ -1,6 +1,11 @@
|
||||
import { initImages } from './generate.mjs';
|
||||
import { initImages, updateImages } from './generate.mjs';
|
||||
|
||||
initImages(10).then(() => {
|
||||
console.log('Downloaded images successfully');
|
||||
console.log('Downloaded initial images successfully');
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
updateImages(10).then(() => {
|
||||
console.log('Updated images successfully');
|
||||
});
|
||||
}, 5 * 60 * 1000);
|
||||
|
||||
Reference in New Issue
Block a user