Step 2: send an on demand extraction request using the received token
if not token:
print("Authentication token is missing. Cannot proceed with extraction.")
else:
requestUrl='https://selectapi.datascope.refinitiv.com/RestApi/v1/Extractions/ExtractRaw'
requestHeaders={
"Prefer":"respond-async",
"Content-Type":"application/json",
"Authorization": "token " + token
}
# *** USING VALID TEST RIC: MCGBc1 (FUTURES) ***
# REPLACE THIS SECTION WITH YOUR 42 OPTIONS RICS WHEN YOU HAVE THE VALID FORMAT
instrument_identifiers = [
{ "Identifier": "MCGB6150U5", "IdentifierType": "Ric" }
]
requestBody={
"ExtractionRequest": {
"@odata .type": "#DataScope.Select.Api.Extractions.ExtractionRequests.TickHistoryIntradaySummariesExtractionRequest",
# --- SIMPLIFIED FIELDS: Open, High, Low, Last (Close) ---
"ContentFieldNames": [
"Open",
"High",
"Low",
"Last"
],
"IdentifierList": {
"@odata .type": "#DataScope.Select.Api.Extractions.ExtractionRequests.InstrumentIdentifierList",
"InstrumentIdentifiers": instrument_identifiers,
"UseUserPreferencesForValidationOptions":"false"
},
"Condition": {
"MessageTimeStampIn": "GmtUtc",
"ReportDateRangeType": "Range",
"QueryStartDate": "2025-09-01T00:00:00.000Z",
"QueryEndDate": "2025-09-30T23:59:59.000Z",
"SummaryInterval": "OneMinute",
"TimebarPersistence": "true",
"DisplaySourceRIC": "true"
}
}
}
try:
r2 = requests.post(requestUrl, json=requestBody, headers=requestHeaders, verify=False)
global status_code
status_code = r2.status_code
print (f"HTTP status of the initial response: {status_code}")
if status_code == 200 or status_code == 202:
# --- GLOBAL DECLARATIONS MOVED TO TOP OF BLOCK ---
global jobId
global notes # Not strictly required if only assigned, but good practice
# Robust JSON handling for 202 responses
if r2.text.strip():
try:
r2Json = json.loads(r2.text.encode('ascii', 'ignore'))
jobId = r2Json.get("JobId")
notes = r2Json.get("Notes", ["No extraction notes available."])
print (f'\nJobId: {jobId}')
print (f'Extraction notes:\n{notes[0]}')
except json.JSONDecodeError:
print("\nWARNING: 202 Response body was not valid JSON or was empty.")
if 'location' in r2.headers:
location_url = r2.headers["location"]
# Extract JobId from Location header URL
jobId = location_url.split("'")[-2]
print(f"Successfully retrieved JobId from Location header: {jobId}")
else:
print("CRITICAL ERROR: Could not find JobId in response body or Location header.")
else:
print("\nResponse body was empty. Attempting to retrieve JobId from Location header.")
if 'location' in r2.headers:
location_url = r2.headers["location"]
jobId = location_url.split("'")[-2]
print(f"Successfully retrieved JobId from Location header: {jobId}")
else:
print("CRITICAL ERROR: Could not find JobId in Location header.")
else:
print (f'Extraction submission failed. Status: {status_code}')
print ('Response content:', r2.text)
except requests.exceptions.RequestException as e:
print(f"An error occurred during extraction submission: {e}")