File size: 873 Bytes
adc711c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import os

def search_products(keyword):

    url = "https://real-time-amazon-data.p.rapidapi.com/search"

    querystring = {
        "query": keyword,
        "country": "IN"
    }

    headers = {
        "X-RapidAPI-Key": os.getenv("RAPIDAPI_KEY"),
        "X-RapidAPI-Host": "real-time-amazon-data.p.rapidapi.com"
    }

    response = requests.get(url, headers=headers, params=querystring)

    return response.json()


def format_products(response):

    products = []

    try:
        items = response["data"]["products"][:3]

        for item in items:
            products.append({
                "name": item.get("product_title"),
                "price": item.get("product_price"),
                "image": item.get("product_photo"),
                "link": item.get("product_url")
            })

    except:
        pass

    return products