Log_service/app/utils/powerSchedule.js

76 lines
2.6 KiB
JavaScript

const puppeteer = require("puppeteer");
const zulip = require("zulip-js");
(async () => {
// Launch a headless browser
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox"],
});
const config = {
username: "networktool-bot@zulip.ipsupply.com.au",
apiKey: "0jMAmOuhfLvBqKJikv5oAkyNM4RIEoAM",
realm: "https://zulip.ipsupply.com.au",
};
const client = await zulip(config);
const page = await browser.newPage();
// Điều hướng đến trang web chứa thẻ <a>
await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien");
// Tìm thẻ <a> cần click dựa trên selector CSS hoặc XPath
const selectBoxSelector1 = "select#idCongTyDienLuc";
await page.waitForSelector(selectBoxSelector1);
await page.select(selectBoxSelector1, "PB11");
// Chờ cho trang mới mở
// await page.waitForNavigation('select#idCongTyDienLuc');
// In ra nội dung của trang mới
setTimeout(async () => {
const tableData = await page.evaluate(() => {
const tbody = document.querySelector("tbody"); // Lựa chọn thẻ <tbody> cần trích xuất
const rows = tbody.querySelectorAll("tr"); // Lựa chọn tất cả các hàng (thẻ <tr>) bên trong thẻ <tbody>
// Lặp qua từng hàng và lấy nội dung của các cột (thẻ <td>) trong hàng
const data = [];
rows.forEach((row) => {
const columns = Array.from(row.querySelectorAll("td")); // Lựa chọn tất cả các cột (thẻ <td>) trong hàng
const rowData = columns.map((column) => column.textContent.trim()); // Lấy nội dung của các cột và xóa khoảng trắng
data.push(rowData); // Thêm dữ liệu của hàng vào mảng data
});
return data.map((u) => u.join("\n\n"));
});
console.log("Table data:", tableData);
await browser.close();
let params = {
type: "stream",
to: "networkToolBot",
topic: "powerSchedule",
content:
":warning: :date: :warning:\n\n" + tableData.join("\n\n") + "\n-------",
};
client.messages.send(params);
if (
tableData.filter((i) => i.includes("KDC 91B") || i.includes("KDC91B"))
.length > 0
) {
let params = {
type: "stream",
to: "Result test - auto.nswteam.net",
topic: "Lịch cúp điện",
content:
":warning: :date: :warning:\n\n" +
tableData
.filter((i) => i.includes("KDC 91B") || i.includes("KDC91B"))[0]
.replace(/KDC 91B/g, "**KDC 91B**") +
"\n-------",
};
client.messages.send(params);
}
}, 5000);
})();