|
@@ -1,6 +1,6 @@
|
|
-from requests import get
|
|
|
|
-from django.conf import settings
|
|
|
|
-from gauth.auth_utils import get_gmb_id
|
|
|
|
|
|
+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 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 .models import Review, Reply
|
|
@@ -9,8 +9,6 @@ from gauth.models import Location
|
|
from django.utils import timezone
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
|
|
-ACCESS_TOKEN_URI = settings.HOST_URI + '/token'
|
|
|
|
-
|
|
|
|
STAR_REVIEW_NUM = {'STAR_RATING_UNSPECIFIED': 0, 'ONE': 1, 'TWO': 2, 'THREE': 3, 'FOUR': 4, 'FIVE': 5}
|
|
STAR_REVIEW_NUM = {'STAR_RATING_UNSPECIFIED': 0, 'ONE': 1, 'TWO': 2, 'THREE': 3, 'FOUR': 4, 'FIVE': 5}
|
|
USER, GMB_ID = get_gmb_id()
|
|
USER, GMB_ID = get_gmb_id()
|
|
|
|
|
|
@@ -20,22 +18,37 @@ def get_review_list_url(loc_id, next_page_token=''):
|
|
return 'https://mybusiness.googleapis.com/v4/accounts/' + GMB_ID + '/locations/' + loc_id + '/reviews?pageToken='+next_page_token
|
|
return 'https://mybusiness.googleapis.com/v4/accounts/' + GMB_ID + '/locations/' + loc_id + '/reviews?pageToken='+next_page_token
|
|
|
|
|
|
|
|
|
|
|
|
+def get_reply_url(location_id, review_id):
|
|
|
|
+ _, account_id = get_gmb_id()
|
|
|
|
+ return f'https://mybusiness.googleapis.com/v4/accounts/{account_id}/locations/{location_id}/reviews/{review_id}/reply'
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def reply_review(review, replied_text):
|
|
|
|
+ '''
|
|
|
|
+ reply a review with a put request.
|
|
|
|
+ :param review: review object -> a review which you want to reply.
|
|
|
|
+ :param replied_text: string -> The actual reply that you want to post.
|
|
|
|
+ :return:
|
|
|
|
+ '''
|
|
|
|
+ url = get_reply_url(review.location.location_id, review.review_id)
|
|
|
|
+ headers = get_auth_header()
|
|
|
|
+ payload = json.dumps({'comment': replied_text})
|
|
|
|
+ response = put(url, headers=headers, data=payload)
|
|
|
|
+ return response
|
|
|
|
+
|
|
|
|
+
|
|
def get_max_date(loc_id):
|
|
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.
|
|
# 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']
|
|
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'
|
|
max_date = largest if largest else '1970-01-11 17:41:17.532740'
|
|
return max_date
|
|
return max_date
|
|
|
|
|
|
|
|
|
|
-def get_auth_header():
|
|
|
|
- access_token = get(ACCESS_TOKEN_URI).text
|
|
|
|
- headers = {
|
|
|
|
- 'authorization': 'Bearer ' + access_token,
|
|
|
|
- 'content-type': 'application/json'
|
|
|
|
- }
|
|
|
|
- return headers
|
|
|
|
-
|
|
|
|
-
|
|
|
|
def filter_unrecorded_review_by_date(reviews, max_date):
|
|
def filter_unrecorded_review_by_date(reviews, max_date):
|
|
# A function that return only those reviews whose has larger value than
|
|
# A function that return only those reviews whose has larger value than
|
|
# the max create_time value in review database.
|
|
# the max create_time value in review database.
|
|
@@ -47,6 +60,12 @@ def filter_unrecorded_review_by_date(reviews, max_date):
|
|
|
|
|
|
|
|
|
|
def insert_review_into_database(unrecorded_reviews, loc):
|
|
def insert_review_into_database(unrecorded_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 unrecorded_reviews:
|
|
review_id = review.get('reviewId')
|
|
review_id = review.get('reviewId')
|
|
# Check the review already in database then We don't need to store again.
|
|
# Check the review already in database then We don't need to store again.
|
|
@@ -61,6 +80,7 @@ def insert_review_into_database(unrecorded_reviews, loc):
|
|
reviewer_name = reviewer.get('displayName')
|
|
reviewer_name = reviewer.get('displayName')
|
|
reviewer_photo = reviewer.get('profilePhotoUrl')
|
|
reviewer_photo = reviewer.get('profilePhotoUrl')
|
|
review_reply = review.get('reviewReply')
|
|
review_reply = review.get('reviewReply')
|
|
|
|
+ # Check if it is already replied.
|
|
if review_reply:
|
|
if review_reply:
|
|
replied_text = review_reply.get('comment')
|
|
replied_text = review_reply.get('comment')
|
|
create_time = review_reply.get('updateTime')
|
|
create_time = review_reply.get('updateTime')
|