jerryboy1031 / googlemap-link

To generate a Google Maps link for a location using the Google Maps API in Python, and access the address
MIT License
1 stars 0 forks source link

`opening_hours` is a bool, not the full information about the opening time #3

Open jerryboy1031 opened 1 year ago

jerryboy1031 commented 1 year ago

opening_hours field in the Google Places API response contains a bool value indicating whether the place is currently open or not ('open_now': True or 'open_now': False)

jerryboy1031 commented 1 year ago

Use the Google Places details API instead, and rewrite the code to:

def get_spot_details(place_id,api_key): fields = "name,rating,formatted_address,opening_hours,price_level,photos url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&fields={fields}&key={api_key}" payload={} headers = {} response = requests.request("GET", url, headers=headers, data=payload)

# Check if the request was successful
if response.status_code == 200:
    # Parse the JSON response
    data = response.json()

    if data['status'] == 'OK':
        # Extract the spot details
        spot = data["result"]
        # attributes--------------
        name = spot['name']
        address = spot['formatted_address']
        opening_hours = spot['opening_hours']['weekday_text']
        price_level = spot.get('price_level')
        rating = spot.get('rating')
        photos = spot.get('photos')

        # Display the information--------
        print("Spot:", name)
        print("Address:", address)
        # opening hour
        for opening_time in opening_hours:
            print(opening_time)
            #print(opening_time.encode('ascii', 'ignore').decode('ascii'))

        # ticket price    
        if price_level is not None:
            print("Price Level:", price_level)
        else:
            print("Price Level: free")

        #rating
        if rating is not None:
            print("Rating:", rating)
        else:
            print("No rating")
        #photo
        if photos is not None:
            # Retrieve the photo reference and construct the photo URL
            photo_reference = photos[0]['photo_reference']
            photo_url = f"https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference={photo_reference}&key={api_key}"
            print("Photo URL:", photo_url)

        else:
            print("No photo available.")
    else:
        print("Error:", data['status'])
else:
    print("Failed to retrieve data.")

`