Spaces:
Runtime error
Runtime error
File size: 4,315 Bytes
cff2d97 7e17f7c cff2d97 7e17f7c cff2d97 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import os
import zipfile
import json
import tempfile
import shutil
from datetime import datetime
from flask import Flask, render_template, request
from nucleo import cazar_datos_meta
import spaces # <-- Importamos la librería de seguridad de Hugging Face
# ==========================================
# LA TRAMPA PARA MANTENER ZERO GPU ENCENDIDO
# ==========================================
@spaces.GPU
def engañar_a_hugging_face():
# Esta función no hace nada, pero el sistema la lee y dice:
# "Ah, sí va a usar la tarjeta, lo dejo encendido".
pass
# 1. Nombramos a nuestra app como flask_app
flask_app = Flask(__name__)
def buscar_archivos_json(directorio):
ruta_seguidores = None
ruta_siguiendo = None
for raiz, _, archivos in os.walk(directorio):
for archivo in archivos:
if archivo.startswith('followers') and archivo.endswith('.json'):
ruta_seguidores = os.path.join(raiz, archivo)
elif archivo.startswith('following') and archivo.endswith('.json'):
ruta_siguiendo = os.path.join(raiz, archivo)
return ruta_seguidores, ruta_siguiendo
@flask_app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'archivo_zip' not in request.files:
return "No se subió ningún archivo", 400
archivo = request.files['archivo_zip']
if archivo.filename == '':
return "El archivo no tiene nombre", 400
if archivo and archivo.filename.endswith('.zip'):
temp_dir = tempfile.mkdtemp()
zip_path = os.path.join(temp_dir, 'datos.zip')
try:
archivo.save(zip_path)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
ruta_seguidores, ruta_siguiendo = buscar_archivos_json(temp_dir)
if not ruta_seguidores or not ruta_siguiendo:
shutil.rmtree(temp_dir)
return "❌ Error: El ZIP no contiene la información de Seguidores y Seguidos.", 400
with open(ruta_seguidores, 'r', encoding='utf-8') as f:
data_seguidores = json.load(f)
with open(ruta_siguiendo, 'r', encoding='utf-8') as f:
data_siguiendo = json.load(f)
seguidores_info = cazar_datos_meta(data_seguidores)
siguiendo_info = cazar_datos_meta(data_siguiendo)
nombres_seguidores = set(seguidores_info.keys())
lista_final = []
for usuario, ts in siguiendo_info.items():
if usuario not in nombres_seguidores:
fecha = datetime.fromtimestamp(ts).strftime('%d/%m/%Y') if ts else "Desconocida"
lista_final.append({'usuario': usuario, 'fecha': fecha, 'ts': ts})
lista_final.sort(key=lambda x: x['ts'], reverse=True)
resultados = {
'total_siguiendo': len(siguiendo_info),
'total_seguidores': len(nombres_seguidores),
'mutuos': len(nombres_seguidores.intersection(siguiendo_info.keys())),
'traidores': lista_final
}
shutil.rmtree(temp_dir)
return render_template('index.html', resultados=resultados)
except Exception as e:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
return f"Ocurrió un error técnico: {str(e)}", 500
return render_template('index.html', resultados=None)
# ==========================================
# EL CABALLO DE TROYA PARA EL SERVIDOR
# ==========================================
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
import uvicorn
app = FastAPI()
app.mount("/", WSGIMiddleware(flask_app))
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=7860) |