|
from flask import Flask, jsonify, request, render_template, Markup |
|
import requests |
|
import json |
|
from traficFranceBleuParis import generate_traffic_html |
|
|
|
TOMTOM_API_KEY = "PVt4NaZSKKtziZi9DaqNAUzN4flNJnSo" |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/') |
|
def index(): |
|
try: |
|
|
|
traffic_report_content = generate_traffic_html() |
|
except Exception as e: |
|
|
|
traffic_report_content = f"<p>Erreur lors de la génération du rapport : {e}</p>" |
|
|
|
|
|
|
|
|
|
return render_template('index.html', traffic_report=Markup(traffic_report_content)) |
|
|
|
@app.route('/api/incidents', methods=['GET']) |
|
def get_incidents(): |
|
""" |
|
Récupère les incidents depuis l'API TomTom Traffic. |
|
""" |
|
try: |
|
|
|
bounding_box = "1.79804455,48.52917947,2.88843762,49.24075760" |
|
url = f"https://api.tomtom.com/traffic/services/5/incidentDetails?bbox={bounding_box}&fields=%7Bincidents%7Btype%2Cgeometry%7Btype%2Ccoordinates%7D%2Cproperties%7BiconCategory%7D%7D%7D&language=en-GB&categoryFilter=0%2C1%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C14&timeValidityFilter=present&key={TOMTOM_API_KEY}" |
|
|
|
|
|
response = requests.get(url) |
|
response.raise_for_status() |
|
|
|
data = response.json() |
|
incidents = data.get("incidents", []) |
|
|
|
|
|
print(f"Incidents récupérés ({len(incidents)} incidents) : {json.dumps(incidents, indent=2)}") |
|
|
|
return jsonify({"status": "success", "data": incidents}), 200 |
|
|
|
except requests.exceptions.RequestException as e: |
|
print(f"Erreur lors de la récupération des incidents: {e}") |
|
return jsonify({"status": "error", "message": str(e)}), 500 |
|
|
|
if __name__ == "__main__": |
|
import os |
|
port = int(os.environ.get("PORT", 7860)) |
|
app.run(host="0.0.0.0", port=port, debug=True, use_reloader=False) |