Result of paged loop instead of unified

Asked

Viewed 60 times

0

I’m having trouble getting a result formatted in JSON paged. My code instead of returning the result of the joined is bringing the result per page!

import requests

def __init__(self, usuario, token):
    self.usuario = usuario
    self.token = token

def get_json(self, url):
    retorno = requests.get(url, auth=(self.usuario, self.token))
    if retorno.status_code != 200:
        print('Status:', retorno.status_code, 'Problem with the request. Exiting.')
        exit()

    return retorno.json()

def get_list_url(self, url):
    lista_url = [url]
    json_retorno = self.get_json(url)

    while json_retorno['next_page'] is not None:
        lista_url.append(json_retorno['next_page'])
        json_retorno = self.get_json(json_retorno['next_page'])
    return lista_url

def imprime_lista(self, url):
    json_retorno = self.get_json(url)
    resolvidos_30d = 0
    limite = 30
    data_atual = datetime.strptime('2018-06-06', '%Y-%m-%d')

    for res in json_retorno['tickets']:
        if res['dates']['solved_at'] is not None:
            solved_at = res['dates']['solved_at'][:10]
            data_resolvido = datetime.strptime(solved_at, '%Y-%m-%d')
            quantidade_dias = abs((data_atual - data_resolvido).days)
            if quantidade_dias <= limite:
                resolvidos_30d = resolvidos_30d + 1
            elif quantidade_dias > limite:
                break

        print(id, resolvidos_30d, sep=';')

tickets = ['2720018']
urlTrigger = 'exemplo.com.br'
api = login('[email protected] /token', 'xxx')

for id in tickets:
    role_znd = id
    lista = api.get_list_url(urlTrigger+str(role_znd+'/includes'))

    for url in lista:
        api.imprime_lista(url)

He returns to me more or less like this...

2720018;51
2720018;1
2720018;0
2720018;0

Process finished with exit code -1

Another basic detail is that the break is not respected, I am doing wrong?

  • You don’t call the method get_list_url nowhere? If it is she who returns the list of all Urls, how did she return all the pages together?

  • Thanks for the help Anderson, I call him down there, I had taken this chunk of code here in the stack (already added again). I have no clue why it returns on page 1 51 after page 2 1 result and so on.

  • Note: I accept tips to make the code cleaner or more efficient... I’m learning Python yet and any hint is valuable to the evolution of my learning :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.