Spaces:
Running
Running
| import os | |
| import socket | |
| import time | |
| def obtenAccesoHF(): | |
| if local_check(): | |
| print("Estoy en LOCAL...") | |
| import bridges | |
| llave = bridges.llave | |
| else: | |
| print("Estoy en REMOTO...") | |
| llave = os.getenv("llave") | |
| print("Ésto es llave:", llave) | |
| return llave | |
| def local_check(): | |
| hostname = socket.gethostname() | |
| #r-moibe-nowme | |
| print("Dentro de local_check... , el hostname es: ", hostname) | |
| if "r-moibe-nowme" in hostname: | |
| print("Ejecutando en el servidor") | |
| return False | |
| else: | |
| print("Ejecutando en local") | |
| return True | |
| def obtenUltimoTimestamp(): | |
| """ | |
| Obtiene el último timestamp de renovación guardado. | |
| """ | |
| archivo_ruta = "archivos/last_timestamp.txt" | |
| try: | |
| with open(archivo_ruta, 'r') as archivo: | |
| contenido = archivo.readline().strip() | |
| ultimo_timestamp = int(contenido) | |
| return ultimo_timestamp | |
| except FileNotFoundError: | |
| print(f"Error: El archivo '{archivo_ruta}' no fue encontrado.") | |
| return | |
| except ValueError: | |
| print(f"Error: El contenido del archivo '{archivo_ruta}' no es un número entero válido.") | |
| return | |
| def nuevoDia(): | |
| """ | |
| Compara dos timestamps (en formato string ISO) y devuelve True si han | |
| pasado más de 24 horas entre ellos, False en caso contrario. | |
| """ | |
| timestamp_original = obtenUltimoTimestamp() | |
| timestamp_actual = int(time.time()) | |
| try: | |
| segundos_en_24_horas = 24 * 60 * 60 | |
| diferencia_en_segundos = abs(timestamp_actual - timestamp_original) | |
| return diferencia_en_segundos > segundos_en_24_horas | |
| except ValueError: | |
| print("Error: Formato de timestamp incorrecto.") | |
| return False | |
| def obtenSegundosDisponibles(): | |
| print("Estoy en obten segundos disponibles...") | |
| if nuevoDia() == True: | |
| renuevaSegundosDisponibles() | |
| archivo_ruta = "archivos/seconds_available.txt" | |
| try: | |
| # Leer el número actual de segundos disponibles | |
| with open(archivo_ruta, 'r') as archivo: | |
| contenido = archivo.readline().strip() | |
| segundos_disponibles = int(contenido) | |
| return segundos_disponibles | |
| except FileNotFoundError: | |
| print(f"Error: El archivo '{archivo_ruta}' no fue encontrado.") | |
| return | |
| except ValueError: | |
| print(f"Error: El contenido del archivo '{archivo_ruta}' no es un número entero válido.") | |
| return | |
| def renuevaSegundosDisponibles(): | |
| print("Estoy en renuevaSegundosDisponibles...") | |
| archivo_ruta = "archivos/seconds_available.txt" | |
| # Guardar el nuevo número en el archivo | |
| try: | |
| with open(archivo_ruta, 'w') as archivo: | |
| archivo.write(str(1500)) | |
| print(f"Se renovaron los 1500 segundos disponibles.") | |
| renuevaTimestampActual() | |
| except Exception as e: | |
| print(f"Error al escribir en el archivo '{archivo_ruta}': {e}") | |
| def renuevaTimestampActual(): | |
| print("Estoy en renuevatimestmap actual...") | |
| archivo_ruta = "archivos/last_timestamp.txt" | |
| timestamp_actual = int(time.time()) | |
| # Guardar el nuevo número en el archivo | |
| try: | |
| with open(archivo_ruta, 'w') as archivo: | |
| archivo.write(str(timestamp_actual)) | |
| print(f"Se renovó por el timestamp de éste momento.") | |
| except Exception as e: | |
| print(f"Error al escribir en el archivo '{archivo_ruta}': {e}") | |
| def restaSegundosGPU(segundos): | |
| """ | |
| Lee el número de segundos disponibles desde seconds_available.txt, | |
| resta los segundos dados como parámetro y guarda el nuevo valor en el archivo. | |
| """ | |
| print("Estoy en resta segundos...") | |
| archivo_ruta = "archivos/seconds_available.txt" | |
| segundos_disponibles = obtenSegundosDisponibles() | |
| # Restar los segundos | |
| nuevos_segundos_disponibles = segundos_disponibles - segundos | |
| # Guardar el nuevo número en el archivo | |
| try: | |
| with open(archivo_ruta, 'w') as archivo: | |
| archivo.write(str(nuevos_segundos_disponibles)) | |
| print(f"Se restaron {segundos} segundos. Ahora quedan {nuevos_segundos_disponibles} segundos disponibles.") | |
| except Exception as e: | |
| print(f"Error al escribir en el archivo '{archivo_ruta}': {e}") |