| import os | |
| import requests | |
| def search_pixabay(query, num_results=5, min_width=1280, min_height=720, lang="es"): | |
| api_key = os.getenv("PIXABAY_API_KEY") | |
| if not api_key: | |
| raise ValueError("La variable de entorno PIXABAY_API_KEY no está configurada.") | |
| if not query.strip(): | |
| raise ValueError("La consulta no puede estar vacía.") | |
| url = "https://pixabay.com/api/videos/" | |
| params = { | |
| "key": api_key, | |
| "q": query, | |
| "lang": lang, | |
| "min_width": min_width, | |
| "min_height": min_height, | |
| "per_page": num_results, | |
| "video_type": "film" | |
| } | |
| response = requests.get(url, params=params) | |
| if response.status_code != 200: | |
| raise Exception(f"Error al buscar en Pixabay: {response.status_code} - {response.text}") | |
| data = response.json() | |
| if "hits" not in data or not data["hits"]: | |
| raise Exception("No se encontraron videos relevantes para la consulta proporcionada.") | |
| return [video["videos"]["large"]["url"] for video in data["hits"]] |