diff --git a/data/current_radar.gif b/data/current_radar.gif new file mode 100644 index 0000000..a45d539 Binary files /dev/null and b/data/current_radar.gif differ diff --git a/data/current_radar.jpg b/data/current_radar.jpg index 7c8aa35..9a814e3 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 c402473..3155d69 100644 --- a/src/index.js +++ b/src/index.js @@ -32,7 +32,7 @@ client.once(Events.ClientReady, readyClient => { async function get_rain_image() { try { - const radar_image = await axios.get('https://weather.bangkok.go.th/Images/Radar/radarh.jpg', { + const radar_image = await axios.get('https://weather.bangkok.go.th/Images/Radar/radar.gif', { responseType: "stream" }); @@ -53,7 +53,7 @@ async function get_radar_caption() { return root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText; } -const curr_image_path = path.resolve("data", "current_radar.jpg"); +const curr_image_path = path.resolve("data", "current_radar.gif"); client.on('ready', () => { (async () => { @@ -69,12 +69,21 @@ client.on('ready', () => { console.log("File Saved"); console.log("Updating on boot"); + + let date = new Date().toISOString(). + replace(/T/, ' '). // replace T with a space + replace(/\..+/, '') + + let caption = `# Weather Update\n## ${date}\n${radar_caption}` + let message = await channel.send({ - content: radar_caption, + content: caption, files: [curr_image_path] }); - console.log(`Sent: ${message.content}`); - + + let message_id = message.id; + console.log(`Sent ${message.id}: ${message.content}`); + setInterval(async () => { let curr_image_size = fs.statSync(curr_image_path).size; @@ -88,16 +97,30 @@ client.on('ready', () => { 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) { console.log("File change detected!"); - let message = await channel.send({ - content: radar_caption, - files: [curr_image_path] - }); - console.log(`Sent: ${message.content}`); + + await channel.messages.delete(message_id); + console.log("Delete old message"); + + let date = new Date().toISOString(). + replace(/T/, ' '). // replace T with a space + replace(/\..+/, '') + + let caption = `# Weather Update\n## ${date} Asia/Bangkok\n${radar_caption}` + + let message = await channel.send({ + content: caption, + files: [curr_image_path] + }); + + console.log(`Sent ${message.id}: ${message.content}`); + message_id = message.id; + console.log(message_id); } else { console.log("No change in data, go on."); } diff --git a/test.js b/test.js deleted file mode 100644 index 7d19a71..0000000 --- a/test.js +++ /dev/null @@ -1,28 +0,0 @@ -import axios from "axios"; -import { parse } from 'node-html-parser'; - -axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx') - .then(function(response) { - // console.log(response); - - const root = parse(response.data); - - //console.log(root.firstChild.structure); - // ul#list - // li - // #text - - console.log(root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText); - // { tagName: 'ul', - // rawAttrs: 'id="list"', - // childNodes: - // [ { tagName: 'li', - // rawAttrs: '', - // childNodes: [Object], - // classNames: [] } ], - // id: 'list', - // classNames: [] } - //console.log(root.toString()); - }) - -//(.*?)<\/span>