from flask import Flask, render_template, jsonify, request import random import json from res.name_cc import name_cc def name_to_image_url(country_name: str) -> str: try: cc = name_cc[country_name] except: cc = None if cc != None: return f"https://gstatic.olympics.com/s1/f_auto/static/light/flag/paris-2024/olympic/3x2/{cc}.png" else: return "https://gstatic.olympics.com/s1/f_auto/static/light/flag/paris-2024/olympic/3x2/EOR.png" app = Flask(__name__) # Load data from JSON file with open('data/data.json') as f: data = json.load(f) # Route to display the game @app.route('/') def index(): return render_template('index.html') # API to get a random discipline and participants @app.route('/get_random_discipline', methods=['GET']) def get_random_discipline(): discipline = random.choice(list(data.keys())) category = random.choice(list(data[discipline].keys())) participants = data[discipline][category]['participants'] # Prepare the data to send to the frontend response = { "participants": [ { "country_name": participant["country"] if participant["country"] != "EOR" else "Équipe olympique des réfugiés", "country_img_url": name_to_image_url(participant["country"]), "athlete_name": participant.get("athlete", {}).get("name", None), "athlete_img_url": participant.get("athlete", {}).get("image", None), "athlete_meta_url": participant.get("athlete", {}).get("meta_url", None) } for participant in participants ], "answer": f"{discipline.capitalize()} {' '.join(category.split('-'))}" } return jsonify(response) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)