“A picture is worth a thousand words…”
Not always, and not so precisely, but we find that as our life is reflected in news, images are an indelible, integral part of news, making news richer, quicker to absorb and easier for us to relate to.
Refinitiv Data Platform (RDP) provides access to the broad range of content, including rich, self-described RDP news and RDP news images.
Next, we are going to look at requesting images that are a part of RDP news. Currently, RDP Top News and RDP News Reports Online are the two types of news that are made available with images.
To interact with RDP platform we require valid RDP credentials and setup:
These steps are included in the companion code examples hosted on GitHub (see References section) and are described in detail in many RDP articles, for example https://developers.refinitiv.com/en/article-catalog/article/exploring-news-metadata-refinitiv-data-platform-and-python, so that we can omit what would be a redundant detailed discussion of these steps here as well, and focus solely on requesting RDP images.
Let us first look at requesting images that are made available with Top News
def getTopNews():
news_category_URL = "/data/news"
topnews_endpoint_URL = "/top-news"
REQUEST_URL = base_URL + news_category_URL + RDP_version + topnews_endpoint_URL
accessToken = getToken();
print("Requesting: ",REQUEST_URL)
acceptValue = "application/json"
dResp = requests.get(REQUEST_URL, headers = {"Authorization": "Bearer " + accessToken, "Accept": acceptValue});
if dResp.status_code != 200:
print("Unable to get data. Code %s, Message: %s" % (dResp.status_code, dResp.text));
if dResp.status_code != 401: # error other then token expired
return("")
accessToken = getToken(); # token refresh on token expired
else:
print("Resource access successful")
return dResp.text
txt = getTopNews()
jResp = json.loads(txt);
print(json.dumps(jResp, indent=2));
Resulting in output:
From the hierarchy, we select the news package of interest to us, for example, Main -> World News:
myTopNewsId = jResp['data'][0].get('pages')[3].get('topNewsId')
def getTopNewsPerId(id):
news_category_URL = "/data/news"
topnews_endpoint_URL = "/top-news/"
REQUEST_URL = base_URL + news_category_URL + RDP_version + topnews_endpoint_URL + id
accessToken = getToken();
print("Requesting: ",REQUEST_URL)
acceptValue = "application/json"
dResp = requests.get(REQUEST_URL, headers = {"Authorization": "Bearer " + accessToken, "Accept": acceptValue});
if dResp.status_code != 200:
print("Unable to get data. Code %s, Message: %s" % (dResp.status_code, dResp.text));
if dResp.status_code != 401: # error other then token expired
return("")
accessToken = getToken(); # token refresh on token expired
else:
print("Resource access successful")
return dResp.text
txt = getTopNewsPerId(myTopNewsId)
jResp2 = json.loads(txt);
print(json.dumps(jResp2, indent=2));
Resulting in: Let us look closely at top news item’s “data”, and observe that it contains “image” that contains “id. So next we
In this example, we select first- [0]
myImageId = jResp2['data'][0].get('image').get('id')
myImageId
And now we are ready to
We request with selected Image ID, store into a file as well as display
def getTopNewsImage(imageId):
news_category_URL = "/data/news"
image_endpoint_URL = "/images/"
REQUEST_URL = base_URL + news_category_URL + RDP_version + image_endpoint_URL + imageId
accessToken = getToken();
print("Requesting: ",REQUEST_URL)
acceptValue = "image/jpeg"
dResp = requests.get(REQUEST_URL, headers = {"Authorization": "Bearer " + accessToken, "Accept": acceptValue});
if dResp.status_code != 200:
print("Unable to get data. Code %s, Message: %s" % (dResp.status_code, dResp.text));
if dResp.status_code != 401: # error other then token expired
return("")
accessToken = getToken(); # token refresh on token expired
else:
print("Resource access successful")
return dResp.content
imgContent = getTopNewsImage(myImageId)
file = open(myImageId+'.jpg', "wb")
file.write(imgContent)
file.close()
from IPython.display import Image
Image(filename=myImageId+'.jpg')
Next let us look how to request images that are part of online reports
def getOnlineReports():
news_category_URL = "/data/news"
reports_endpoint_URL = "/online-reports"
REQUEST_URL = base_URL + news_category_URL + RDP_version + reports_endpoint_URL
accessToken = getToken();
print("Requesting: ",REQUEST_URL)
acceptValue = "application/json"
dResp = requests.get(REQUEST_URL, headers = {"Authorization": "Bearer " + accessToken, "Accept": acceptValue});
if dResp.status_code != 200:
print("Unable to get data. Code %s, Message: %s" % (dResp.status_code, dResp.text));
if dResp.status_code != 401: # error other then token expired
return("")
accessToken = getToken(); # token refresh on token expired
else:
print("Resource access successful")
return dResp.text
txt = getOnlineReports()
jResp = json.loads(txt);
print(json.dumps(jResp, indent=2));
Observe the hierarchy:
report_id = jResp['data'][0].get('reports')[0].get('reportId')
report_id
Resulting in:
#report_id = "/OLUSTOPNEWS"
def getReportsById(report_ir):
news_category_URL = "/data/news"
reports_endpoint_URL = "/online-reports"
REQUEST_URL = base_URL + news_category_URL + RDP_version + reports_endpoint_URL + "/" + report_id
accessToken = getToken();
print("Requesting: ",REQUEST_URL)
acceptValue = "application/json"
dResp = requests.get(REQUEST_URL, headers = {"Authorization": "Bearer " + accessToken, "Accept": acceptValue});
if dResp.status_code != 200:
print("Unable to get data. Code %s, Message: %s" % (dResp.status_code, dResp.text));
if dResp.status_code != 401: # error other then token expired
return("")
accessToken = getToken(); # token refresh on token expired
else:
print("Resource access successful")
return dResp.text
txt = getReportsById(report_id)
jResp = json.loads(txt);
print(json.dumps(jResp, indent=2));
In this exmaple - first - [0]
myImageId = jResp['data'][0].get('newsItem').get('itemMeta').get('link')[0].get('remoteContent')[0].get('_residref')
myImageId
By passing image id, in this example - first [0]
myImageName = 'uniqueImageName'
def getImage(imageId):
news_category_URL = "/data/news"
image_endpoint_URL = "/images/"
REQUEST_URL = base_URL + news_category_URL + RDP_version + image_endpoint_URL + imageId
accessToken = getToken();
print("Requesting: ",REQUEST_URL)
acceptValue = "image/jpeg"
dResp = requests.get(REQUEST_URL, headers = {"Authorization": "Bearer " + accessToken, "Accept": acceptValue});
if dResp.status_code != 200:
print("Unable to get data. Code %s, Message: %s" % (dResp.status_code, dResp.text));
if dResp.status_code != 401: # error other then token expired
return("")
accessToken = getToken(); # token refresh on token expired
else:
print("Resource access successful")
return dResp.content
imgContent = getImage(myImageId)
file = open(myImageName+'.jpg', "wb")
file.write(imgContent)
file.close()
from IPython.display import Image
Image(filename=myImageName+'.jpg')
We store and display the image that was just requested and stored: