location_utils.py 906 B

123456789101112131415161718192021
  1. from requests import get
  2. from .models import Location
  3. from gauth.auth_utils import get_auth_header
  4. from .auth_utils import get_gmb_id
  5. def populate_locations():
  6. headers = get_auth_header()
  7. user, gmb_id = get_gmb_id()
  8. url = 'https://mybusiness.googleapis.com/v4/accounts/' + gmb_id + '/locations'
  9. res = get(url, headers=headers).json()
  10. locations = res['locations']
  11. for loc in locations:
  12. # loc['name'] = 'accounts/103266181421855655295/locations/8916258876770296726'
  13. loc_id = loc['name'].split('/')[-1]
  14. loc_name = loc['address']['locality']
  15. loc_website = loc['websiteUrl']
  16. location, created = Location.objects.update_or_create(location_id=loc_id, location_name=loc_name,
  17. website_url=loc_website, owner=user)
  18. if created:
  19. print('A new location has been created!')