Skip to content

Commit

Permalink
get_request updated
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhanduzel committed Dec 10, 2023
1 parent fe319ef commit b0bd7a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
62 changes: 30 additions & 32 deletions server/djangoapp/restapis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand All @@ -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)

3 changes: 2 additions & 1 deletion server/djangoapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand Down

0 comments on commit b0bd7a6

Please sign in to comment.