Log_service/app/utils/powerSchedule.js

84 lines
3.3 KiB
JavaScript

const puppeteer = require("puppeteer");
const zulip = require("zulip-js");
const moment = require("moment");
const { addLogFunction } = require("./addLogFunctionJS");
(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();
await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien");
const selectBoxSelector1 = "select#idCongTyDienLuc";
await page.waitForSelector(selectBoxSelector1);
await page.select(selectBoxSelector1, "PB11");
// In ra nội dung của trang mới
setTimeout(async () => {
const htmlContent = 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
// });
const htmlContent = document.querySelector('.notification').outerHTML
return htmlContent || "";
});
var TurndownService = require('turndown')
const turndownService = new TurndownService();
const markdown = turndownService.turndown(htmlContent);
await browser.close();
let params = {
type: "stream",
to: "networkToolBot",
topic: "powerSchedule",
content:
`:warning: <time:${(new Date()).toDateString()}> :warning:\n\n ${markdown.replaceAll('KDC 91B', ':warning:KDC 91B:warning:').replaceAll('B31', ':warning:B31:warning:')}\n-------`,
};
client.messages.send(params);
let checkTime = moment(Date.now()).format("DD_MM_YYYY").toString()
// if (
// markdown.filter((i) => (i.includes("KDC 91B") || i.includes("KDC91B")) && i.includes(checkTime))
// .length > 0
// ) {
// let params = {
// type: "stream",
// to: "Cục tình báo",
// topic: "Thông báo chung",
// content:
// ":warning: :date: :warning:\n\n" +
// markdown
// .filter((i) => i.includes("KDC 91B") || i.includes("KDC91B"))[0]
// .replace(/KDC 91B/g, "**KDC 91B**") +
// "\n-------",
// };
// client.messages.send(params);
// }
const fileName =
"/home/Log_service/app/store/logsAPI/" +
moment(Date.now()).format("DD_MM_YYYY").toString() +
".log";
addLogFunction(fileName, JSON.stringify(params, null, 2), "powerSchedule")
}, 5000);
})();