| import os
|
| import aiohttp
|
| import logging
|
| from typing import List, Optional
|
| from dotenv import load_dotenv
|
|
|
|
|
| load_dotenv()
|
|
|
|
|
| GOOGLE_SEARCH_API_KEY = os.getenv("GOOGLE_SEARCH_API_KEY")
|
| GOOGLE_SEARCH_ENGINE_ID = os.getenv("GOOGLE_SEARCH_ENGINE_ID")
|
|
|
| logger = logging.getLogger(__name__)
|
|
|
| async def fetch_hotel_images_from_google(
|
| session: aiohttp.ClientSession,
|
| hotel_name: str,
|
| destination: str,
|
| max_results: int = 5
|
| ) -> List[str]:
|
| """
|
| Fetch hotel images using Google Custom Search API as a fallback
|
| when scraping fails to return any images.
|
|
|
| Args:
|
| session: aiohttp client session
|
| hotel_name: Name of the hotel to search for
|
| destination: Location/destination of the hotel
|
| max_results: Maximum number of images to return (default: 5)
|
|
|
| Returns:
|
| List of image URLs
|
| """
|
| if not GOOGLE_SEARCH_API_KEY or not GOOGLE_SEARCH_ENGINE_ID:
|
| logger.error("Google Search API credentials not configured")
|
| return []
|
|
|
|
|
| search_query = f"{hotel_name} {destination} hotel"
|
|
|
|
|
| url = "https://www.googleapis.com/customsearch/v1"
|
|
|
|
|
| params = {
|
| 'q': search_query,
|
| 'cx': GOOGLE_SEARCH_ENGINE_ID,
|
| 'key': GOOGLE_SEARCH_API_KEY,
|
| 'searchType': 'image',
|
| 'num': max_results,
|
| 'imgSize': 'large',
|
| 'imgType': 'photo',
|
| 'safe': 'active'
|
| }
|
|
|
| try:
|
| async with session.get(url, params=params) as response:
|
| if response.status == 200:
|
| data = await response.json()
|
|
|
|
|
| image_urls = []
|
|
|
| if 'items' in data:
|
| for item in data['items']:
|
| if 'link' in item:
|
| image_urls.append(item['link'])
|
|
|
| logger.info(f"Google API returned {len(image_urls)} images for {hotel_name} in {destination}")
|
| return image_urls
|
| else:
|
| error_data = await response.text()
|
| logger.error(f"Google API error: {response.status} - {error_data}")
|
| return []
|
| except Exception as e:
|
| logger.error(f"Error fetching hotel images from Google: {e}")
|
| return [] |