From 2b96f3ec413dac33916fa3df46d8378dbf9d023f Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Sun, 24 Jan 2021 22:31:09 +0100 Subject: [PATCH] Scrape current jpl matchweek every few hours --- cogs/tasks.py | 20 ++++++++++++++++++-- files/config.json | 2 +- functions/scraping.py | 28 +++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cogs/tasks.py b/cogs/tasks.py index b57a87d..d38e14c 100644 --- a/cogs/tasks.py +++ b/cogs/tasks.py @@ -3,7 +3,9 @@ from data.remind import Reminders from discord.ext import commands, tasks from enums.numbers import Numbers from functions import timeFormatters +from functions.config import config from functions.database import currency, poke, prison, birthdays, stats +from functions.scraping import getMatchweek import json import random import requests @@ -21,6 +23,7 @@ class Tasks(commands.Cog): self.checkBirthdays.start() self.updateMessageCounts.start() self.sendReminders.start() + self.updateMatchweek.start() @tasks.loop(hours=1.0) async def bankInterest(self): @@ -218,9 +221,22 @@ class Tasks(commands.Cog): async def beforeSendReminders(self): await self.client.wait_until_ready() - # TODO + @tasks.loop(hours=2.0) async def updateMatchweek(self): - pass + """ + Task that checks the current JPL matchweek & changes the dict value + """ + matchweek = getMatchweek() + + if matchweek is None: + return + + # Change the setting in the config + config("jpl_day", int(matchweek)) + + @updateMatchweek.before_loop + async def beforeUpdateMatchweek(self): + await self.client.wait_until_ready() def getCurrentHour(self): return timeFormatters.dateTimeNow().hour diff --git a/files/config.json b/files/config.json index 6ef40bc..d2a501a 100644 --- a/files/config.json +++ b/files/config.json @@ -1 +1 @@ -{"semester": "1", "year": "2", "years": 2, "jpl": 161733, "jpl_day": 4} \ No newline at end of file +{"semester": "1", "year": "2", "years": 2, "jpl": 161733, "jpl_day": 21} \ No newline at end of file diff --git a/functions/scraping.py b/functions/scraping.py index fcb2b3a..aef771a 100644 --- a/functions/scraping.py +++ b/functions/scraping.py @@ -1,3 +1,5 @@ +import re + from requests import get from urllib.parse import urlencode from bs4 import BeautifulSoup @@ -49,4 +51,28 @@ def getMatchweek(): """ Parses the current JPL matchweek out of Sporza's site """ - pass + resp = get("https://sporza.be/nl/categorie/voetbal/jupiler-pro-league/") + + if resp.status_code != 200: + return None + + bs = BeautifulSoup(resp.text, "html.parser") + matchdays = bs.find_all("section", attrs={"class": "sc-matchdays"}) + + if len(matchdays) < 2: + return None + + # Table header + header = matchdays[1] + + # Regex to find current matchday + r = re.compile(r"speeldag\s*\d+", flags=re.I) + + match = r.search(str(header)) + + # Something went wrong, just ignore + if match is None: + return None + + # "Speeldag DD" -> split on space & take second + return match[0].split(" ")[1]