Clean up jpl table

pull/31/head
Stijn De Clercq 2021-01-26 21:58:49 +01:00
parent 7c47c6af73
commit 89fdc70813
5 changed files with 48 additions and 33 deletions

View File

@ -1,11 +1,8 @@
from bs4 import BeautifulSoup
from decorators import help from decorators import help
from discord.ext import commands from discord.ext import commands
from enums.help_categories import Category from enums.help_categories import Category
from functions import checks, config from functions import checks, config
from functions.football import getMatches from functions.football import getMatches, getTable
import requests
import tabulate
class Football(commands.Cog): class Football(commands.Cog):
@ -35,30 +32,10 @@ class Football(commands.Cog):
else: else:
return await ctx.send("Dit is geen geldige speeldag.") return await ctx.send("Dit is geen geldige speeldag.")
# TODO check back when there's active games & add the timestamp instead of EINDE
def format_match(self, match):
return [match["day"], match["date"], match["home"], match["score"], match["away"], "Einde"]
def get_weekday(self, day: int):
days = ["Ma", "Di", "Wo", "Do", "Vr", "Za", "Zo"]
return days[day]
@jpl.command(name="Table", aliases=["Ranking", "Rankings", "Ranks", "T"]) @jpl.command(name="Table", aliases=["Ranking", "Rankings", "Ranks", "T"])
async def table(self, ctx, *args): async def table(self, ctx, *args):
page_html = requests.get("https://sporza.be/nl/categorie/voetbal/jupiler-pro-league/").text await ctx.send(getTable())
bs_parsed = BeautifulSoup(page_html, "html.parser")
rows = bs_parsed.find(summary="algemeen klassement").find_all("tr")[1:]
rowsFormatted = []
for row in rows:
rowsFormatted.append(self.createRowList(row))
await ctx.send("```Jupiler Pro League Klassement\n\n{}```".format(tabulate.tabulate(rowsFormatted, headers=["#", "Ploeg", "Punten", "M", "M+", "M-", "M="])))
# Formats the row into an list that can be passed to Tabulate
def createRowList(self, row):
scoresArray = list([td.renderContents().decode("utf-8") for td in row.find_all("td")])[:6]
# Insert the team name into the list
scoresArray.insert(1, row.find_all("a")[0].renderContents().decode("utf-8").split("<!--")[0])
return scoresArray
def setup(client): def setup(client):

View File

@ -1 +1 @@
{"semester": "1", "year": "2", "years": 2, "jpl": 161733, "jpl_day": 21} {"semester": "1", "year": "2", "years": 2, "jpl": 161733, "jpl_day": 22}

View File

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
from attr import dataclass, field from attr import dataclass, field
from functions.timeFormatters import fromString from functions.timeFormatters import fromString
from functions.scraping import getJPLMatches from functions.scraping import getJPLMatches, getJPLTable
from functions.stringFormatters import leadingZero from functions.stringFormatters import leadingZero
from datetime import datetime from datetime import datetime
import tabulate import tabulate
@ -117,13 +117,13 @@ class Navigation(Enum):
def getMatches(matchweek: int): def getMatches(matchweek: int):
""" """
Function that constructs the table for a given matchweek Function that constructs the list of matches for a given matchweek
""" """
current_day = getJPLMatches(matchweek) current_day = getJPLMatches(matchweek)
# API request failed # API request failed
if current_day is None: if current_day is None:
return "Er ging iets mis. Probeer het later opnieuw." return "Er ging iets fout. Probeer het later opnieuw."
matches = list(map(Match, current_day)) matches = list(map(Match, current_day))
matches = list(map(lambda x: x.getInfo(), matches)) matches = list(map(lambda x: x.getInfo(), matches))
@ -132,3 +132,30 @@ def getMatches(matchweek: int):
table = tabulate.tabulate(matches, headers=["Dag", "Datum", "Thuis", "Stand", "Uit", "Tijd"]) table = tabulate.tabulate(matches, headers=["Dag", "Datum", "Thuis", "Stand", "Uit", "Tijd"])
return "```{}\n\n{}```".format(header, table) return "```{}\n\n{}```".format(header, table)
def getTable():
"""
Function that constructs the current table of the JPL
"""
rows = getJPLTable()
if rows is None:
return "Er ging iets fout. Probeer het later opnieuw."
formatted = [_formatRow(row) for row in rows]
header = "Jupiler Pro League Klassement"
table = tabulate.tabulate(formatted, headers=["#", "Ploeg", "Punten", "M", "M+", "M-", "M="])
return "```{}\n\n{}```".format(header, table)
def _formatRow(row):
"""
Function that formats a row into a list for Tabulate to use
"""
scoresArray = list([td.renderContents().decode("utf-8") for td in row.find_all("td")])[:6]
# Insert the team name into the list
scoresArray.insert(1, row.find_all("a")[0].renderContents().decode("utf-8").split("<!--")[0])
return scoresArray

View File

@ -87,10 +87,7 @@ def _applyMeme(meme: Meme, fields):
def _mockingSpongebob(fields): def _mockingSpongebob(fields):
for i, field in enumerate(fields): return list(map(mock, fields))
fields[i] = mock(field)
return fields
def _xXEverywhere(fields): def _xXEverywhere(fields):

View File

@ -89,3 +89,17 @@ def getJPLMatches(week: int):
return None return None
return current_day.json()["groupedMatches"][0]["matches"] return current_day.json()["groupedMatches"][0]["matches"]
def getJPLTable():
"""
JPL table
"""
page_html = get("https://sporza.be/nl/categorie/voetbal/jupiler-pro-league/")
if page_html.status_code != 200:
return None
bs_parsed = BeautifulSoup(page_html.text, "html.parser")
rows = bs_parsed.find(summary="algemeen klassement").find_all("tr")[1:]
return rows