location_utils.py 1013 B

123456789101112131415161718192021222324252627
  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 = Location.objects.filter(location_id=loc_id).first()
  17. if not location:
  18. location = Location.objects.create(
  19. location_id=loc_id,
  20. location_name=loc_name,
  21. website_url=loc_website,
  22. owner=user
  23. )
  24. if location:
  25. print('A new location has been created!')