testing
This commit is contained in:
parent
1ae871f3fb
commit
8ccfcf2d5e
154
src/index.js
154
src/index.js
|
@ -15,7 +15,7 @@ import {
|
||||||
} from 'node-html-parser';
|
} from 'node-html-parser';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
pipeline
|
pipeline
|
||||||
} from 'node:stream/promises';
|
} from 'node:stream/promises';
|
||||||
|
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
@ -33,76 +33,76 @@ client.once(Events.ClientReady, readyClient => {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function get_rain_image() {
|
async function get_rain_image() {
|
||||||
try {
|
try {
|
||||||
let radar_src = "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, {
|
const radar_image = await axios.get(radar_src, {
|
||||||
responseType: "stream"
|
responseType: "stream"
|
||||||
});
|
});
|
||||||
|
|
||||||
const radar_image_size = radar_image.headers['content-length'];
|
const radar_image_size = radar_image.headers['content-length'];
|
||||||
console.log("Got Radar Data");
|
console.log("Got Radar Data");
|
||||||
return [radar_image, radar_image_size];
|
return [radar_image, radar_image_size];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return [error];
|
return [error];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get_radar_caption() {
|
async function get_radar_caption() {
|
||||||
const response = await axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx');
|
const response = await axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx');
|
||||||
|
|
||||||
let root = parse(response.data);
|
let root = parse(response.data);
|
||||||
console.log("Got Radar Caption")
|
console.log("Got Radar Caption")
|
||||||
return root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText;
|
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.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 webhooks = await channel.fetchWebhooks();
|
||||||
const webhook = webhooks.first();
|
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();
|
||||||
|
|
||||||
await pipeline(
|
await pipeline(
|
||||||
radar_image[0].data,
|
radar_image[0].data,
|
||||||
fs.createWriteStream(curr_image_path)
|
fs.createWriteStream(curr_image_path)
|
||||||
);
|
);
|
||||||
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 date = new Date().toLocaleString('th-TH');
|
||||||
|
|
||||||
let caption = `# Raindar Update\n## ${date}\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast`
|
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: caption,
|
content: caption,
|
||||||
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
|
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
|
||||||
});
|
});
|
||||||
|
|
||||||
let message_id = message.id;
|
let message_id = message.id;
|
||||||
console.log(`Sent ${message.id}: ${message.content}`);
|
console.log(`Sent ${message.id}: ${message.content}`);
|
||||||
|
|
||||||
await webhook.send({
|
await webhook.send({
|
||||||
content: caption,
|
content: caption,
|
||||||
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"],
|
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"],
|
||||||
threadId: config.history,
|
threadId: config.history,
|
||||||
});
|
});
|
||||||
console.log(`Sent message to 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;
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
await pipeline(
|
await pipeline(
|
||||||
radar_image[0].data,
|
radar_image[0].data,
|
||||||
fs.createWriteStream(curr_image_path)
|
fs.createWriteStream(curr_image_path)
|
||||||
);
|
);
|
||||||
console.log("File Saved");
|
console.log("File Saved");
|
||||||
|
|
||||||
|
@ -111,38 +111,56 @@ client.on('ready', () => {
|
||||||
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!");
|
||||||
|
|
||||||
const delete_msg = await channel.messages.delete(message_id);
|
channel.messages
|
||||||
|
.fetch(sentMessage.id)
|
||||||
|
.then((fetchedMessage) => {
|
||||||
|
console.log('Message exists');
|
||||||
|
|
||||||
let date = new Date().toLocaleString('th-TH');
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let caption = `# Raindar Update\n## ${date}\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast`
|
let date = new Date().toLocaleString('th-TH');
|
||||||
|
|
||||||
let message = await channel.send({
|
let caption = `# Raindar Update\n## ${date}\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast`
|
||||||
content: caption,
|
|
||||||
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
|
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}`);
|
console.log(`Sent ${message.id}: ${message.content}`);
|
||||||
message_id = message.id;
|
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`)
|
||||||
|
|
||||||
|
|
||||||
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.");
|
||||||
}
|
}
|
||||||
}, 60000);
|
}, 60000);
|
||||||
})();
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on(Events.ShardError, error => {
|
client.on(Events.ShardError, error => {
|
||||||
console.log('A websocket connection encountered an error: ', error);
|
console.log('A websocket connection encountered an error: ', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('unhandledRejection', error => {
|
process.on('unhandledRejection', error => {
|
||||||
|
|
Loading…
Reference in New Issue