1

I am writing a script to automate the extraction of a report via API, I came across a problem where the API limits the query to one page (x-page) at a time. How could I change this page to this last amount? And how to insert the new information after the page change in the file I'm generating?

Could you please help me?

url="https://api.example.com.br/company/7/history?createdFrom:2019:11:21T07:00:000updatedTo:2019:11:21T08:00:000Z"
heards = {
    "x-page": "1",
    "Content-Type": "application/json",
    "Authorization": bearer_token
}


r = requests.get(url, headers=heards)

file = open('data-history.json', 'wb')

for x in r:
    file.write(x)
file.close()

data = tablib.Dataset(headers=(
    'id', 'name', 'messages_count', 'last_message_at', 'status_id', 'created_at', 'updated_at', 'company_id',
    'contact_id', 'sector_id', 'channelable_id', 'channelable_type', 'is_json', 'last_user_id', 'closed_at',
    'first_message_at', 'protocol', 'first_start_chat_at', 'first_operator_answer_at', 'latest_contact_chat_message_at',
    'latest_operator_answer_at', 'waiting_time_medium', 'waiting_time_count', 'last_message_by_operator', 'closed_by_user_id',
    'operator_assigned_id', 'sla_count', 'is_auto')
)

import_filename = 'data-history.json'
data.json = open(import_filename, 'r').read()

data_export = data.export('xlsx')
with open('/home/mgsoares/Documentos/API/file.xlsx', 'wb') as f:  
    f.write(data_export)  
f.close()
  • some API gives in every JSON information how many pages it has and then you can use it to get last page - but it depends on API. So you have to read documentation. – furas Nov 24 '19 at 05:45
  • To add it to file you can use append mode `a` instead of `w`. OR open file only once at the beginning and close after you write all data. But some types of file need to read all data to memory, change/add data, and write all data back to file. – furas Nov 24 '19 at 05:47
  • Thanks for the response furas, in case the API does not have this information so would have to go page by page to extract the information, I wanted a way to use some loop loop but I don't know how to do it. – Manassés G. Nov 24 '19 at 14:01
  • I don't know how works this API - if it use different value in `x-page` to load different page, or it need other argument in header, or it need it in as parameter in url like `?page=2`. You would have to search in documentation. Or observer urls and request's data in DevTool in Chrome/Firefox - if you can use it in web browser. – furas Nov 24 '19 at 14:27

0 Answers0