Google My Business to Woosmap

  1. Prerequisites
  2. Google My Business
  3. Authorization and Credentials
  4. Business Locations
  5. Map to Woosmap Asset

In this tutorial you will learn how to connect to Google My Business using OAuth 2.0, call the Google API to get the data from your authorized account, convert and import them to Woosmap.

Prerequisites

To run this tutorial, you’ll need:

Google My Business

This product allows to manage your business locations on Google so that customers can find you through Search and Maps.

You can use the Google My Business API to create and edit locations in Google My Business. Before start coding your first client application managing your business locations, there are a few things you need to do, if you haven’t done them already. Follow the different steps described on the official documentation

Authorization and Credentials

Get authorization and retrieve credentials are previously described at the Google Sheets to Woosmap tutorial except that you need to turn on the Google My Business API instead of Google Sheets. Because the Google My Business API is not a public API, you can’t use API Key. You will need a client ID for a web application to proceed. Please note that to get authorization for a project, you need to complete and submit this access request form.

Business Locations

Location data is represented by the v3.accounts.locations collection. In our special case all we need is to list all of the locations for a given account. As the Google Sheets to Woosmap tutorial, we’ve implemented the Google API Discovery Service represented by self.service object.

Python
        def list_locations(self):
    return self.service.accounts().locations().list(name=self.account_name).execute()

    

Map to Woosmap Asset

Just for an example, here is the conversion for the openingHours data.

Python
        def find_timezone(asset):
    latlng = get_geometry(asset)
    timezone_name = ''
    try:
        lat = float(latlng['lat'])
        lng = float(latlng['lng'])
        timezone_name = tf.timezone_at(lng=lng, lat=lat)
        if timezone_name is None:
            timezone_name = tf.closest_timezone_at(lng=lng, lat=lat)
        return timezone_name

    except ValueError:
        print('Unable to Get the timezone for {latlng}'.format(latlng=latlng))
        timezone_name = 'Europe/Paris'

    finally:
        return {'timezone': timezone_name}


def get_regular_hours(asset):
    regular_hours = asset.get('regularHours', {})
    periods = regular_hours.get('periods', [])

    usual = {}
    week_days = [{'MONDAY': '1'}, {'TUESDAY': '2'}, {'WEDNESDAY': '3'}, {'THURSDAY': '4'}, {'FRIDAY': '5'},
                 {'SATURDAY': '6'},
                 {'SUNDAY': '7'}]
    if periods:
        for period in periods:
            for day in week_days:
                for key in day:
                    if period['openDay'] == key:
                        usual.setdefault(day[key], []).append({'start': period['openTime'], 'end': period['closeTime']})

    return {'usual': usual}


def get_special_hours(asset):
    special_hours = asset.get('specialHours', {})
    periods = special_hours.get('specialHourPeriods', [])

    special = {}
    if periods:
        for period in periods:
            start_date = period.get('startDate', '')
            if start_date:
                key = str(start_date.get('year')) + '-' + str(start_date.get('month')) + '-' + str(
                    start_date.get('day'))
                if period.get('isClosed', False):
                    special.setdefault(key, [])
                else:
                    special.setdefault(key, []).append({'start': period['openTime'], 'end': period['closeTime']})

    return {'special': special}


def get_hours(asset):
    return dict(find_timezone(asset).items() + get_regular_hours(asset).items() + get_special_hours(asset).items())


    

For the whole data conversion, please refer to the GitHub Sample.

Related help articles