From 15d5275a87466a22deb37845e224d29919170f19 Mon Sep 17 00:00:00 2001 From: Win Parker Date: Thu, 18 Jul 2024 17:27:12 +0700 Subject: [PATCH] added an hour delay --- src/index.js | 62 ++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/index.js b/src/index.js index 8c7536f..11d76c6 100644 --- a/src/index.js +++ b/src/index.js @@ -73,8 +73,6 @@ app.get('/callback', function(req, res) { access_token = credentials.token; } - let lap = 0; - let static_image_url = "https://weather.bangkok.go.th/Images/Radar/radarh.jpg"; let animated_image_url = "https://dds.bangkok.go.th/Radar/radar_rain.gif"; @@ -86,9 +84,8 @@ app.get('/callback', function(req, res) { await Fetch.save_image(static_image_url, static_radar_path); } + let post_lap = 0; async function post() { - lap += 1; - console.log(lap); let raw_date = new Date(); let curr_static_image_size = fs.statSync(static_radar_path).size; @@ -98,34 +95,43 @@ app.get('/callback', function(req, res) { console.log("Scanned size: " + fetch_static_image_size); if(fetch_static_image_size != curr_static_image_size) { - console.log("File size change detected!"); - await Fetch.save_image(static_image_url, static_radar_path); - await Fetch.save_image(animated_image_url, animated_radar_path); + if(post_lap == 0 || post_lap == 12) { // 12 * 5 = 60 + console.log("File size change detected!"); + await Fetch.save_image(static_image_url, static_radar_path); + await Fetch.save_image(animated_image_url, animated_radar_path); - // Fetch static image - let static_radar_image_name = `${raw_date.toISOString()}.jpg`; - const upload_static_radar = await Post.create_image(host, access_token, static_radar_image_name, static_radar_path); - - // Fetch animated image - let animated_radar_image_name = `${raw_date.toISOString()}.gif`; - const upload_gif_radar = await Post.create_image(host, access_token, animated_radar_image_name, animated_radar_path); + // Fetch static image + let static_radar_image_name = `${raw_date.toISOString()}.jpg`; + const upload_static_radar = await Post.create_image(host, access_token, static_radar_image_name, static_radar_path); + + // Fetch animated image + let animated_radar_image_name = `${raw_date.toISOString()}.gif`; + const upload_gif_radar = await Post.create_image(host, access_token, animated_radar_image_name, animated_radar_path); - let radar_caption = await Fetch.get_radar_caption(); - let note_caption = `$[x2 Bangkok Raindar Update]\n**${raw_date.toLocaleString('th-TH')}**\n\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast\n\n#bangkok #weather #bangkokweather` - const post_note = await Post.create_note( - host, - access_token, - note_caption, - [upload_static_radar.id, upload_gif_radar.id], - "public" - ) - - if(post_note.createdNote) { - console.log("Note created!"); + let radar_caption = await Fetch.get_radar_caption(); + let note_caption = `$[x2 Bangkok Raindar Update]\n**${raw_date.toLocaleString('th-TH')}**\n\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast\n\n#bangkok #weather #bangkokweather` + const post_note = await Post.create_note( + host, + access_token, + note_caption, + [upload_static_radar.id, upload_gif_radar.id], + "public" + ) + + if(post_note.createdNote) { + console.log("Note created!"); + post_lap++; + + if(post_lap == 12) { + post_lap = 0; + } + + } else { + console.log("Note was not created"); + } } else { - console.log("Note was not created"); + console.log("Skipping post"); } - } else { console.log("No change in data, go on."); }