Untitled
let itemData = []; async function processPage(pageNumber) { try { const url = "https://admin.gambit-rp.ru/logs/player/1006?page=" + pageNumber; const response = await fetch(url); const html = await response.text(); const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); const elems = doc.querySelectorAll("tbody tr"); let stop = false; for (let i = 0; i < elems.length; i++) { let elem = elems[i]; let item_id = elem.children[0].textContent.trim().split("/")[0]; let item_count = elem.children[0].textContent.trim().split("/")[1]; let player1_idElem = elem.children[1].querySelector("a"); let player1_id = player1_idElem ? player1_idElem.getAttribute("href").split("/players/")[1] : null; let player1_nick = player1_idElem ? player1_idElem.textContent.trim() : null; let player2_idElem = elem.children[2].querySelector("a"); let player2_id = player2_idElem ? player2_idElem.getAttribute("href").split("/players/")[1] : null; let player2_nick = player2_idElem ? player2_idElem.textContent.trim() : null; let date = elem.children[3].textContent.trim(); itemData.push({ "item_id": parseInt(item_id), "item_count": parseInt(item_count), "player1_id": player1_id ? parseInt(player1_id) : null, "player1_nick": player1_nick, "player2_id": player2_id ? parseInt(player2_id) : null, "player2_nick": player2_nick, "date": date }); let time = new Date(date).getTime() / 1000; if (time < start) { stop = true; continue; } } return { stop, data: itemData }; } catch (error) { console.error("error", error); return { stop: true, data: [] }; } } const maxPages = 1; let currentDate = new Date(); // days count currentDate.setDate(currentDate.getDate() - 1); let start = currentDate.getTime() / 1000; const pagePromises = Array.from({ length: maxPages }, (_, i) => processPage(i + 1)); try { const results = await Promise.all(pagePromises); let stop = false; results.forEach(({ stop: pageStop, data }) => { if (pageStop) { stop = true; } else { itemData.push(...data); } }); if (!stop) { const postUrl = "https://logs.enotixa.ru/add-api"; const requestOptions = { method: 'POST', mode: 'cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ code: "CODE_VALUE", data: itemData }) }; const response = await fetch(postUrl, requestOptions); if (response.ok) { console.log("done"); let a = await response.json(); console.log(a); } else { console.error("status", response.status); } } else { console.log("Some pages failed, check logs for details."); } } catch (error) { console.error("error", error); }
Leave a Comment