import requests import json import os try: myToken = "=====YOUR ACCESS TOKEN======="; sess = requests.Session() def requestData(): url = "https://selectapi.datascope.refinitiv.com/RestApi/v1/Extractions/ExtractRaw" hdrs = { "Authorization": "Token " + myToken, "Prefer": "respond-async, wait=10", "Content-Type": "application/json; odata=minimalmetadata" }; body = { "ExtractionRequest": { "@odata.type": "#DataScope.Select.Api.Extractions.ExtractionRequests.CompositeExtractionRequest", "ContentFieldNames": [ "ISIN", "Average Volume - 30 Days", "Average Volume - 90 Days", "Close on Close Volatility - 90 Days", "Dollar Daily Value Average - 30 Days", "CESR Free Float", "CRA Free Float", "Outstanding Shares - Issue Shares Amount", "Outstanding Shares - Other Shares Amount" ], "IdentifierList": { "@odata.type": "#DataScope.Select.Api.Extractions.ExtractionRequests.InstrumentIdentifierList", "InstrumentIdentifiers": [{ "Identifier": "FR0000120321", "IdentifierType": "Isin" }, { "Identifier": "FR0000121014", "IdentifierType": "Isin" }, { "Identifier": "FR0014000MR3", "IdentifierType": "Isin" }, { "Identifier": "GB0005630420", "IdentifierType": "Isin" }, { "Identifier": "GB00BH4HKS39", "IdentifierType": "Isin" }, { "Identifier": "IE00B1FZS467", "IdentifierType": "Isin" }, { "Identifier": "IE00B1FZS467", "IdentifierType": "Isin" }, { "Identifier": "IE00B1XNHC34", "IdentifierType": "Isin" }, { "Identifier": "IE00B3VWMM18", "IdentifierType": "Isin" }, { "Identifier": "IE00B4BNMY34", "IdentifierType": "Isin" }, { "Identifier": "IE00B6R52036", "IdentifierType": "Isin" }, { "Identifier": "IE00B6R52036", "IdentifierType": "Isin" }, { "Identifier": "IE00BD2B9488", "IdentifierType": "Isin" }, { "Identifier": "IE00BH480S68", "IdentifierType": "Isin" }, { "Identifier": "IE00BKM4GZ66", "IdentifierType": "Isin" }, { "Identifier": "IE00BKTLJC87", "IdentifierType": "Isin" }, { "Identifier": "IE00BMYDM794", "IdentifierType": "Isin" }, { "Identifier": "IE00BQ1YBK98", "IdentifierType": "Isin" }, { "Identifier": "IE00BQQP9H09", "IdentifierType": "Isin" }, { "Identifier": "IE00BZ12WP82", "IdentifierType": "Isin" }, { "Identifier": "LU0133360163", "IdentifierType": "Isin" }, { "Identifier": "LU0294851513", "IdentifierType": "Isin" }, { "Identifier": "LU0514695690", "IdentifierType": "Isin" }, { "Identifier": "LU0971623524", "IdentifierType": "Isin" }, { "Identifier": "LU1127970256", "IdentifierType": "Isin" }, { "Identifier": "LU1376267727", "IdentifierType": "Isin" }, { "Identifier": "LU1659686460", "IdentifierType": "Isin" }, { "Identifier": "NL0000009538", "IdentifierType": "Isin" }, { "Identifier": "NL0000395903", "IdentifierType": "Isin" }, { "Identifier": "NL0010273215", "IdentifierType": "Isin" }, { "Identifier": "SE0015961909", "IdentifierType": "Isin" }, { "Identifier": "SE0015988019", "IdentifierType": "Isin" }, { "Identifier": "US00724F1012", "IdentifierType": "Isin" }, { "Identifier": "US01609W1027", "IdentifierType": "Isin" }, { "Identifier": "US02079K3059", "IdentifierType": "Isin" }, { "Identifier": "US0231351067", "IdentifierType": "Isin" }, { "Identifier": "US0378331005", "IdentifierType": "Isin" }, { "Identifier": "US0383361039", "IdentifierType": "Isin" }, { "Identifier": "US0758871091", "IdentifierType": "Isin" }, { "Identifier": "US09075V1026", "IdentifierType": "Isin" }, { "Identifier": "US1713401024", "IdentifierType": "Isin" }, { "Identifier": "US2358511028", "IdentifierType": "Isin" }, { "Identifier": "US2546871060", "IdentifierType": "Isin" }, { "Identifier": "US2788651006", "IdentifierType": "Isin" }, { "Identifier": "US28176E1082", "IdentifierType": "Isin" }, { "Identifier": "US29414B1044", "IdentifierType": "Isin" }, { "Identifier": "US4612021034", "IdentifierType": "Isin" }, { "Identifier": "US5797802064", "IdentifierType": "Isin" }, { "Identifier": "US5949181045", "IdentifierType": "Isin" }, { "Identifier": "US7170811035", "IdentifierType": "Isin" }, { "Identifier": "US7427181091", "IdentifierType": "Isin" } ] }, "Condition": { "ScalableCurrency": True } } } return sess.post(url, data = json.dumps(body), headers = hdrs); def checkStatus(url): hdrs = { "Authorization": "Token " + myToken, "Prefer": "respond-async, wait=10" }; return sess.get(url, headers = hdrs); def retrieveResults(jobID): url = "https://selectapi.datascope.refinitiv.com/RestApi/v1/Extractions/RawExtractionResults('" + jobID + "')/$value" hdrs = { "Authorization": "Token " + myToken, "Prefer": "respond-async" }; return sess.get(url, headers = hdrs); # STEP1: Resuest the data print("Requesting tick history data..."); resp = requestData(); # is it a 202 (in progress) while (resp.status_code == 202): print("waiting..."); # check if the response completed yet URL = resp.headers['Location']; resp = checkStatus(URL); print("Data available on server, fetching..."); print(resp.status_code) print(resp.text) # request completed with status 200 OK; get data if (resp.status_code == 200): jobid = resp.json()['JobId']; resp = retrieveResults(jobid); print("Data:"); print(resp.text); except KeyError: print("No Authentication token found, run login script first");