diff --git a/server/djangoapp/restapis.py b/server/djangoapp/restapis.py index e4df2ca6fe..d365689526 100644 --- a/server/djangoapp/restapis.py +++ b/server/djangoapp/restapis.py @@ -11,20 +11,24 @@ def get_request(url, **kwargs): print(kwargs) print("GET from {} ".format(url)) try: - # headers = {'Content-Type': 'application/json'} - # Call get method of requests library with URL and parameters - # params=kwargs - response = requests.get(url, headers={'Content-Type': 'application/json'}, - params=kwargs) - # if api_key: - # response = requests.get(url, params=params, headers={'Content-Type': 'application/json'}, - # auth=HTTPBasicAuth('apikey', api_key)) - # else: - # request.get(url, headers=headers, params=kwargs) - except: - # If any error occurs + if 'api' in kwargs: + response = requests.get(url, params=kwargs, headers={'Content-Type': 'application/json'}, + auth=HTTPBasicAuth('apikey', api_key)) + print(response,'api') + elif kwargs: + response = requests.get( + url, headers={'Content-Type': 'application/json'}, params=kwargs) + print(params,"second") + else: + response = requests.get( + url, headers={'Content-Type': 'application/json'}) + print('third') + + except Exception as e: + print("Network exception occurred") - status_code = response.status_code + + status_code=response.status_code print("With status {} ".format(status_code)) json_data = json.loads(response.text) return json_data @@ -113,6 +117,7 @@ def get_dealer_reviews_from_cf(url, **kwargs): sentiment='', id=review.get("id", "") ) + review_obj.sentiment = analyze_review_sentiments(review_obj.review) results.append(review_obj) @@ -123,23 +128,16 @@ def get_dealer_reviews_from_cf(url, **kwargs): # def analyze_review_sentiments(text): # - Call get_request() with specified arguments # - Get the returned sentiment label such as Positive or Negative -def analyze_review_sentiments(dealerreview): - url = "https://api.us-east.natural-language-understanding.watson.cloud.ibm.com/instances/9de934ca-8c19-41c3-9897-0fb787b8b736" - api_key = "bGzxfI_dfm53PLexIyD2rBMlLwLy1r5s1fEOLgvRd0g4" # Replace with your IBM Watson Natural Language Understanding API key - version = "2021-03-25" # Replace with the desired version - - params = { - "text": dealerreview.review, - "version": version, - "features": "sentiment", - "return_analyzed_text": True - } - - response = get_request(url, params=params, api_key=api_key) - - if response and 'sentiment' in response: - sentiment = response['sentiment']['document']['label'] - return sentiment - else: - return None +def analyze_review_sentiments(dealer_review): + API_KEY = "bGzxfI_dfm53PLexIyD2rBMlLwLy1r5s1fEOLgvRd0g4" + NLU_URL = 'https://api.us-east.natural-language-understanding.watson.cloud.ibm.com/instances/9de934ca-8c19-41c3-9897-0fb787b8b736' + authenticator = IAMAuthenticator(API_KEY) + natural_language_understanding = NaturalLanguageUnderstandingV1( + version='2021-08-01', authenticator=authenticator) + natural_language_understanding.set_service_url(NLU_URL) + response = natural_language_understanding.analyze(text=dealer_review,language='en', features=Features( + sentiment=SentimentOptions(targets=[dealer_review]))).get_result() + label = json.dumps(response, indent=2) + label = response['sentiment']['document']['label'] + return(label) diff --git a/server/djangoapp/views.py b/server/djangoapp/views.py index 9baf02c9fd..1bbdb60ef0 100644 --- a/server/djangoapp/views.py +++ b/server/djangoapp/views.py @@ -113,7 +113,8 @@ def get_dealer_details(request, dealer_id): # Concat all review's name review_names = ' '.join([review.sentiment for review in reviews]) # Return a list of dealer short name - return HttpResponse(reviews) + print(review_names) + return HttpResponse(review_names)