import urllib.request
import json
import urllib.parse


url = 'https://lms.fu-berlin.de/learn/api/v1/courses?fields=id,name,description,courseId,startDate,endDate'

# now, with the below headers, we defined ourselves as a simpleton who is
# still using internet explorer.
headers = {}
headers['Cookie'] = "JSESSIONID=1C7D8F7562968A6693BDD5C5F049D35A; session_id=8286C021DFB6CCA0DB4089A1D31EB422; s_session_id=C2CB57E4AEEFBF6496A09F517CDBDE91; web_client_cache_guid=f6ec07e0-7c39-4d0e-b690-5abab8d6cc06; loginType=shibboleth; JSESSIONID=3929500B4C87181844342524E78BDC89; _shibsession_64656661756c7468747470733a2f2f6c6d732e66752d6265726c696e2e64652f73686962626f6c657468=_a144d509c3e7defaec6a5a7d23d5f00c"


with open('your_file.txt', 'w') as f:
    f.write("description°courseId°name°id°startDate°endDate")
    while url is not None:
        req = urllib.request.Request(url, headers = headers)
        resp = urllib.request.urlopen(req)
        respData = resp.read().decode('utf-8')
        j = json.loads(respData)
        if len(j['results']) == 0:
            url = None
        else:
            url = 'https://lms.fu-berlin.de' + urllib.parse.unquote_plus(j['paging']['nextPage'])
            for result in j['results']:
                data = result.get('description', "") + "°" + result.get('courseId', "") + "°" + result.get('name', "") + "°" + result.get('id', "") + "°" + result.get('startDate', "") + "°" + result.get('endDate', "") + "\n"
                f.write(data)
            print(url)
