radio-bullshit/next_song.py
2021-06-15 23:27:04 +02:00

46 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
import random, glob, os, json
if __name__ == "__main__":
songs_folder = "/songs"
jingles_folder = "/jingles"
air_support_folder = "/air-support"
def get_air_support():
return random.choice(glob.glob(f"{air_support_folder}/*.ogg"))
# 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)
else:
with open(f"{songs_folder}/state.json") as f:
state = json.load(f)
# song choice
if state["current_song_type"] == "song":
state["current_song_type"] = "jingle"
try:
print(random.choice(glob.glob(f"{jingles_folder}/*.ogg")))
except IndexError:
print(get_air_support())
elif state["current_song_type"] == "jingle":
state["current_song_type"] = "song"
try:
print(random.choice(glob.glob(f"{songs_folder}/*.ogg")))
except IndexError:
print(get_air_support())
else:
# should not happen
# resiliency mode
print(get_air_support())
state = {"current_song_type": "jingle"}
# state saving
with open(f"{songs_folder}/state.json", "w") as f:
json.dump(state, f, indent=4)