Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
2.9 kB
2
Indexable
Never
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;
    } catch (error) {
        console.error("error", error);
        return false;
    }
}

const maxPages = 30;
let result = true;
let currentDate = new Date();

// скок дней
currentDate.setDate(currentDate.getDate() - 1);

let start = currentDate.getTime() / 1000;

for (let i = 1; i <= maxPages && result; i++) {
    console.log("checking page", i);
    result = await processPage(i);
    if (!result) {
        console.log("last page is", i);
    }
}

const postData = JSON.stringify(itemData, null, 2);

const postUrl = "https://admin.enotixa.ru/";
const requestOptions = {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({ data: postData }),
};

try {
    const response = await fetch(postUrl, requestOptions);
    if (response.ok) {
        console.log("done");
    } else {
        console.error("status", response.status);
    }
} catch (error) {
    console.error("error", error);
}
Leave a Comment