diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c49c3e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +images/*.png \ No newline at end of file diff --git a/bg.png b/bg.png new file mode 100644 index 0000000..78cbd8e Binary files /dev/null and b/bg.png differ diff --git a/generate.mjs b/generate.mjs index 5e73492..29240ce 100644 --- a/generate.mjs +++ b/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 - return; + 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}`); + } } \ No newline at end of file diff --git a/images/.gitignore b/images/.gitignore deleted file mode 100644 index aab52d9..0000000 --- a/images/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.png \ No newline at end of file diff --git a/index.mjs b/index.mjs index c7cceff..a19f75f 100644 --- a/index.mjs +++ b/index.mjs @@ -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);