Update app.py
Browse files
app.py
CHANGED
@@ -40,11 +40,36 @@ def cleanup_temp_files():
|
|
40 |
def search_google_images(query, num_images=1):
|
41 |
"""Search for images using Google Custom Search API."""
|
42 |
try:
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
-
print(f"Error
|
48 |
return []
|
49 |
|
50 |
def download_image(url):
|
|
|
40 |
def search_google_images(query, num_images=1):
|
41 |
"""Search for images using Google Custom Search API."""
|
42 |
try:
|
43 |
+
api_key = os.getenv('GOOGLE_API_KEY')
|
44 |
+
cse_id = os.getenv('GOOGLE_CSE_ID')
|
45 |
+
|
46 |
+
# Imprimir informaci贸n de depuraci贸n (los ... son para no mostrar la key completa)
|
47 |
+
print(f"Buscando im谩genes para: {query}")
|
48 |
+
print(f"CSE ID configurado: {cse_id}")
|
49 |
+
print(f"API Key configurada: {api_key[:5]}...{api_key[-5:] if api_key else ''}")
|
50 |
+
|
51 |
+
service = build("customsearch", "v1", developerKey=api_key)
|
52 |
+
|
53 |
+
# Realizar la b煤squeda
|
54 |
+
result = service.cse().list(
|
55 |
+
q=query,
|
56 |
+
cx=cse_id,
|
57 |
+
searchType="image",
|
58 |
+
num=num_images,
|
59 |
+
safe='off' # Permitir todos los resultados
|
60 |
+
).execute()
|
61 |
+
|
62 |
+
# Verificar si hay resultados
|
63 |
+
if 'items' in result:
|
64 |
+
image_urls = [item['link'] for item in result.get('items', [])]
|
65 |
+
print(f"Encontradas {len(image_urls)} im谩genes")
|
66 |
+
return image_urls
|
67 |
+
else:
|
68 |
+
print("No se encontraron im谩genes")
|
69 |
+
return []
|
70 |
+
|
71 |
except Exception as e:
|
72 |
+
print(f"Error detallado en la b煤squeda de im谩genes: {str(e)}")
|
73 |
return []
|
74 |
|
75 |
def download_image(url):
|