accomodation-info-api / services /utils /google_search_utils.py
garvitcpp's picture
Upload 27 files
28df1e8 verified
import os
import aiohttp
import logging
from typing import List, Optional
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Get API credentials from environment variables
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 []
# Construct the search query
search_query = f"{hotel_name} {destination} hotel"
# API endpoint
url = "https://www.googleapis.com/customsearch/v1"
# Parameters for the API request
params = {
'q': search_query,
'cx': GOOGLE_SEARCH_ENGINE_ID,
'key': GOOGLE_SEARCH_API_KEY,
'searchType': 'image',
'num': max_results,
'imgSize': 'large', # Prefer large images
'imgType': 'photo', # Only return photos, not illustrations
'safe': 'active' # Safe search
}
try:
async with session.get(url, params=params) as response:
if response.status == 200:
data = await response.json()
# Extract image URLs from the response
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 []