Compare commits
12 Commits
Author | SHA1 | Date |
---|---|---|
Win | af6e09c200 | |
Win | 8ccfcf2d5e | |
Win | 1ae871f3fb | |
Win | 4c4db68a03 | |
Win | d0df64b48f | |
Win | 0f9abdd0e1 | |
Win | 305f0ed8fe | |
Win | 4d3d4356a0 | |
Win | 127b430632 | |
Win | 5f4e00634e | |
Win | dc9cb2a8d9 | |
Win | e9a4bfe264 |
|
@ -132,3 +132,7 @@ dist
|
|||
|
||||
# config
|
||||
config/config.json
|
||||
|
||||
# data
|
||||
data/current_radar.gif
|
||||
data/current_radar.jpg
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{
|
||||
"room": "",
|
||||
"token": ""
|
||||
"history": "",
|
||||
"token": "",
|
||||
"timezone": "",
|
||||
}
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 279 KiB |
158
src/index.js
158
src/index.js
|
@ -14,8 +14,8 @@ import {
|
|||
parse
|
||||
} from 'node-html-parser';
|
||||
|
||||
import {
|
||||
pipeline
|
||||
import {
|
||||
pipeline
|
||||
} from 'node:stream/promises';
|
||||
|
||||
import path from 'node:path';
|
||||
|
@ -26,91 +26,145 @@ const client = new Client({
|
|||
|
||||
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json")));
|
||||
|
||||
process.env.TZ = config.timezone;
|
||||
|
||||
client.once(Events.ClientReady, readyClient => {
|
||||
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
||||
});
|
||||
|
||||
async function get_rain_image() {
|
||||
try {
|
||||
const radar_image = await axios.get('https://weather.bangkok.go.th/Images/Radar/radarh.jpg', {
|
||||
responseType: "stream"
|
||||
});
|
||||
|
||||
const radar_image_size = radar_image.headers['content-length'];
|
||||
console.log("Got Radar Data");
|
||||
return [radar_image, radar_image_size];
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return [error];
|
||||
}
|
||||
try {
|
||||
let radar_src = "https://weather.bangkok.go.th/Images/Radar/radarh.jpg"
|
||||
const radar_image = await axios.get(radar_src, {
|
||||
responseType: "stream"
|
||||
});
|
||||
|
||||
const radar_image_size = radar_image.headers['content-length'];
|
||||
console.log("Got Radar Data");
|
||||
return [radar_image, radar_image_size];
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return [error];
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
console.log("Got Radar Caption")
|
||||
return root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText;
|
||||
let root = parse(response.data);
|
||||
console.log("Got Radar Caption")
|
||||
return root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText;
|
||||
}
|
||||
|
||||
const curr_image_path = path.resolve("data", "current_radar.jpg");
|
||||
|
||||
client.on('ready', () => {
|
||||
(async () => {
|
||||
const channel = client.channels.cache.get(config.room);
|
||||
|
||||
let radar_image = await get_rain_image();
|
||||
let radar_caption = await get_radar_caption();
|
||||
(async () => {
|
||||
const channel = client.channels.cache.get(config.room);
|
||||
const webhooks = await channel.fetchWebhooks();
|
||||
const webhook = webhooks.first();
|
||||
|
||||
await pipeline(
|
||||
radar_image[0].data,
|
||||
fs.createWriteStream(curr_image_path)
|
||||
);
|
||||
console.log("File Saved");
|
||||
|
||||
console.log("Updating on boot");
|
||||
let message = await channel.send({
|
||||
content: radar_caption,
|
||||
files: [curr_image_path]
|
||||
});
|
||||
console.log(`Sent: ${message.content}`);
|
||||
|
||||
setInterval(async () => {
|
||||
let curr_image_size = fs.statSync(curr_image_path).size;
|
||||
let radar_image = await get_rain_image();
|
||||
let radar_caption = await get_radar_caption();
|
||||
|
||||
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");
|
||||
|
||||
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({
|
||||
content: caption,
|
||||
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
|
||||
});
|
||||
|
||||
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 () => {
|
||||
let curr_image_size = fs.statSync(curr_image_path).size;
|
||||
|
||||
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)
|
||||
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) {
|
||||
console.log("File change detected!");
|
||||
|
||||
if (radar_image_size != curr_image_size) {
|
||||
console.log("File change detected!");
|
||||
|
||||
channel.messages
|
||||
.fetch(message_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);
|
||||
}
|
||||
});
|
||||
|
||||
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: radar_caption,
|
||||
files: [curr_image_path]
|
||||
content: caption,
|
||||
files: [curr_image_path, "https://dds.bangkok.go.th/Radar/radar_rain.gif"]
|
||||
});
|
||||
console.log(`Sent: ${message.content}`);
|
||||
|
||||
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 {
|
||||
console.log("No change in data, go on.");
|
||||
}
|
||||
}, 60000);
|
||||
})();
|
||||
}, 60000);
|
||||
})();
|
||||
});
|
||||
|
||||
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 => {
|
||||
console.error(error);
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
client.login(config.token);
|
||||
|
|
28
test.js
28
test.js
|
@ -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>
|
Loading…
Reference in New Issue