BirdQuizz/game.py

32 lines
1.2 KiB
Python

import random
import os
from glob import glob
bird_species_folders = list(map(os.path.basename, glob("static/data/src_audio/*")))
format_name = lambda folder_name : folder_name.replace('_', ' ')
def get_proposals(question_species_name, n):
proposals = [question_species_name]
for i in range(n):
proposition = format_name(random.choice(bird_species_folders))
while proposition == question_species_name:
proposition = format_name(random.choice(bird_species_folders))
proposals.append(proposition)
random.shuffle(proposals)
return proposals
def new_question():
question_species_folder = random.choice(bird_species_folders)
question_species_name = format_name(question_species_folder)
print(question_species_folder)
question = {}
audio_paths = list(map(os.path.basename, glob(f"static/data/src_audio/{question_species_folder}/*.mp3")))
print(audio_paths)
audio_path = random.choice(audio_paths)
question["species"] = question_species_name
question["species_folder"] = question_species_folder
question["audio_path"] = audio_path
question["proposals"] = get_proposals(question_species_name, 5)
return question