Spaces:
Building
Building
Create pixabay_api.py
Browse files- pixabay_api.py +31 -0
pixabay_api.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def search_pixabay(query, num_results=5, min_width=1280, min_height=720, lang="es"):
|
5 |
+
api_key = os.getenv("PIXABAY_API_KEY")
|
6 |
+
if not api_key:
|
7 |
+
raise ValueError("La variable de entorno PIXABAY_API_KEY no está configurada.")
|
8 |
+
|
9 |
+
if not query.strip():
|
10 |
+
raise ValueError("La consulta no puede estar vacía.")
|
11 |
+
|
12 |
+
url = "https://pixabay.com/api/videos/"
|
13 |
+
params = {
|
14 |
+
"key": api_key,
|
15 |
+
"q": query,
|
16 |
+
"lang": lang,
|
17 |
+
"min_width": min_width,
|
18 |
+
"min_height": min_height,
|
19 |
+
"per_page": num_results,
|
20 |
+
"video_type": "film"
|
21 |
+
}
|
22 |
+
response = requests.get(url, params=params)
|
23 |
+
|
24 |
+
if response.status_code != 200:
|
25 |
+
raise Exception(f"Error al buscar en Pixabay: {response.status_code} - {response.text}")
|
26 |
+
|
27 |
+
data = response.json()
|
28 |
+
if "hits" not in data or not data["hits"]:
|
29 |
+
raise Exception("No se encontraron videos relevantes para la consulta proporcionada.")
|
30 |
+
|
31 |
+
return [video["videos"]["large"]["url"] for video in data["hits"]]
|