Compare commits

...

5 Commits

Author SHA1 Message Date
Stijn De Clercq 968df71f98
Merge pull request #88 from stijndcl/fix_matches
Add support for postponed matches
2021-08-19 21:12:57 +02:00
Stijn De Clercq b083cfe0bf Add support for postponed matches 2021-08-19 21:12:29 +02:00
Stijn De Clercq b910018ddc
Merge pull request #87 from stijndcl/links
Fix JPL tournament code & links
2021-08-19 20:49:42 +02:00
Stijn De Clercq bf9b9c23b0 Fix matches url 2021-08-19 20:47:53 +02:00
Stijn De Clercq daae31a298 Update jpl code 2021-08-19 20:35:22 +02:00
4 changed files with 43 additions and 9 deletions

View File

@ -2,7 +2,7 @@ from decorators import help
from discord.ext import commands
from enums.help_categories import Category
from functions import checks, config
from functions.football import getMatches, getTable
from functions.football import getMatches, getTable, get_jpl_code
class Football(commands.Cog):
@ -36,6 +36,13 @@ class Football(commands.Cog):
async def table(self, ctx, *args):
await ctx.send(getTable())
@commands.check(checks.isMe)
@jpl.command(name="Update")
async def update(self, ctx):
code = get_jpl_code()
config.config("jpl", code)
await ctx.message.add_reaction("")
def setup(client):
client.add_cog(Football(client))

View File

@ -56,6 +56,7 @@
"jpl": "Informatie over de Jupiler Pro League.",
"jpl matches": "Bekijk de wedstrijden die gespeeld worden op [Week]. Default naar de huidige speeldag.",
"jpl table": "De huidige stand van het klassement.",
"jpl update": "Haalt de nieuwe code voor de competitie van dit jaar op.",
"leaderboard": "Bekijk de Top 10 van [Categorie].\nIndien je geen categorie opgeeft krijg je een lijst van categorieën.",
"les": "Bekijk het lessenrooster voor [Dag] in het [Jaargang]-de jaar.\nIndien je geen dag opgeeft, is dit standaard vandaag. De jaargang is standaard 2.\nLes Morgen/Overmorgen werkt ook.",
"lmgtfy": "Stuur iemand een LMGTFY link wanneer ze je een domme vraag stellen in plaats van het zelf op te zoeken.\nQueries met spaties moeten **niet** tussen aanhalingstekens staan.",

View File

@ -1,15 +1,20 @@
from enum import Enum
from typing import Optional
from attr import dataclass, field
from datetime import datetime
from enum import Enum
from functions.timeFormatters import fromString
from functions.scrapers.sporza import getJPLMatches, getJPLTable
from functions.stringFormatters import leadingZero
from datetime import datetime
import re
from requests import get
import tabulate
class Status(Enum):
AfterToday = "--:--"
NotStarted = "--:--"
Postponed = "--:--"
Over = "Einde"
HalfTime = "Rust"
@ -24,7 +29,7 @@ class Match:
homeScore: int = 0
away: str = field(init=False)
awayScore: int = 0
start: datetime = field(init=False)
start: Optional[datetime] = field(init=False)
date: str = field(init=False)
weekDay: str = field(init=False)
status: Status = field(init=False)
@ -42,9 +47,13 @@ class Match:
self.homeScore = self.matchDict[Navigation.HomeScore.value]
self.awayScore = self.matchDict[Navigation.AwayScore.value]
self.start = fromString(self.matchDict["startDateTime"], formatString="%Y-%m-%dT%H:%M:%S.%f%z")
self.date = self.start.strftime("%d/%m")
self.weekDay = self._getWeekday()
if "startDateTime" in self.matchDict:
self.start = fromString(self.matchDict["startDateTime"], formatString="%Y-%m-%dT%H:%M:%S.%f%z")
else:
self.start = None
self.date = self.start.strftime("%d/%m") if self.start is not None else "Uitgesteld"
self.weekDay = self._getWeekday() if self.start is not None else "??"
def _getStatus(self, status: str):
"""
@ -65,6 +74,7 @@ class Match:
statusses: dict = {
"after_today": Status.AfterToday.value,
"not_started": Status.NotStarted.value,
"postponed": Status.Postponed.value,
"end": Status.Over.value
}
@ -88,6 +98,9 @@ class Match:
"""
Returns a string representing the scoreboard
"""
if self.start is None:
return "??"
# No score to show yet, show time when the match starts
if not self._hasStarted():
return "{}:{}".format(leadingZero(str(self.start.hour)), leadingZero(str(self.start.minute)))
@ -95,7 +108,7 @@ class Match:
return "{} - {}".format(self.homeScore, self.awayScore)
def _hasStarted(self):
return self.status not in [Status.AfterToday.value, Status.NotStarted.value]
return self.status not in [Status.AfterToday.value, Status.NotStarted.value, Status.Postponed.value]
class Navigation(Enum):
@ -162,3 +175,14 @@ def _formatRow(row):
scoresArray.insert(1, row.find_all("a")[0].renderContents().decode("utf-8").split("<!--")[0])
return scoresArray
def get_jpl_code() -> int:
editions = get("https://api.sporza.be/web/soccer/competitions/48").json()["editions"]
newest_edition = editions[0]["_links"]["self"]["href"]
phase = get(newest_edition).json()["phases"][0]
phase_url = phase["_links"]["self"]["href"]
r = re.compile(r"\d+$")
match = re.search(r, phase_url)
return int(match[0])

View File

@ -1,4 +1,5 @@
from bs4 import BeautifulSoup
from functions import config
import re
from requests import get
@ -38,7 +39,8 @@ def getJPLMatches(week: int):
"""
JPL matches for a given matchweek
"""
current_day = get("https://api.sporza.be/web/soccer/matchdays/161733/{}".format(week))
jpl = config.get("jpl")
current_day = get(f"https://api.sporza.be/web/soccer/phases/{jpl}/matchdays/{week}")
# Something went wrong
if current_day.status_code != 200: