Compare commits

...

11 Commits

Author SHA1 Message Date
Win 8ccfcf2d5e testing 2024-06-30 11:20:48 +07:00
Win 1ae871f3fb updates 2024-06-29 17:47:25 +07:00
Win 4c4db68a03 publish 2024-06-25 19:25:03 +07:00
Win d0df64b48f raindar 2024-05-30 12:47:18 +07:00
Win 0f9abdd0e1 gif and image 2 2024-05-30 12:18:06 +07:00
Win 305f0ed8fe gif and image 2024-05-30 12:17:13 +07:00
Win 4d3d4356a0 gif and image 2024-05-30 12:15:43 +07:00
Win 127b430632 removed radar gif 2024-05-30 12:05:15 +07:00
Win 5f4e00634e updated timezone config 2024-05-30 12:04:24 +07:00
Win dc9cb2a8d9 updated timezone 2024-05-30 12:03:49 +07:00
Win e9a4bfe264 updated gif 2024-05-30 11:43:09 +07:00
5 changed files with 113 additions and 81 deletions

4
.gitignore vendored
View File

@ -132,3 +132,7 @@ dist
# config # config
config/config.json config/config.json
# data
data/current_radar.gif
data/current_radar.jpg

View File

@ -1,4 +1,6 @@
{ {
"room": "", "room": "",
"token": "" "history": "",
"token": "",
"timezone": "",
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 279 KiB

View File

@ -26,13 +26,16 @@ const client = new Client({
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json"))); const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json")));
process.env.TZ = config.timezone;
client.once(Events.ClientReady, readyClient => { client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`); console.log(`Ready! Logged in as ${readyClient.user.tag}`);
}); });
async function get_rain_image() { async function get_rain_image() {
try { try {
const radar_image = await axios.get('https://weather.bangkok.go.th/Images/Radar/radarh.jpg', { let radar_src = "https://weather.bangkok.go.th/Images/Radar/radarh.jpg"
const radar_image = await axios.get(radar_src, {
responseType: "stream" responseType: "stream"
}); });
@ -58,6 +61,8 @@ const curr_image_path = path.resolve("data", "current_radar.jpg");
client.on('ready', () => { client.on('ready', () => {
(async () => { (async () => {
const channel = client.channels.cache.get(config.room); const channel = client.channels.cache.get(config.room);
const webhooks = await channel.fetchWebhooks();
const webhook = webhooks.first();
let radar_image = await get_rain_image(); let radar_image = await get_rain_image();
let radar_caption = await get_radar_caption(); let radar_caption = await get_radar_caption();
@ -69,11 +74,25 @@ client.on('ready', () => {
console.log("File Saved"); console.log("File Saved");
console.log("Updating on boot"); console.log("Updating on boot");
let date = new Date().toLocaleString('th-TH');
let caption = `# Raindar Update\n## ${date}\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast`
let message = await channel.send({ let message = await channel.send({
content: radar_caption, content: caption,
files: [curr_image_path] files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
}); });
console.log(`Sent: ${message.content}`);
let message_id = message.id;
console.log(`Sent ${message.id}: ${message.content}`);
await webhook.send({
content: caption,
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"],
threadId: config.history,
});
console.log(`Sent message to history`)
setInterval(async () => { setInterval(async () => {
let curr_image_size = fs.statSync(curr_image_path).size; let curr_image_size = fs.statSync(curr_image_path).size;
@ -88,16 +107,51 @@ client.on('ready', () => {
console.log("File Saved"); console.log("File Saved");
let radar_image_size = radar_image[1]; let radar_image_size = radar_image[1];
console.log("Current: " + curr_image_size); console.log("Current: " + curr_image_size);
console.log("New: " + radar_image_size); console.log("New: " + radar_image_size);
if(radar_image_size != curr_image_size) { if (radar_image_size != curr_image_size) {
console.log("File change detected!"); console.log("File change detected!");
let message = await channel.send({
content: radar_caption, channel.messages
files: [curr_image_path] .fetch(sentMessage.id)
.then((fetchedMessage) => {
console.log('Message exists');
fetchedMessage
.delete()
.then(() => console.log('Message deleted successfully'))
.catch((err) => console.log('Could not delete the message', err));
})
.catch((err) => {
if (err.httpStatus === 404) {
console.log('Message already deleted');
} else {
console.log(err);
}
}); });
console.log(`Sent: ${message.content}`);
let date = new Date().toLocaleString('th-TH');
let caption = `# Raindar Update\n## ${date}\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast`
let message = await channel.send({
content: caption,
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
});
console.log(`Sent ${message.id}: ${message.content}`);
message_id = message.id;
await webhook.send({
content: caption,
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"],
threadId: config.history,
});
console.log(`Sent message to history`)
} else { } else {
console.log("No change in data, go on."); console.log("No change in data, go on.");
} }

28
test.js
View File

@ -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 id="repeaDaily_lblDESCRIPTION_0">(.*?)<\/span>