Spaces:
Paused
Paused
File size: 2,129 Bytes
968add6 c093882 2e2ac19 c093882 2e2ac19 92d23f8 2e2ac19 8bc42cc 2e2ac19 8bc42cc e1b1541 968add6 c093882 2e2ac19 c3cb9f1 2e2ac19 8bc42cc 9445a2a 8bc42cc 9445a2a 8bc42cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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."
|