From af3646ae1efe2a5a9b13b955f36fa12465de6946 Mon Sep 17 00:00:00 2001 From: Tyler Porter Date: Thu, 14 Mar 2024 23:42:01 -0400 Subject: [PATCH] Get demo dates working --- rewrite/config/__init__.py | 21 +++++++++++++++++++++ rewrite/data/__init__.py | 4 ++++ rewrite/data/schedule.py | 9 +++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/rewrite/config/__init__.py b/rewrite/config/__init__.py index 27ae9cfe..af716d76 100644 --- a/rewrite/config/__init__.py +++ b/rewrite/config/__init__.py @@ -1,5 +1,7 @@ import logging +from datetime import datetime, timedelta + from config.layout import Layout from config.colors import Colors @@ -9,6 +11,7 @@ class Config: REFERENCE_FILENAME = "config.json.example" + DATE_FORMAT = "%Y-%m-%d" def __init__(self, config_filename, width, height): self.width = width @@ -23,6 +26,14 @@ def __init__(self, config_filename, width, height): self.layout = Layout(width, height) self.colors = Colors() + self.__set_end_of_day() + + def today(self): + if self.demo_date: + return datetime.strptime(self.demo_date, Config.DATE_FORMAT) + + return datetime.today() - timedelta(hours=self.eod_hours, minutes=self.eod_minutes) + def __fetch_config(self, name): """ Loads a config (JSON-formatted) with a custom filename. Falls back to a default if not found. @@ -79,3 +90,13 @@ def __set_log_level(self): logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.WARNING) + + def __set_end_of_day(self): + try: + hours, minutes = [int(part) for part in self.end_of_day.split(":")] + except: + hours, minutes = 0, 0 + ScoreboardLogger.warning(f"Expected 24 hour time for end_of_day in format '12:34'. Received {self.end_of_day}. Defaulting to '00:00'.") + + self.eod_hours = hours + self.eod_minutes = minutes diff --git a/rewrite/data/__init__.py b/rewrite/data/__init__.py index e73bb754..9ef1c66b 100644 --- a/rewrite/data/__init__.py +++ b/rewrite/data/__init__.py @@ -9,3 +9,7 @@ def __init__(self, screen_manager): def request_next_screen(self, screen, **kwargs): self.screen_manager.request_next_screen(screen, **kwargs) + + @property + def config(self): + return self.screen_manager.config diff --git a/rewrite/data/schedule.py b/rewrite/data/schedule.py index d961f5bc..0a7b60af 100644 --- a/rewrite/data/schedule.py +++ b/rewrite/data/schedule.py @@ -1,7 +1,5 @@ import statsapi, time -from datetime import datetime as dt - from utils import logger as ScoreboardLogger from data.update_status import UpdateStatus @@ -28,10 +26,10 @@ def __init__(self, data): def update(self): self.last_update = time.time() - ScoreboardLogger.log(f"Updating schedule for {dt.today()}") + ScoreboardLogger.log(f"Updating schedule for {self.data.config.today()}") try: - self.__fetch_updated_schedule(dt.today()) + self.__fetch_updated_schedule(self.data.config.today()) except Exception as exception: ScoreboardLogger.exception("Networking error while refreshing schedule!") ScoreboardLogger.exception(exception) @@ -50,8 +48,7 @@ def request_next_game(self): self.__request_transition_to_game(self.game) def __fetch_updated_schedule(self, date): - # self._games = statsapi.schedule(date.strftime("%Y-%m-%d")) - self._games = statsapi.schedule(date.strftime("2024-03-12")) + self._games = statsapi.schedule(date.strftime("%Y-%m-%d")) self.games = [Game.from_schedule(game) for game in self._games]