import mariadb import requests import json import datetime # Function to convert date string to MySQL date format def convert_date(date_str): # Parse the date string assuming it's in 'DD/MM/YYYY' format date_obj = datetime.datetime.strptime(date_str, '%d/%m/%Y') # Format the date object into 'YYYY-MM-DD' format return date_obj.strftime('%Y-%m-%d') # Conexión a la base de datos MySQL db = mariadb.connect( host="localhost", port=3306, user="********", password="**********", database="BCRAstats" ) cursor = db.cursor() # URL de la API de Estadísticas del BCRA url = "https://api.bcra.gob.ar/estadisticas/v1/principalesvariables" try: # Realiza la solicitud GET a la API response = requests.get(url, verify=False) # Verifica que la respuesta sea exitosa (código 200) if response.status_code == 200: # Convierte la respuesta JSON en un diccionario data = json.loads(response.text) # Itera sobre los datos y ejecuta consultas INSERT for item in data["results"]: id_bcra = item["idVariable"] # Inside the loop where you iterate over data["results"] valor_bcra_cleaned = item["valor"].replace('.', '') # Remove dots (thousands separators) valor_bcra_cleaned = valor_bcra_cleaned.replace(',', '.') # Replace commas with dots (decimal separators) valor_bcra_truncated = round(float(valor_bcra_cleaned), 5) # Round to 5 decimal places and convert to float fecha_bcra_formatted = convert_date(item["fecha"]) query = "INSERT INTO tabla_bcra (idvariable, fecha, valor) VALUES (?, ?, ?)" values = (id_bcra, fecha_bcra_formatted, valor_bcra_truncated) #print(values) cursor.execute(query, values) db.commit() # Confirmar los cambios print("Datos guardados en la base de datos") else: print(f"Error en la respuesta: {response.status_code}") except json.JSONDecodeError as e: print(f"Error al decodificar JSON: {e}") except requests.RequestException as e: print(f"Error en la solicitud HTTP: {e}") finally: # Cierra la conexión db.close()