From 56e7ba92e8487bc09653193f24afba8efc793909 Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Fri, 1 Nov 2024 11:06:10 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20myice/myice.py=20->=20a?= =?UTF-8?q?dded=20EventType=20enum=20and=20updated=20search=20command=20to?= =?UTF-8?q?=20support=20optional=20event=20type=20filtering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added `EventType` enum with two values: 'game' and 'practice'. - Updated `parse_schedule` function to accept an optional `event_type_filter` argument which filters displayed events based on their type. - Changed `age_group` parameter in `parse_schedule` to allow `None`. --- myice/myice.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/myice/myice.py b/myice/myice.py index fc74051..6c85e52 100755 --- a/myice/myice.py +++ b/myice/myice.py @@ -31,6 +31,11 @@ class AgeGroup(str, Enum): u15a = "U15 (A)" +class EventType(str, Enum): + game = "game" + practice = "practice" + + def load_cookies(file: str = "cookies.txt") -> requests.cookies.RequestsCookieJar: cookie_jar_file = Path(file) cj_dict = {} @@ -47,12 +52,14 @@ def save_cookies(): def get_login(local_file: str = "myice.ini") -> tuple[str, str, int]: config = configparser.ConfigParser() - config.read( - [ - Path("~/.config/myice.ini").expanduser(), - "myice.ini", - local_file, - ] + print( + config.read( + [ + Path("~/.config/myice.ini").expanduser(), + "myice.ini", + local_file, + ] + ) ) if "default" in config.sections(): default_config = config["default"] @@ -245,7 +252,11 @@ def get_practice_pdf( @app.command("search") def parse_schedule( - age_group: Annotated[AgeGroup, typer.Argument(...)], + age_group: Annotated[AgeGroup | None, typer.Option(...)] = None, + event_type_filter: Annotated[ + EventType | None, + typer.Option("--type", help="Only display events of this type"), + ] = None, schedule_file: Annotated[ Path, typer.Option(help="schedule json file to parse") ] = Path("schedule.json"), @@ -255,9 +266,22 @@ def parse_schedule( """ with schedule_file.open("r") as f: data = json.load(f) - for event in [x for x in data if x["agegroup"] == age_group]: - # print(json.dumps(event, indent=2)) - raw_title = event["title"].removeprefix(age_group + "\n") + # age_group filter + if age_group: + events = [x for x in data if x["agegroup"] == age_group] + else: + events = [x for x in data if x["agegroup"] in AgeGroup] + # event_type filter + if event_type_filter: + if event_type_filter.value == EventType.game: + events = [x for x in events if "event" in x and x["event"] == "Jeu"] + else: + events = [x for x in events if "event" not in x or x["event"] == "Jeu"] + for event in events: + if age_group: + raw_title = event["title"].removeprefix(age_group + "\n") + else: + raw_title = event["title"] title = " ".join(raw_title.split("\n")) start = datetime.datetime.fromisoformat(event["start"]) start_fmt = start.strftime("%H:%M")