requestUrl = "https://hosted.datascopeapi.reuters.com/RestApi/v1/Authentication/RequestToken" requestHeaders={ "Prefer":"respond-async", "Content-Type":"application/json" } requestBody={ "Credentials": { "Username": xxx "Password": xxx } } r1 = requests.post(requestUrl, json=requestBody,headers=requestHeaders) if r1.status_code == 200 : jsonResponse = json.loads(r1.text.encode('ascii', 'ignore')) token = jsonResponse["value"] print ('Authentication token (valid 24 hours):') print (token) else: print ('Replace myUserName and myPassword with valid credentials, then repeat the request') #Step 2: send an on demand extraction request using the received token requestUrl='https://hosted.datascopeapi.reuters.com/RestApi/v1/Extractions/ExtractRaw' requestHeaders={ "Prefer":"respond-async", "Content-Type":"application/json", "Authorization": "token " + token } requestBody={ "ExtractionRequest": { "@odata.type": "#ThomsonReuters.Dss.Api.Extractions.ExtractionRequests.TickHistoryRawExtractionRequest", "IdentifierList": { "@odata.type": "#ThomsonReuters.Dss.Api.Extractions.ExtractionRequests.InstrumentIdentifierList", "InstrumentIdentifiers": generateChainRICs(sd,nc,bt,dTable), "ValidationOptions": { "AllowHistoricalInstruments": "true" } }, "Condition": { "MessageTimeStampIn": "LocalExchangeTime", #To be linked to Instrument's time zone - a mapping table needed #"ApplyCorrectionsAndCancellations": "false", "ReportDateRangeType": "Range", "QueryStartDate": sd+openString, #"T18:00:00.000Z", #To be user defined - linked to variables "QueryEndDate": ed+closeString, #"T17:00:00.000Z", #To be user defined - linked to variables "ExtractBy": "Ric", "SortBy": "SingleByRic", "DomainCode": "MarketPrice", "Fids" : fi, # "70", "DateRangeTimeZone" : "UTC", "DisplaySourceRIC": "true" } } } r2 = requests.post(requestUrl, json=requestBody,headers=requestHeaders) #Step 3: poll the status of the request using the received location URL. #Once the request has completed, retrieve the jobId and extraction notes. status_code = r2.status_code print ("HTTP status of the response: " + str(status_code)) if status_code == 202 : requestUrl = r2.headers["location"] requestHeaders={ "Prefer":"respond-async", "Content-Type":"application/json", "Authorization":"token " + token } while (status_code == 202): print ('As we received a 202, we wait 900 seconds, then poll again (until we receive a 200)') time.sleep(900) r3 = requests.get(requestUrl,headers=requestHeaders) status_code = r3.status_code print ("HTTP status of the response: " + str(status_code)) if status_code == 200 : r3Json = json.loads(r3.text.encode('ascii', 'ignore')) jobId = r3Json["JobId"] if status_code != 200 : print ('An error occurred. Try to run this cell again. If it fails, re-run the previous cell.\n') requestUrl = "https://hosted.datascopeapi.reuters.com/RestApi/v1/Extractions/RawExtractionResults" + "('" + jobId + "')" + "/$value" print "Job ID" print jobId #AWS requires an additional header: X-Direct-Download if useAws: requestHeaders={ "Prefer":"respond-async", "Content-Type":"text/plain", "Accept-Encoding":"gzip", "X-Direct-Download":"true", "Authorization": "token " + token, #'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36', #"X-Client-Session-Id":"Direct AWS" } else: requestHeaders={ "Prefer":"respond-async", "Content-Type":"text/plain", "Accept-Encoding":"gzip", "Authorization": "token " + token, #'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36', #"X-Client-Session-Id":"From DSS" } #import pdb;pdb.set_trace() r5 = requests.get(requestUrl,headers=requestHeaders,stream=True) #time.sleep(3) #Ensure we do not automatically decompress the data on the fly: r5.raw.decode_content = False if useAws: print ('Content response headers (AWS server): type: ' + r5.headers["Content-Type"] + '\n') #AWS does not set header Content-Encoding="gzip". else: print ('Content response headers (TRTH server): type: ' + r5.headers["Content-Type"] + ' - encoding: ' + r5.headers["Content-Encoding"] + '\n') #Next 2 lines display some of the compressed data, but if you uncomment them save to file fails fileName = filePath0 + dateforfilename+".csv.gz" chunk_size = 1024 rr = r5.raw print (rr) with open(fileName, 'wb') as fd: shutil.copyfileobj(rr, fd, chunk_size) fd.close