diff --git a/generate.mjs b/generate.mjs index 29240ce..4b0cadf 100644 --- a/generate.mjs +++ b/generate.mjs @@ -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) {