This commit is contained in:
parent
ef6ee2e3c6
commit
24367d1b98
18
next_song.py
18
next_song.py
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
from json.decoder import JSONDecodeError
|
||||||
import random, glob, os, json
|
import random, glob, os, json
|
||||||
|
|
||||||
|
|
||||||
@ -11,15 +12,24 @@ if __name__ == "__main__":
|
|||||||
def get_air_support():
|
def get_air_support():
|
||||||
return random.choice(glob.glob(f"{air_support_folder}/*.ogg"))
|
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
|
# state loading
|
||||||
if not os.path.isfile(f"{songs_folder}/state.json"):
|
if not os.path.isfile(f"{songs_folder}/state.json"):
|
||||||
# create state file if it doesnt exist
|
# create state file if it doesnt exist
|
||||||
with open(f"{songs_folder}/state.json", "w") as f:
|
with open(f"{songs_folder}/state.json", "w") as f:
|
||||||
state = {"current_song_type": "jingle"}
|
state = create_state_from_scratch()
|
||||||
json.dump(state, f, indent=4)
|
|
||||||
else:
|
else:
|
||||||
with open(f"{songs_folder}/state.json") as f:
|
try:
|
||||||
state = json.load(f)
|
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
|
# song choice
|
||||||
if state["current_song_type"] == "song":
|
if state["current_song_type"] == "song":
|
||||||
|
Loading…
Reference in New Issue
Block a user