url = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=samvkampen&api_key=974a5ebc077564f72bd639d122479d4b&limit=200&format=json&page="
import time
import requests
import json
import pickle

store = []

data = requests.get(url + '1').json()
totalPages = int(data['recenttracks']['@attr']['totalPages'])

for n in range(1, totalPages + 1):
    page_url = url + str(n)
    retry = True
    while retry:
        resp = requests.get(page_url)
        if resp.status_code != 200:
            print(f"Page status code {resp.status_code}. Retrying after 200ms.")
            time.sleep(0.2)
            continue
        data = resp.json()
        store.append(data)
        break
    print(f"Got data for page {n}")

with open('data.json', 'w') as f:
    json.dump(store, f)
