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
|
||||
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":
|
||||
|
Loading…
Reference in New Issue
Block a user