| import json |
| import os |
| import requests |
| from modules import log_module |
| activo = False |
| try: |
| api_key = os.getenv("WEATHERAPI_KEY") |
| except: |
| api_key = False |
| if not api_key: |
| activo = False |
|
|
| user_text = "Consultando el clima" |
|
|
| info = { |
| "type": "function", |
| "function": { |
| "name": "obtener_clima", |
| "description": "Obtiene el clima de una ubicación", |
| "parameters": { |
| "type": "object", |
| "properties": { |
| "ubicacion": { |
| "type": "string", |
| "description": "La ciudad, estado y pais, e.g. Caracas, Distrito Capital, Venezuela", |
| }, |
| "unidad": { |
| "type": "string", |
| "enum": ["celsius"] |
| }, |
| }, |
| "required": ["ubicacion", "unidad"], |
| } |
| } |
| } |
|
|
| def ejecutar(ubicacion, unidad="celsius"): |
| url = f"https://api.weatherapi.com/v1/current.json?key={api_key}&q={ubicacion}&aqi=no" |
| elementos = ["temp_c", "is_day", "condition", "wind_kph", "wind_dir", "precip_mm", "humidity", "cloud", "feelslike_c", "vis_km"] |
| try: |
| rq = requests.get(url, timeout=3) |
| if rq.status_code!=200: |
| return json.dumps({"status": "api failed"}) |
| clima = json.loads(rq.text) |
| clima = {k: v for k, v in clima["current"].items() if k in elementos} |
| return json.dumps(clima) |
| except Exception as e: |
| log_module.error(repr(e)) |
| return json.dumps({"status": "api failed"}) |