review_utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from requests import get
  2. from django.conf import settings
  3. from gauth.auth_utils import get_gmb_id
  4. from gauth.location_utils import get_all_location_ids
  5. from django.db.models import Max
  6. from .models import Review, Reply
  7. from gauth.models import Location
  8. ACCESS_TOKEN_URI = settings.HOST_URI + '/token'
  9. STAR_REVIEW_NUM = {'STAR_RATING_UNSPECIFIED': 0, 'ONE': 1, 'TWO': 2, 'THREE': 3, 'FOUR': 4, 'FIVE': 5}
  10. USER, GMB_ID = get_gmb_id()
  11. def get_review_list_url(loc_id, next_page_token=''):
  12. # An helper function that make a url that need to consume GMB review api
  13. return 'https://mybusiness.googleapis.com/v4/accounts/' + GMB_ID + '/locations/' + loc_id + '/reviews?pageToken='+next_page_token
  14. def get_max_date(loc_id):
  15. # Function that takes a location id and return the latest updated review.
  16. largest = Review.objects.filter(location_id=loc_id).aggregate(Max('create_time'))['create_time__max']
  17. max_date = largest if largest else '1970-01-11 17:41:17.532740'
  18. return max_date
  19. def get_auth_header():
  20. access_token = get(ACCESS_TOKEN_URI).text
  21. headers = {
  22. 'authorization': 'Bearer ' + access_token,
  23. 'content-type': 'application/json'
  24. }
  25. return headers
  26. def filter_unrecorded_review_by_date(reviews, max_date):
  27. # A function that return only those reviews whose has larger value than
  28. # the max create_time value in review database.
  29. filtered_reviews = []
  30. for rev in reviews:
  31. if rev['createTime'] >= str(max_date):
  32. filtered_reviews.append(rev)
  33. return filtered_reviews
  34. def insert_review_into_database(unrecorded_reviews, loc):
  35. for review in unrecorded_reviews:
  36. review_id = review.get('reviewId')
  37. # Check the review already in database then We don't need to store again.
  38. rev = Review.objects.filter(pk=review_id).first()
  39. if rev:
  40. continue
  41. comment = review.get('comment')
  42. create_time = review.get('createTime')
  43. update_time = review.get('updateTime')
  44. star_rating = STAR_REVIEW_NUM[review.get('starRating')]
  45. reviewer = review.get('reviewer')
  46. reviewer_name = reviewer.get('displayName')
  47. reviewer_photo = reviewer.get('profilePhotoUrl')
  48. review_reply = review.get('reviewReply')
  49. if review_reply:
  50. replied_text = review_reply.get('comment')
  51. create_time = review_reply.get('updateTime')
  52. reply = Reply.objects.create(replied_text=replied_text, create_time=create_time)
  53. else:
  54. reply = None
  55. review = Review(
  56. review_id=review_id,
  57. comment=comment,
  58. create_time=create_time,
  59. update_time=update_time,
  60. star_rating=star_rating,
  61. reviewer_name=reviewer_name,
  62. reviewer_photo=reviewer_photo,
  63. location=loc,
  64. reply=reply
  65. )
  66. review.save()
  67. def fetch_all_review(loc_id):
  68. loc = Location.objects.get(pk=loc_id)
  69. max_date = get_max_date(loc_id)
  70. next_page_token = ''
  71. headers = get_auth_header()
  72. while True:
  73. url = get_review_list_url(loc_id, next_page_token)
  74. res = get(url, headers=headers)
  75. if res.status_code == 401:
  76. headers = get_auth_header()
  77. continue
  78. data = res.json()
  79. reviews = data['reviews']
  80. unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
  81. if len(unrecorded_reviews) != 0:
  82. insert_review_into_database(unrecorded_reviews, loc)
  83. next_page_token = data.get('nextPageToken')
  84. if next_page_token is None:
  85. break
  86. def populate_reviews():
  87. locations = get_all_location_ids()
  88. for loc_id in locations:
  89. fetch_all_review(loc_id)