ProductSummary / app.py
antfraia's picture
Update app.py
8bc42cc
def get_product_details(asin, domain):
payload = {
'source': 'amazon_product',
'domain': domain,
'query': asin,
'parse': True,
'context': [{'key': 'autoselect_variant', 'value': True}]
}
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
data = response.json()
# Check if 'results' and 'content' keys are present in the data
if 'results' in data and len(data['results']) > 0 and 'content' in data['results'][0]:
content = data['results'][0]['content']
product_info = {
"asin": content.get("asin", "N/A"),
"title": content.get("title", "N/A"),
"brand": content.get("brand", "N/A"),
"price": content.get("price", "N/A"),
"rating": content.get("rating", "N/A"),
"description": content.get("description", "N/A")
}
return json.dumps(product_info, indent=4)
else:
return "Failed to retrieve product details."
def get_reviews(asin, domain):
payload = {
'source': 'amazon_reviews',
'domain': domain,
'query': asin,
'parse': True
}
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
data = response.json()
# Check if 'results', 'content', and 'reviews' keys are present in the data
if 'results' in data and len(data['results']) > 0 and 'content' in data['results'][0] and 'reviews' in data['results'][0]['content']:
reviews = data['results'][0]['content']['reviews']
review_texts = "\n\n".join([review["content"] for review in reviews])
openai_payload = {
'prompt': f"Summarize the following reviews:\n\n{review_texts}",
'max_tokens': 150
}
headers = {
'Authorization': OPENAI_AUTH,
'Content-Type': 'application/json'
}
response = requests.post(OPENAI_ENDPOINT, headers=headers, json=openai_payload)
summary = response.json()["choices"][0]["text"].strip()
return summary
else:
return "Failed to retrieve reviews."