File size: 2,648 Bytes
28df1e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 []