Justin Priede
Justin Priede

Reputation: 119

Grabbing opening hours from google places API

I am using this python library to grab a response from the google places API.

Using the places function from the above library returns the object you see below. You can see that is just gives me a boolean on whether the restaurant is open or closed. How can I see the hours it is open for each day of the week? If this library isnt capable of it, can anyone show me an example that is?

Here is the line of code making the request for full context.

import googlemaps # https://googlemaps.github.io/google-maps-services-python/docs/index.html
gmaps = googlemaps.Client(key='apiKey')
response = gmaps.places(query=charlestonBars[idx], location=charleston)
[   {   'business_status': 'OPERATIONAL',
        'formatted_address': '467 King St, Charleston, SC 29403, United States',
        'geometry': {   'location': {'lat': 32.7890988, 'lng': -79.9386229},
                        'viewport': {   'northeast': {   'lat': 32.79045632989271,
                                                         'lng': -79.93725907010727},
                                        'southwest': {   'lat': 32.78775667010727,
                                                         'lng': -79.93995872989272}}},
        'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/bar-71.png',
        'icon_background_color': '#FF9E67',
        'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/bar_pinlet',
        'name': "A.C.'s Bar & Grill",
------->'opening_hours': {'open_now': True},
        'photos': [   {   'height': 1816,
                          'html_attributions': [   '<a '
                                                   'href="https://maps.google.com/maps/contrib/106222166168758671498">Lisa '
                                                   'Royce</a>'],
                          'photo_reference': 'ARywPAKpP_eyNL_y625xWYrQvSjAI91TzEx4XgT1rwCxjFyQjEAwZb2ha9EgE2RcKJalrZhjp0yTWa6QqvPNU9c7GeNBTDtzXVI0rHq2RXtTySGu8sjcB76keFugOmsl1ix4NnDVh0NO0vt_PO3nIZ-R-ytOzzIRhJgPAJd3SxKNQNfEyVp5',
                          'width': 4032}],
        'place_id': 'ChIJk6nSrWt6_ogRE9KiNXVG5KA',
        'plus_code': {   'compound_code': 'Q3Q6+JG Charleston, South Carolina',
                         'global_code': '8742Q3Q6+JG'},
        'price_level': 1,
        'rating': 4.3,
        'reference': 'ChIJk6nSrWt6_ogRE9KiNXVG5KA',
        'types': [   'bar',
                     'restaurant',
                     'point_of_interest',
                     'food',
                     'establishment'

Upvotes: 2

Views: 1541

Answers (1)

Yrll
Yrll

Reputation: 2576

What you need to use is the Place Details of Places API

Before we proceed with the solution, we must understand that there's a difference in the result of a Place Search and a Place Details request.

According to the documentation:

"Place Search requests and Place Details requests do not return the same fields. Place Search requests return a subset of the fields that are returned by Place Details requests. If the field you want is not returned by Place Search, you can use Place Search to get a place_id, then use that Place ID to make a Place Details request."

Now what you used on your code according to the python library documentation is the places(*args, **kwargs) which is the Place Search.

The Google Maps API documentation you provided on your comment above where you can get the expected result of hours each day is from Place Details which is the place(*args, **kwargs) from the python library documentation.

As quoted above, to request the detail of a place, you need the place_id which you can get by doing a Places Search request like you did on your question. So what you only need to do is get the place_id of the location you want through Place Search, then use that place_id to get the Place Details result that includes the opening_hours field result.

Here's what it looks like on a python code:

# I used pprint to print the result on the console

from pprint import pprint
import googlemaps #import googlemaps python library

# Instantiate the client using your own API key

API_KEY = 'API_KEY'
map_client = googlemaps.Client(API_KEY)

# Store the location you want, in my case, I tried using 'Mall of Asia'

location_name = 'Mall of Asia'

# Instantiate Place Search request using `places(*args, **kwargs)` from the library
# Use the `stored location` as an argument for query

place_search = map_client.places(location_name)

# Get the search results

search_results = place_search.get('results')

# Store the place_id from the result to be used

place_id = (search_results[0]['place_id'])

# Instantiate Place Details request using the `place(*args, **kwargs)` from the library
# Use the stored place_id as an argument for the request

place_details = map_client.place(place_id)

# Get the Place Details result

details_results = place_details.get('result')

# Print the result specifying what you only need which is the `opening_hours` field

pprint(details_results['opening_hours'])

The result of this sample request would be this:

{'open_now': False,
 'periods': [{'close': {'day': 0, 'time': '2200'},
              'open': {'day': 0, 'time': '1000'}},
             {'close': {'day': 1, 'time': '2200'},
              'open': {'day': 1, 'time': '1000'}},
             {'close': {'day': 2, 'time': '2200'},
              'open': {'day': 2, 'time': '1000'}},
             {'close': {'day': 3, 'time': '2200'},
              'open': {'day': 3, 'time': '1000'}},
             {'close': {'day': 4, 'time': '2200'},
              'open': {'day': 4, 'time': '1000'}},
             {'close': {'day': 5, 'time': '2200'},
              'open': {'day': 5, 'time': '1000'}},
             {'close': {'day': 6, 'time': '2200'},
              'open': {'day': 6, 'time': '1000'}}],
 'weekday_text': ['Monday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Tuesday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Wednesday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Thursday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Friday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Saturday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Sunday: 10:00\u202fAM\u2009–\u200910:00\u202fPM']}

That would be all and I hope this helps! Feel free to comment below if this does not meet your expected results.

Upvotes: 2

Related Questions