Spaces:
Sleeping
Sleeping
| import random | |
| from datetime import datetime | |
| from flask import Flask, render_template, request, Response | |
| import pandas as pd | |
| from huggingface_hub import list_repo_files | |
| from urllib.parse import quote | |
| import requests | |
| app = Flask(__name__) | |
| REPO_ID = "Andrew12121212/RASP" | |
| def get_csv_url(nombre): | |
| nombre_codificado = quote(nombre) | |
| return f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{nombre_codificado}" | |
| # Funci贸n robusta para fechas en espa帽ol tipo "10 junio 2025 - 20:30" | |
| def fecha_es_a_datetime(fecha_str): | |
| meses = { | |
| 'enero': '01', 'febrero': '02', 'marzo': '03', 'abril': '04', | |
| 'mayo': '05', 'junio': '06', 'julio': '07', 'agosto': '08', | |
| 'septiembre': '09', 'octubre': '10', 'noviembre': '11', 'diciembre': '12' | |
| } | |
| try: | |
| partes = fecha_str.strip().split(' - ') | |
| if len(partes) != 2: | |
| return None | |
| fecha, hora = partes | |
| dia, mes, anio = fecha.strip().split(' ') | |
| mes = meses.get(mes.lower()) | |
| if not mes: | |
| return None | |
| fecha_iso = f"{anio}-{mes}-{dia.zfill(2)} {hora}" | |
| return datetime.strptime(fecha_iso, "%Y-%m-%d %H:%M") | |
| except Exception: | |
| return None | |
| def index(): | |
| archivos = list_repo_files(repo_id=REPO_ID, repo_type="dataset") | |
| csvs = [a for a in archivos if a.endswith(".csv")] | |
| csvs.sort(reverse=True) | |
| nombre = request.args.get("csv", "pronosticos.csv") | |
| url = get_csv_url(nombre) | |
| try: | |
| df = pd.read_csv(url) | |
| columnas = ["Equipo A", "Equipo B", "Fecha", "Recomendaci贸n", "Cuota"] | |
| filas = [] | |
| for _, row in df.iterrows(): | |
| fila = {col: row.get(col, "") for col in columnas} | |
| fila["Resumen"] = row.get("Resumen", "") | |
| filas.append(fila) | |
| tabla_html = df.to_html(classes="table table-striped", index=False) | |
| except Exception as e: | |
| columnas = [] | |
| filas = [] | |
| tabla_html = f"<p>Error al leer el archivo: {e}</p>" | |
| return render_template( | |
| "index.html", | |
| columnas=columnas, | |
| filas=filas, | |
| tabla_html=tabla_html, | |
| nombre=nombre, | |
| csvs=csvs, | |
| actual=nombre | |
| ) | |
| def ver_csv(): | |
| archivos = list_repo_files(repo_id=REPO_ID, repo_type="dataset") | |
| csvs = [a for a in archivos if a.endswith(".csv")] | |
| csvs.sort(reverse=True) | |
| nombre = request.args.get("csv", "pronosticos.csv") | |
| url = get_csv_url(nombre) | |
| try: | |
| df = pd.read_csv(url) | |
| tabla_html = df.to_html(classes="table table-striped", index=False) | |
| except Exception as e: | |
| tabla_html = f"<p>Error al leer el archivo: {e}</p>" | |
| return render_template( | |
| "ver_csv.html", | |
| tabla_html=tabla_html, | |
| nombre=nombre, | |
| csvs=csvs, | |
| actual=nombre | |
| ) | |
| def descargar_csv(): | |
| nombre = request.args.get("csv", "pronosticos.csv") | |
| url = get_csv_url(nombre) | |
| r = requests.get(url) | |
| response = Response(r.content, mimetype='text/csv') | |
| response.headers.set("Content-Disposition", "attachment", filename=nombre) | |
| return response | |
| def acca(): | |
| archivos = list_repo_files(repo_id=REPO_ID, repo_type="dataset") | |
| csvs = [a for a in archivos if a.endswith(".csv")] | |
| csvs.sort(reverse=True) | |
| nombre = request.args.get("csv", "pronosticos.csv") | |
| url = get_csv_url(nombre) | |
| partidos = [] | |
| ahora = datetime.now() | |
| try: | |
| df = pd.read_csv(url) | |
| for _, row in df.iterrows(): | |
| cuota = str(row.get("Cuota", "")).strip() | |
| recomendacion = str(row.get("Recomendaci贸n", "")).strip() | |
| fecha_str = str(row.get("Fecha", "")).strip() | |
| fecha_partido = fecha_es_a_datetime(fecha_str) | |
| if ( | |
| cuota and recomendacion and | |
| "No encontrada" not in cuota and | |
| "No encontrada" not in recomendacion and | |
| fecha_partido is not None and | |
| fecha_partido >= ahora | |
| ): | |
| partidos.append({ | |
| "equipoA": row.get("Equipo A", ""), | |
| "equipoB": row.get("Equipo B", ""), | |
| "fecha": fecha_str, | |
| "recomendacion": recomendacion, | |
| "cuota": cuota | |
| }) | |
| # NO hagas random.sample aqu铆 | |
| except Exception as e: | |
| partidos = [] | |
| return render_template( | |
| "acca.html", | |
| partidos=partidos, | |
| csvs=csvs, | |
| actual=nombre | |
| ) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |