import requests
import json

url = "https://api.fairsharing.org/users/sign_in"

payload="{\"user\": {\"login\":\"andrea.mannocci@isti.cnr.it\",\"password\":\"sovietsoyouz\"} }"
headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

# print(response.text)
token = response.json()['jwt']
# print(jwt)

# Get the JWT from the response.text to use in the next part.

headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
  'Authorization': 'Bearer ' + token,
}
print(headers)

with open('./fairsharing.json', 'w') as file_out:
    for page in range(1, 5):
        url = 'https://api.fairsharing.org/databases/?page[number]=%s&page[size]=500' % page
        print(url)

        response = requests.request("GET", url, headers=headers)
        file_out.writelines('\n'.join([json.dumps(record) for record in response.json()['data']]))
        file_out.write('\n')