diff --git a/.gitignore b/.gitignore index e1c8515..269fdbe 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,4 @@ dist .pnp.* # config -config.json +config/config.json diff --git a/config.json.example b/config/config.json.example similarity index 100% rename from config.json.example rename to config/config.json.example diff --git a/data/current_radar.jpg b/data/current_radar.jpg index 89da2d0..f1a483c 100644 Binary files a/data/current_radar.jpg and b/data/current_radar.jpg differ diff --git a/src/index.js b/src/index.js index bfcb5f1..4dca5c0 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds] }); -const { config } = JSON.parse(fs.readFileSync(path.resolve("config.json"))); +const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json"))); client.once(Events.ClientReady, readyClient => { console.log(`Ready! Logged in as ${readyClient.user.tag}`); @@ -61,6 +61,7 @@ client.on('ready', () => { let radar_image = await get_rain_image(); let radar_caption = await get_radar_caption(); + await pipeline( radar_image[0].data, fs.createWriteStream(curr_image_path) @@ -74,57 +75,33 @@ client.on('ready', () => { }); console.log(`Sent: ${message.content}`); - /* setInterval(() => { + setInterval(async () => { let curr_image_size = fs.statSync(curr_image_path).size; - let fetched_image_size = await get_rain_image(curr_) - }, 5000); */ + + let radar_image = await get_rain_image(); + let radar_caption = await get_radar_caption(); + + await pipeline( + radar_image[0].data, + fs.createWriteStream(curr_image_path) + ); + console.log("File Saved"); + + let radar_image_size = radar_image[1]; + console.log("Current: " + curr_image_size); + console.log("New: " + radar_image_size); + + if(radar_image_size != curr_image_size) { + let message = await channel.send({ + content: radar_caption, + files: [curr_image_path] + }); + console.log(`Sent: ${message.content}`); + } else { + console.log("No change in data, go on."); + } + }, 60000); })(); }); -/* client.on('ready', () => { - console.log("Client: on ready"); - const curr_image_path = path.resolve("data", "current_radarh.jpg"); - - // continuously check for data change - console.log("Checking for image changes"); - setInterval(() => { - console.log("File Exists"); - axios.get('https://weather.bangkok.go.th/Images/Radar/radarh.jpg', { - responseType: "stream" - }) - .then(function(response) { - let new_image_size = response.headers['content-length']; - let curr_image_size = fs.statSync(curr_image_path).size; - console.log("Current: " + curr_image_size); - console.log("New: " + new_image_size); - - if (new_image_size != curr_image_size) { - console.log("A change in data!"); - - console.log("Saving File"); - response.data.pipe(fs.createWriteStream(curr_image_path)); - - console.log("Retrieving Caption"); - axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx') - .then(function(response) { - console.log("Got Caption!"); - let root = parse(response.data); - let caption = root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText; - - const channel = client.channels.cache.get('1165160812797771858'); - channel.send({ - content: caption, - files: ["https://weather.bangkok.go.th/Images/Radar/radarh.jpg"] - }) - .then(message => console.log(`Sent message: ${message.content}`)) - .catch(console.error); - }) - } else { - console.log("No change in data, go on"); - } - }); - }, 60000); - }); -}); */ - client.login(config.token);