|
@@ -2,7 +2,7 @@ import json
|
|
|
from requests import get, put
|
|
|
from gauth.auth_utils import get_gmb_id, get_auth_header
|
|
|
from gauth.location_utils import get_all_location_ids
|
|
|
-from django.db.models import Max
|
|
|
+# from django.db.models import Max
|
|
|
from .models import Review, Reply
|
|
|
from gauth.models import Location
|
|
|
|
|
@@ -10,12 +10,12 @@ from django.utils import timezone
|
|
|
|
|
|
|
|
|
STAR_REVIEW_NUM = {'STAR_RATING_UNSPECIFIED': 0, 'ONE': 1, 'TWO': 2, 'THREE': 3, 'FOUR': 4, 'FIVE': 5}
|
|
|
-USER, GMB_ID = get_gmb_id()
|
|
|
|
|
|
|
|
|
-def get_review_list_url(loc_id, next_page_token=''):
|
|
|
+def get_review_list_url(location_id, next_page_token=''):
|
|
|
# An helper function that make a url that need to consume GMB review api
|
|
|
- return 'https://mybusiness.googleapis.com/v4/accounts/' + GMB_ID + '/locations/' + loc_id + '/reviews?pageToken='+next_page_token
|
|
|
+ _, account_id = get_gmb_id()
|
|
|
+ return f'https://mybusiness.googleapis.com/v4/accounts/{account_id}/locations/{location_id}/reviews?pageToken='+next_page_token
|
|
|
|
|
|
|
|
|
def get_reply_url(location_id, review_id):
|
|
@@ -37,38 +37,38 @@ def reply_review(review, replied_text):
|
|
|
return response
|
|
|
|
|
|
|
|
|
-def get_max_date(loc_id):
|
|
|
- '''
|
|
|
- find the maximum date of any particular location. the latest date
|
|
|
- :param loc_id: Integer -> Globally unique ID for google my business locations.
|
|
|
- :return: DateTime Obj -> The latest reviews date of that location.
|
|
|
- '''
|
|
|
- # Function that takes a location id and return the latest updated review.
|
|
|
- largest = Review.objects.filter(location_id=loc_id).aggregate(Max('create_time'))['create_time__max']
|
|
|
- max_date = largest if largest else '1970-01-11 17:41:17.532740'
|
|
|
- return max_date
|
|
|
+# def get_max_date(loc_id):
|
|
|
+# '''
|
|
|
+# find the maximum date of any particular location. the latest date
|
|
|
+# :param loc_id: Integer -> Globally unique ID for google my business locations.
|
|
|
+# :return: DateTime Obj -> The latest reviews date of that location.
|
|
|
+# '''
|
|
|
+# # Function that takes a location id and return the latest updated review.
|
|
|
+# largest = Review.objects.filter(location_id=loc_id).aggregate(Max('create_time'))['create_time__max']
|
|
|
+# max_date = largest if largest else '1970-01-11 17:41:17.532740'
|
|
|
+# return max_date
|
|
|
|
|
|
|
|
|
-def filter_unrecorded_review_by_date(reviews, max_date):
|
|
|
- # A function that return only those reviews whose has larger value than
|
|
|
- # the max create_time value in review database.
|
|
|
- filtered_reviews = []
|
|
|
- for rev in reviews:
|
|
|
- if rev['createTime'] >= str(max_date):
|
|
|
- filtered_reviews.append(rev)
|
|
|
- return filtered_reviews
|
|
|
+# def filter_unrecorded_review_by_date(reviews, max_date):
|
|
|
+# # A function that return only those reviews whose has larger value than
|
|
|
+# # the max create_time value in review database.
|
|
|
+# filtered_reviews = []
|
|
|
+# for rev in reviews:
|
|
|
+# if rev['createTime'] >= str(max_date):
|
|
|
+# filtered_reviews.append(rev)
|
|
|
+# return filtered_reviews
|
|
|
|
|
|
|
|
|
-def insert_review_into_database(unrecorded_reviews, loc):
|
|
|
+def insert_review_into_database(reviews, loc):
|
|
|
'''
|
|
|
Insert reviews to database.
|
|
|
:param unrecorded_reviews: all reviews for location.
|
|
|
:param loc: location that unrecorded_reviews belongs to.
|
|
|
:return: It insert all reviews if it is not exits in database and return nothing.
|
|
|
'''
|
|
|
- for review in unrecorded_reviews:
|
|
|
+ for review in reviews:
|
|
|
review_id = review.get('reviewId')
|
|
|
- # Check the review already in database then We don't need to store again.
|
|
|
+ # Check the review already exists in database then We don't need to store again.
|
|
|
rev = Review.objects.filter(pk=review_id).first()
|
|
|
if rev:
|
|
|
continue
|
|
@@ -137,23 +137,23 @@ def update_location_data(loc, average_rating, total_reviews, total_reviews_db):
|
|
|
loc.save()
|
|
|
|
|
|
|
|
|
-def fetch_all_review(loc_id):
|
|
|
- loc = Location.objects.get(pk=loc_id)
|
|
|
- max_date = get_max_date(loc_id)
|
|
|
- headers = get_auth_header()
|
|
|
- url = get_review_list_url(loc_id)
|
|
|
- res = get(url, headers=headers)
|
|
|
- if res.status_code == 401:
|
|
|
- return
|
|
|
- data = res.json()
|
|
|
- reviews = data['reviews']
|
|
|
- average_rating = data.get('averageRating')
|
|
|
- total_reviews = data.get('totalReviewCount')
|
|
|
- unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
|
|
|
- if len(unrecorded_reviews) != 0:
|
|
|
- insert_review_into_database(unrecorded_reviews, loc)
|
|
|
- total_reviews_db = Review.objects.filter(location_id=loc_id).count()
|
|
|
- update_location_data(loc, average_rating, total_reviews, total_reviews_db)
|
|
|
+# def fetch_all_review(loc_id):
|
|
|
+# loc = Location.objects.get(pk=loc_id)
|
|
|
+# max_date = get_max_date(loc_id)
|
|
|
+# headers = get_auth_header()
|
|
|
+# url = get_review_list_url(loc_id)
|
|
|
+# res = get(url, headers=headers)
|
|
|
+# if res.status_code == 401:
|
|
|
+# return
|
|
|
+# data = res.json()
|
|
|
+# reviews = data['reviews']
|
|
|
+# average_rating = data.get('averageRating')
|
|
|
+# total_reviews = data.get('totalReviewCount')
|
|
|
+# unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
|
|
|
+# if len(unrecorded_reviews) != 0:
|
|
|
+# insert_review_into_database(unrecorded_reviews, loc)
|
|
|
+# total_reviews_db = Review.objects.filter(location_id=loc_id).count()
|
|
|
+# update_location_data(loc, average_rating, total_reviews, total_reviews_db)
|
|
|
|
|
|
|
|
|
def fetch_last_20_reviews(loc_id):
|
|
@@ -161,8 +161,6 @@ def fetch_last_20_reviews(loc_id):
|
|
|
headers = get_auth_header()
|
|
|
url = get_review_list_url(loc_id)+'&pageSize=20'
|
|
|
res = get(url, headers=headers)
|
|
|
- if res.status_code == 401:
|
|
|
- return
|
|
|
data = res.json()
|
|
|
reviews = data['reviews']
|
|
|
average_rating = data.get('averageRating')
|