Search code examples
pythonexcellistapirequest

project ID printed for every task on the API problem


I have this code below, requesting an api. Every ID in listID loops 4 times in the url. So if the listID = [1,2,3,4,5], the url will be :
url = "https://xxxxapi/v1/implantacao/projeto/1/tarefa?start=0&limit=50" then url = "https://xxxxapi/v1/implantacao/projeto/1/tarefa?start=1&limit=50" and etc, start goes 0 to 3 for every id in the list

Then, im saving every data request i get into and xls file and that's working fine. For example, every 4 loops at the id, normally returns 120 tasks. I want to print the ID for every task that the code returns, in the line : #sheet.cell(row=i+1, column=1).value = listID

 def teste(id): 
      listID = (id)
      headers = {
            "xxxxxxxxx",
            "Content-Type":"application/json;charset=UTF-8"
        } 
      
      length = len(listID)
      nome = []
      codigoTarefa = []
      situacaoTarefa = []
      faseNome = []
      
      for li in range(length):
        for count in range(4):
          url = "https://xxxxapi/v1/implantacao/projeto/{}/tarefa?start={}&limit=50".format(listID[li], count)
          response = requests.get(url, headers=headers)
          data = response.json()
    
          
          unidades2(data, nome, codigoTarefa, situacaoTarefa, faseNome)
      
      
      wb = openpyxl.Workbook()
      sheet = wb.active
    
      for i in range(len(nome)):
        #sheet.cell(row=i+1, column=1).value = listID
        sheet.cell(row=i+1, column=2).value = nome[i]
        sheet.cell(row=i+1, column=3).value = codigoTarefa[i]
        sheet.cell(row=i+1, column=4).value = situacaoTarefa[i]
        
      wb.save("dados11.xlsx")
      
          
    
    def unidades2(data, nome, codigoTarefa, situacaoTarefa, faseNome):
      workbook = xlwt.Workbook()
      sheet = workbook.add_sheet("BACKOFFICE")
      coluna = 1
      for i in data['data']:
        nome.append(i['nome'])
        codigoTarefa.append(i['codigo'])
        situacaoTarefa.append(i['situacao'])
        coluna +=1
        
       
      
      
     
      
    if __name__ == '__main__':
      Sults() 

To be more clear : One output example from one task at project ID 1 :

INAUGURAÇÃO T98 4

I want this output (1 is the first item in listID for example) : 1 INAUGURAÇÃO T98 4

How can i get it ? Thanks for the help btw


Solution

  • def teste(id): 
      listID = (id)# this is list of ids. Yes, that list you want this id go to the file xml. Yes, but the file today is a big one
      #print (listID)
      headers = {
            "Authorization":"xxxxxxxxxx",
            "Content-Type":"application/json;charset=UTF-8"
        } 
      
      
      #A contagem do counter começa negativa pois eu quero que a primeira busca de página seja no valor 0 > que busca as primeiras 50 tarefas
      
      length = len(listID)
      nome = []
      codigoTarefa = []
      situacaoTarefa = []
      faseNome = []
      global ID_List
      ID_List = [] #but how did u get this list became an [254,254,254,254,255,255,255 etc]
    
      for li in range(length):
        for count in range(4):
          #o start dentro de url é o counter. Ou seja, ele vai até 4, a partir do 0
          url = "https://xxxxxxxxx.com/api/v1/implantacao/projeto/{}/tarefa?start={}&limit=50".format(listID[li], count) #here i got the id from the code above, and count is the pagination about api
          #print(url)
          response = requests.get(url, headers=headers)
          data = response.json()
    
          
          unidades2(data, nome, codigoTarefa, situacaoTarefa, faseNome, listID[li])#li is legnth of  id list ok? Ok, i get it now. So )
      #print(nome)
      
      wb = openpyxl.Workbook()
      sheet = wb.active
    
      for i in range(len(nome)):
        sheet.cell(row=i+1, column=1).value = ID_List[i]
        sheet.cell(row=i+1, column=2).value = nome[i]
        sheet.cell(row=i+1, column=3).value = codigoTarefa[i]
        sheet.cell(row=i+1, column=4).value = situacaoTarefa[i]
        
      wb.save("dados12.xlsx")
      #print(codigoTarefa)
      #print(situacaoTarefa)
      #print(faseNome)
          
    
    def unidades2(data, nome, codigoTarefa, situacaoTarefa, faseNome, ID):   # ok just wait secounds this ID here is the parameter to the above listID[li]
      workbook = xlwt.Workbook()
      sheet = workbook.add_sheet("BACKOFFICE")
      coluna = 1
      for i in data['data']:
        nome.append(i['nome'])
        codigoTarefa.append(i['codigo'])
        situacaoTarefa.append(i['situacao'])
        
        ID_List.append(ID)# append here
        coluna +=1
       
      
      
     
      
    if __name__ == '__main__':
      Sults()