update function powerSchedule
This commit is contained in:
		
							parent
							
								
									2e195e2630
								
							
						
					
					
						commit
						52ee65cf53
					
				| 
						 | 
					@ -13,37 +13,54 @@ const zulip = require("zulip-js");
 | 
				
			||||||
    realm: "https://zulip.ipsupply.com.au",
 | 
					    realm: "https://zulip.ipsupply.com.au",
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
  const client = await zulip(config);
 | 
					  const client = await zulip(config);
 | 
				
			||||||
  // Open a new page
 | 
					 | 
				
			||||||
  const page = await browser.newPage();
 | 
					  const page = await browser.newPage();
 | 
				
			||||||
  await page.goto("https://ithongtin.com/lich-cup-dien/can-tho/ninh-kieu");
 | 
					
 | 
				
			||||||
  const data = await page.evaluate(() => {
 | 
					  // Điều hướng đến trang web chứa thẻ <a>
 | 
				
			||||||
    const title = document.title;
 | 
					  await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien");
 | 
				
			||||||
    const paragraphText = document.querySelector("table").textContent;
 | 
					
 | 
				
			||||||
    console.log(paragraphText)
 | 
					  // Tìm thẻ <a> cần click dựa trên selector CSS hoặc XPath
 | 
				
			||||||
    return {
 | 
					  const selectBoxSelector1 = "select#idCongTyDienLuc";
 | 
				
			||||||
      title,
 | 
					  await page.waitForSelector(selectBoxSelector1);
 | 
				
			||||||
      paragraphText,
 | 
					  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();
 | 
					    await browser.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (
 | 
					    if (
 | 
				
			||||||
    data.paragraphText
 | 
					      tableData.filter((i) => i.includes("KDC 91B") || i.includes("KDC91B")).length > 0
 | 
				
			||||||
      .split("   ")
 | 
					 | 
				
			||||||
      .filter((i) => i.includes("KDC 91B") || i.includes("KDC91B")).length > 0
 | 
					 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
      let params = {
 | 
					      let params = {
 | 
				
			||||||
          type: "stream",
 | 
					          type: "stream",
 | 
				
			||||||
          to: "Result test - auto.nswteam.net",
 | 
					          to: "Result test - auto.nswteam.net",
 | 
				
			||||||
          topic: "Lịch cúp điện",
 | 
					          topic: "Lịch cúp điện",
 | 
				
			||||||
          content:
 | 
					          content:
 | 
				
			||||||
          ":warning: :date: :warning:\n" +
 | 
					            ":warning: :date: :warning:\n\n" +
 | 
				
			||||||
          data.paragraphText
 | 
					            tableData.filter((i) => i.includes("KDC 91B") || i.includes("KDC91B"))[0].replace(/KDC 91B/g,"**KDC 91B**") +
 | 
				
			||||||
            .split("   ")
 | 
					 | 
				
			||||||
            .filter((i) => i.includes("KDC 91B") || i.includes("KDC91B"))[0]?.replace(/  /g,"\n\n")?.replace(/KDC 91B/g,"**KDC 91B**") +
 | 
					 | 
				
			||||||
            "\n-------",
 | 
					            "\n-------",
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
      client.messages.send(params);
 | 
					      client.messages.send(params);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }, 5000);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
| 
						 | 
					@ -2,34 +2,66 @@ const puppeteer = require("puppeteer");
 | 
				
			||||||
const zulip = require("zulip-js");
 | 
					const zulip = require("zulip-js");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(async () => {
 | 
					(async () => {
 | 
				
			||||||
  // Launch a headless browser
 | 
					  const browser = await puppeteer.launch();
 | 
				
			||||||
  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);
 | 
					 | 
				
			||||||
  // Open a new page
 | 
					 | 
				
			||||||
  const page = await browser.newPage();
 | 
					  const page = await browser.newPage();
 | 
				
			||||||
  await page.goto("https://ithongtin.com/lich-cup-dien/can-tho/ninh-kieu");
 | 
					 | 
				
			||||||
  const data = await page.evaluate(() => {
 | 
					 | 
				
			||||||
    const title = document.title;
 | 
					 | 
				
			||||||
    const paragraphText = document.querySelector("table").textContent;
 | 
					 | 
				
			||||||
    return {
 | 
					 | 
				
			||||||
      title,
 | 
					 | 
				
			||||||
      paragraphText,
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
  await browser.close();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
console.log("data: ",
 | 
					  // Điều hướng đến trang web chứa thẻ <a>
 | 
				
			||||||
data.paragraphText
 | 
					  await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien");
 | 
				
			||||||
      .split("   ")
 | 
					
 | 
				
			||||||
      .filter((i) => i.includes("KDC 91B") || i.includes("KDC91B")))
 | 
					  // 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("table"); // 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>
 | 
				
			||||||
 | 
					      // const headerRow = document.querySelectorAll("thead"); // Lựa chọn tất cả các hàng (thẻ <tr>) bên trong thẻ <tbody>
 | 
				
			||||||
 | 
					      // Lấy tên các cột từ hàng đầu (thẻ <th>)
 | 
				
			||||||
 | 
					      // const headerColumns = headerRow.querySelectorAll("tr");
 | 
				
			||||||
 | 
					      // const columnNames = headerColumns.map((headerColumn) =>
 | 
				
			||||||
 | 
					        // headerColumn.textContent.trim()
 | 
				
			||||||
 | 
					      // );
 | 
				
			||||||
 | 
					      const data1 = [];
 | 
				
			||||||
 | 
					      // headerColumns.forEach((clo) => {
 | 
				
			||||||
 | 
					      //   const columns = Array.from(clo.querySelectorAll("th")); // 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
 | 
				
			||||||
 | 
					      //   data1.push(rowData); // Thêm dữ liệu của hàng vào mảng data
 | 
				
			||||||
 | 
					      // });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // 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 headers = Array.from(row.querySelectorAll("th"));
 | 
				
			||||||
 | 
					        const rowHeader = headers.map((header) => header.textContent.trim());
 | 
				
			||||||
 | 
					        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
 | 
				
			||||||
 | 
					        data1.push(rowHeader)
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      return { data: data, columnNames: data1 };
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    const content = []
 | 
				
			||||||
 | 
					    tableData.data?.map((u,index)=>{
 | 
				
			||||||
 | 
					      for(let i=0; i<u.length;i++){
 | 
				
			||||||
 | 
					        tableData.data[index] = tableData.columnNames[index]+": "+u[]
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					    console.log("Table data:", );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await browser.close();
 | 
				
			||||||
 | 
					  }, 5000);
 | 
				
			||||||
 | 
					})();
 | 
				
			||||||
 | 
					// console.log("data: ",
 | 
				
			||||||
 | 
					// data.paragraphText
 | 
				
			||||||
 | 
					//       .split("   ")
 | 
				
			||||||
 | 
					//       .filter((i) => i.includes("KDC 91B") || i.includes("KDC91B")))
 | 
				
			||||||
// if (
 | 
					// if (
 | 
				
			||||||
//   data.paragraphText
 | 
					//   data.paragraphText
 | 
				
			||||||
//     .split("   ")
 | 
					//     .split("   ")
 | 
				
			||||||
| 
						 | 
					@ -48,7 +80,6 @@ data.paragraphText
 | 
				
			||||||
//     };
 | 
					//     };
 | 
				
			||||||
//   client.messages.send(params);
 | 
					//   client.messages.send(params);
 | 
				
			||||||
// }
 | 
					// }
 | 
				
			||||||
})();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// const zulip = require("zulip-js");
 | 
					// const zulip = require("zulip-js");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue