jeudi 30 mai 2019

Limiting the lenght of response with python requests

I'm setting up a Views that save data from an API into my database on a button click and I'm having a hard time trying to figure out how to limit the size of the requests response for product descriptions in the following way:

If the lenght of a description is higher than 2000, delete some of the letters at the end of it until it reaches the limit of 2000, but don't remove it completely from the request.

As of now, what I've been able to achieve is to completely remove the product infos if the lenght is higher than 2000 as you can see below.

My django views function:

def api_data(request):
    if request.GET.get('mybtn'):  # to improve, == 'something':
        resp_1 = requests.get(
            "https://www.test-headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=PARIS&limit=5000&currencyCode=CAD",
            headers={
                "Headout-Auth": HEADOUT_TEST_API_KEY
            })
        resp_1_data = resp_1.json()
        base_url_2 = "https://www.test-headout.com/api/public/v1/product/get/"

        for item in resp_1_data['items']:
            # concat ID to the URL string
            url = '{}{}'.format(base_url_2, item['id'] + '?language=fr')

            # make the HTTP request
            resp_2 = requests.get(
                url,
                headers={
                    "Headout-Auth": HEADOUT_TEST_API_KEY
                })
            resp_2_data = resp_2.json()

            if len(resp_2_data['contentListHtml'][0]['html']) < 2000: #represent the description of a product
                Product.objects.get_or_create(
                    title=item['name'],
                    destination=item['city']['name'],
                    description=resp_2_data['contentListHtml'][0]['html'],
                    link=item['canonicalUrl'],
                    image=item['image']['url']
                )

    return render(request, "form.html")

But I remove way to much rows by doing this so I would like to know how can I fix this?

Please help.

Aucun commentaire:

Enregistrer un commentaire