Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: top statistics scraping #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions db/top_statistics.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
"rank": 1,
"team": "Saiyans FC",
"image": "https://kingsleague.dev/teams/logos/saiyans-fc.svg",
"playerImage": "saiyans-fc-feliu.webp",
"teamId": "saiyans-fc"
"teamId": "saiyans-fc",
"playerImage": "saiyans-fc-20.webp"
},
{
"playerName": "Jacobo",
Expand All @@ -99,7 +99,6 @@
"rank": 2,
"team": "El Barrio",
"image": "https://kingsleague.dev/teams/logos/el-barrio.svg",
"playerImage": "el-barrio-jacobo.webp",
"teamId": "el-barrio"
},
{
Expand All @@ -109,7 +108,6 @@
"rank": 3,
"team": "Los Troncos FC",
"image": "https://kingsleague.dev/teams/logos/los-troncos.svg",
"playerImage": "placeholder.png",
"teamId": "los-troncos-fc"
},
{
Expand All @@ -119,7 +117,6 @@
"rank": 4,
"team": "Ultimate Móstoles",
"image": "https://kingsleague.dev/teams/logos/ultimate-mostoles.svg",
"playerImage": "ultimate-mostoles-juanma-glez.webp",
"teamId": "ultimate-mostoles"
},
{
Expand All @@ -129,7 +126,6 @@
"rank": 5,
"team": "Kunisports",
"image": "https://kingsleague.dev/teams/logos/kunisports.svg",
"playerImage": "kunisports-vctor.webp",
"teamId": "kunisports"
}
],
Expand All @@ -141,8 +137,8 @@
"goals": 3,
"team": "Saiyans FC",
"image": "https://kingsleague.dev/teams/logos/saiyans-fc.svg",
"playerImage": "saiyans-fc-r-carbo.webp",
"teamId": "saiyans-fc"
"teamId": "saiyans-fc",
"playerImage": "saiyans-fc-11.webp"
},
{
"rank": 2,
Expand All @@ -151,7 +147,6 @@
"goals": 3,
"team": "Ultimate Móstoles",
"image": "https://kingsleague.dev/teams/logos/ultimate-mostoles.svg",
"playerImage": "ultimate-mostoles-juanma-glez.webp",
"teamId": "ultimate-mostoles"
},
{
Expand All @@ -161,7 +156,6 @@
"goals": 2,
"team": "El Barrio",
"image": "https://kingsleague.dev/teams/logos/el-barrio.svg",
"playerImage": "el-barrio-jacobo.webp",
"teamId": "el-barrio"
},
{
Expand All @@ -171,7 +165,6 @@
"goals": 2,
"team": "Los Troncos FC",
"image": "https://kingsleague.dev/teams/logos/los-troncos.svg",
"playerImage": "los-troncos-fc-s-sanchez-drago.webp",
"teamId": "los-troncos-fc"
},
{
Expand All @@ -181,7 +174,6 @@
"goals": 2,
"team": "Los Troncos FC",
"image": "https://kingsleague.dev/teams/logos/los-troncos.svg",
"playerImage": "placeholder.png",
"teamId": "los-troncos-fc"
}
],
Expand All @@ -193,8 +185,8 @@
"rank": 1,
"team": "Los Troncos FC",
"image": "https://kingsleague.dev/teams/logos/los-troncos.svg",
"playerImage": "placeholder.png",
"teamId": "los-troncos-fc"
"teamId": "los-troncos-fc",
"playerImage": "los-troncos-fc-23.webp"
},
{
"playerName": "A. Bueno",
Expand All @@ -203,7 +195,6 @@
"rank": 2,
"team": "1K FC",
"image": "https://kingsleague.dev/teams/logos/1k.svg",
"playerImage": "placeholder.png",
"teamId": "1k"
},
{
Expand All @@ -213,7 +204,6 @@
"rank": 3,
"team": "Saiyans FC",
"image": "https://kingsleague.dev/teams/logos/saiyans-fc.svg",
"playerImage": "saiyans-fc-gio-ferinu.webp",
"teamId": "saiyans-fc"
},
{
Expand All @@ -223,7 +213,6 @@
"rank": 4,
"team": "Saiyans FC",
"image": "https://kingsleague.dev/teams/logos/saiyans-fc.svg",
"playerImage": "saiyans-fc-feliu.webp",
"teamId": "saiyans-fc"
},
{
Expand All @@ -233,7 +222,6 @@
"rank": 5,
"team": "Saiyans FC",
"image": "https://kingsleague.dev/teams/logos/saiyans-fc.svg",
"playerImage": "saiyans-fc-david-alba.webp",
"teamId": "saiyans-fc"
}
]
Expand Down
54 changes: 39 additions & 15 deletions scraping/top_statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,29 @@ const [leaderboardDB, mvpDB, topScorersDB, topAssistsDB] = await Promise.all([
readDBFile('players_twelve')
])

export function getTopStatistics() {
const keys = [
{
category: 'mvp'
},
{
category: 'topScorers'
},
{
category: 'topAssists'
}
]

export function getTopStatistics($) {
const extractIdFromUrl = (url) => url.split('/').at(-1).split('.').at(0)

const $rows = $('.uk-card-media-right.uk-cover-container>img.el-image:not(.uk-invisible)')

$rows.each((idx, el) => {
const $el = $(el)
const id = extractIdFromUrl($el.attr('src'))
keys[idx].id = id
})

const leaderboard = leaderboardDB.slice(0, 5).map((data) => {
const { team, ...rest } = data
const { id, name, image, coach, shortName } = team
Expand All @@ -32,31 +54,33 @@ export function getTopStatistics() {
const topScorers = getLimitFrom(topScorersDB).map(extractMoreData)
const topAssists = getLimitFrom(topAssistsDB).map(extractMoreData)

return { leaderboard, mvp, topScorers, topAssists }
return {
leaderboard,
mvp: addImageToFirstPlayer(mvp, 'mvp'),
topScorers: addImageToFirstPlayer(topScorers, 'topScorers'),
topAssists: addImageToFirstPlayer(topAssists, 'topAssists')
}
}

function extractMoreData(player) {
const { team: teamName } = player
const team = TEAMS.find((team) => team.name === teamName)

const { players, id: teamId } = team

const playerImage = findPlayerImage({ playerName: player.playerName, players })
const { id: teamId } = team

return {
...player,
playerImage,
teamId
}
}

function findPlayerImage({ playerName, players }) {
console.log(playerName)

const player = players.find((player) => player.name === playerName)
const playerImage = player?.image ? `${player.image}` : 'placeholder.png'

// buscar player 12

return playerImage
function addImageToFirstPlayer(array, category) {
return [
...array.slice(0, 1).map((player) => {
const id = keys.find((key) => key.category === category).id
const playerImage = `${player.teamId}-${id}.webp`
return { ...player, playerImage }
}),
...array.slice(1)
]
}
1 change: 1 addition & 0 deletions scraping/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const SCRAPINGS = {
scraper: getSchedule
},
top_statistics: {
url: 'https://kingsleague.pro/estadisticas/',
scraper: getTopStatistics
}
// coaches: {
Expand Down