location_utils.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from requests import get
  2. from .models import Location
  3. from django.conf import settings
  4. from .auth_utils import get_gmb_id
  5. ACCESS_TOKEN_URI = settings.HOST_URI + '/token'
  6. def get_all_location_ids():
  7. locations = Location.objects.only('location_id')
  8. ids = [loc.location_id for loc in locations]
  9. '''
  10. There is a location which we don't need to add into database.
  11. It is a Medical Billing Service, That's why it is nothing to do with review.
  12. '''
  13. if '5397588228065547694' in ids:
  14. ids.remove('5397588228065547694')
  15. return ids
  16. def populate_locations():
  17. access_token = get(ACCESS_TOKEN_URI).text
  18. user, gmb_id = get_gmb_id()
  19. url = 'https://mybusiness.googleapis.com/v4/accounts/' + gmb_id + '/locations'
  20. headers = {
  21. 'authorization': 'Bearer ' + access_token,
  22. 'content-type': 'application/json'
  23. }
  24. res = get(url, headers=headers).json()
  25. locations = res['locations']
  26. for loc in locations:
  27. # loc['name'] = 'accounts/103266181421855655295/locations/8916258876770296726'
  28. loc_id = loc['name'].split('/')[-1]
  29. location_ids = get_all_location_ids()
  30. location_ids.append('5397588228065547694')
  31. if loc_id in location_ids:
  32. continue
  33. loc_name = loc['address']['locality']
  34. loc_website = loc['websiteUrl']
  35. loc_display = loc['primaryCategory']['displayName']
  36. location = Location.objects.create(location_id=loc_id, location_name=loc_name, website_url=loc_website, display_name=loc_display, owner=user)
  37. location.save()
  38. # loc_display = loc_website.spilt('/')[-1]