Last active
December 12, 2023 18:32
-
-
Save oleksavyshnivsky/b4d014872a91d5a0a2fab902c9f2dea4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Перевірка участі у міжнародних кубках команд з півфіналу Кубка України (баскетбол PPM) | |
*/ | |
/** | |
* Масив елементів | |
* {{Сезон півфіналу кубка}}: [ | |
* {{Ідентифікатор команд}}: { | |
* name: {{Назва команди}} | |
* international: {{Чи грала у міжнародних кубках наступного сезону}}, | |
* }, | |
* ... | |
* ], | |
* @type {Array} | |
*/ | |
let results = [] | |
/** | |
* Текст на вивід | |
* @type {String} | |
*/ | |
let output = '' | |
/** | |
* Перший сезон для перевірки. З сезону 28 саме 5-й раунд кубка є півфіналом | |
* Для попередніх сезонів потрібно ускладнювати | |
* @type {Number} | |
*/ | |
let initSeason = 1 | |
// Поточний сезон | |
const curSeason = parseInt(document.querySelectorAll('[class="link_r"]')[2].innerText.match(/\d+/)[0]) | |
/** | |
* Читання DOM | |
* @param {String} url Цільова сторінка | |
* @return {Object} DOM-документ | |
*/ | |
async function fetchAndParse(url) { | |
try { | |
const response = await fetch(url) | |
if (!response.ok) { | |
throw new Error(`Не вдалося завантажити: ${response.status}`) | |
} | |
const text = await response.text() | |
return new DOMParser().parseFromString(text, 'text/html') | |
} catch (error) { | |
console.error(`Помилка скачування або читання: ${error.message}`) | |
throw error | |
} | |
} | |
/** | |
* Читання команд із півфіналу | |
* @param {Int} season Сезон національного кубка | |
* @param {Int} round Раунд національного кубка | |
* @return {Array} Масив елементів-посилань на команди | |
*/ | |
async function fetchSemiFinalResults(season, round) { | |
const url = `https://basketball.powerplaymanager.com/uk/national-cup.html?data=results-ukr-${season}-${round}` | |
const doc = await fetchAndParse(url) | |
return doc.querySelectorAll('table a[href*="team.html?data="]') | |
} | |
/** | |
* Читання команд-учасниць міжнародних кубків у наступному сезоні | |
* @param {Int} season Сезон національного кубка | |
* @return {Array} Масив елементів-посилань на команди | |
*/ | |
async function fetchInternationalCupParticipants(season) { | |
const url = `https://basketball.powerplaymanager.com/uk/international-cup-participants-by-country.html?data=teams-ukr-${season + 1}` | |
const doc = await fetchAndParse(url) | |
return doc.querySelectorAll('table a[href*="team.html?data="]') | |
} | |
/** | |
* Читання одного сезону | |
* @param {Int} season Сезон національного кубка | |
*/ | |
async function processSeason(season) { | |
const semiFinalTeams = await fetchSemiFinalResults(season, 5) | |
const isNotSemiFinal = semiFinalTeams.length > 4 | |
if (isNotSemiFinal) { | |
// Читання справжнього півфіналу | |
// const semifinal = doc.getElementById('round').options[doc.getElementById('round').options.length - 2].value | |
const semifinal = Math.ceil(Math.log(semiFinalTeams.length) / Math.log(2)) + 3 | |
const alternativeTeams = await fetchSemiFinalResults(season, semifinal) | |
processTeams(season, alternativeTeams) | |
} else { | |
processTeams(season, semiFinalTeams) | |
} | |
const internationalTeams = await fetchInternationalCupParticipants(season) | |
updateInternationalStatus(season, internationalTeams) | |
} | |
/** | |
* Додавання команд у таблицю для перевірки | |
* @param {Int} season Сезон національного кубка | |
* @param {Array} teams Масив елементів-посилань на команди | |
*/ | |
function processTeams(season, teams) { | |
for (const team of teams) { | |
const teamId = team.href.match(/data=(\d+)-/)[1] | |
results[season] ??= [] | |
results[season][teamId] = { | |
name: team.innerText.trim(), | |
international: false // Значення буде перевірене на наступному кроці | |
} | |
} | |
} | |
/** | |
* Оновлення статусу участі у міжнародних кубках | |
* @param {Int} season Сезон національного кубка | |
* @param {Array} internationalTeams Масив елементів-посилань на команди | |
*/ | |
function updateInternationalStatus(season, internationalTeams) { | |
for (const team of internationalTeams) { | |
const teamId = team.href.match(/data=(\d+)-/)[1] | |
if (results[season][teamId]) { | |
// Якщо ця команда є у результатах читання півфіналу, змінюємо статус | |
results[season][teamId].international = true | |
} | |
} | |
} | |
/** | |
* Перебір сезонів | |
*/ | |
for (let season = initSeason; season < curSeason; season++) { | |
console.log(`Скачується сезон ${season}\n`) | |
// try { | |
await processSeason(season) | |
// } catch (error) { | |
// console.error(error) | |
// } | |
} | |
/** | |
* Підготовка тексту на вивід | |
*/ | |
for (const season in results) { | |
for (const teamId in results[season]) { | |
if (results[season].hasOwnProperty(teamId)) { | |
output += `${season}: ${results[season][teamId].name}: ${results[season][teamId].international}\n` | |
} | |
} | |
} | |
/** | |
* Вивід | |
*/ | |
console.log(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment