From 24367d1b98d0b05d37fbe0924d7da61a747d34c4 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 25 Nov 2021 18:49:58 +0100 Subject: [PATCH] fixed SLI's bug (<3) --- next_song.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/next_song.py b/next_song.py index 320f88e..18752b0 100755 --- a/next_song.py +++ b/next_song.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +from json.decoder import JSONDecodeError import random, glob, os, json @@ -11,15 +12,24 @@ if __name__ == "__main__": def get_air_support(): return random.choice(glob.glob(f"{air_support_folder}/*.ogg")) + def create_state_from_scratch() -> dict: + state = {"current_song_type": "jingle"} + with open(f"{songs_folder}/state.json", "w") as f: + json.dump(state, f, indent=4) + return state + # state loading if not os.path.isfile(f"{songs_folder}/state.json"): # create state file if it doesnt exist with open(f"{songs_folder}/state.json", "w") as f: - state = {"current_song_type": "jingle"} - json.dump(state, f, indent=4) + state = create_state_from_scratch() else: - with open(f"{songs_folder}/state.json") as f: - state = json.load(f) + try: + with open(f"{songs_folder}/state.json") as f: + state = json.load(f) + except JSONDecodeError: + # if file doesnt exist, we recreate a state on the disk + state = create_state_from_scratch() # song choice if state["current_song_type"] == "song":