TRKD Overview

The Thomson Reuters Knowledge Direct (TRKD) API integrates into your website, trading platform, company intranet/extranet, advisory portal and mobile applications to provide up-to-date financial market data, news and analytics and powerful investment tools.

TRKD offers a wide range of Thomson Reuters' information and services delivered in a request-response scenario via web services using today's industry standard protocols (SOAP/XML and REST/JSON). Connectivity can be via HTTP and HTTPS, over the Internet or Delivery Direct. All data are snapshot (non-streaming) data.

With new HTTP JSON services, developers can integrate TRKD information to their application easier then before. This article provides an information regarding the basic knowledge of how to implement TRKD JSON application to consume news headlines data with Python language.

TRKD News Headline Service Overview

The TRKD News service allows you to query for news headlines and also retrieve news content via TRKD RetrieveHeadlineML_1 operation. This RetrieveHeadlineML_1 operation provides news headlines that match specific requirements for developers. For complex searches, RetrieveHeadlineML_1 operation supports sophisticated search filters that express complex Boolean criteria. These filters can incorporate keywords, companies, products, etc., as well as meta-data such as story time, news provider, and language – and can be used in a single query.

The service also includes symbol resolution and ambiguous symbol handling.

Note: The number of headlines returned by a response is limited to 1000 headlines in TRKD API. Headlines and corresponding stories can only be retrieved for the last 90 days.

TRKD JSON application implementation process

The JSON application requires the following steps to consume data from TRKD API services

  1. Authentication with TRKD Authentication service (TRKD Service Token) to get an authen token

  2. Send a JSON request message with the required input information and authen token to the interested TRKD service

TRKD News Headline Detail

TRKD RetrieveHeadlineML_1 URL and Header

The URL enponint for the TRKD Quote Service is following: https://api.trkd.thomsonreuters.com/api/News/News.svc/REST/News_1/RetrieveHeadlineML_1

Header:

  • Content-type = application/json;charset=utf-8
  • X-Trkd-Auth-ApplicationID = Application ID
  • X-Trkd-Auth-Token = Service Token

Method:

  • Post

TRKD RetrieveHeadlineML_1 Request Message

The TRKD News RetrieveHeadlineML_1 operation requires the Filters parameter as a mandatory input for query users interested news headlines.  An example of headline request message structure is following

{
  "RetrieveHeadlineML_Request_1": {
    "HeadlineMLRequest": {
      "TimeOut": <Defines maximum time in seconds to wait for a response (optional)>,
      "MaxCount": <Defines maximum number of items to be fetched (optional)>,
      "StartTime": "<Defines the start of a time window within which news stories must fall (optional)>",
      "EndTime": "<Defines the end of a time window within which news stories must fall (optional)>",
      "Direction": "Newer",
      "Filter": [ //Complex search criteria
        {
          "FreeTextConstraint": {
            "where": "headline",
            "Value": {
              "Text": "thomson reuters"
            }
          }
        }
      ]
    }
  }
}

Filters

Complex search criteria are specified by using XML elements called filters and constraints to represent the terms of a Boolean expression. Like the terms of a Boolean expression, these elements contain one or more statements to evaluate, can be combined using logical operators (AND, OR, ANDNOT), and can be nested within other terms. Searches can be constructed to perform free text searches (e.g., look for "Sun" anywhere in the headline), or look for specific meta-data (e.g., COMPANY="SUNW", LANGUAGE="EN", etc.), or both.

You can find more detail regarding the News Filters in TRKD Devlopment Guide document.

"trkd_newsheadline.py" Example Application

TRKD API News service provides a snapshot (non-streaming) news for the application. To retrieve news headlines data, your application must send the request message in JSON formation along with an authenticated credentials (Service Token) and application id in the request message header.

This section describes how to implement the trkd_newsheadline.py script that performs authentication with the TRDK Service Token, then request for 10 latest news headlines that relates to the user input RIC name via a command line (Text filter).

Implementation Details

  1. Firstly, we create a file named “trkd_newsheadline.py” in the working directory. Then we write the code to import all required libraries at the top of the source code
    import os
    import sys
    import requests
    import json
    import getpass

     

  2. Since this application needs to send multiple HTTP request messages to TRKD Services (for authentication and request news headlines data), we will separate the HTTP request source code to the dedicate function call instead. We start by creating the doSendRequest() function which receives three parameters: the requested URL, request message  and request message's header. This function sends the JSON request message to the requested URL and returns the HTTP response via the result parameter. If the HTTP response message status code is not 200 (200 OK), it shows the error detail.
    #Send HTTP request for all services
    def doSendRequest(url, requestMsg, headers):
    	result = None
    	try:
    		##send request
    		result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
    		##handle error
    		if result.status_code is not 200:
    			print('Request fail')
    			print('response status %s'%(result.status_code))
    			if result.status_code == 500: ## if username or password or appid is wrong
    				print('Error: %s'%(result.json()))
    				result.raise_for_status()
    	except requests.exceptions.RequestException as e:
    		print('Exception!!!')
    		print(e)
    		sys.exit(1)
    	return result

     

  3. Next, Then we add the code to receive the input user name, password and application id from a console

    ## ------------------------------------------ Main App ------------------------------------------ ##
    ##Get username, password and applicationid
    username = raw_input('Please input username: ')
    ##use getpass.getpass to hide user inputted password
    password = getpass.getpass(prompt='Please input password: ')
    appid = raw_input('Please input appid: ')

     

  4. Next, we create the CreateAuthorization() function to manage and handle TRKD authentication process. We pass user name, password and application id information to the function, then print out the Service Token from TRKD in a console.

    ## Perform authentication
    def CreateAuthorization(username, password, appid):
    	token = None
    	return token
    
    ## ------------------------------------------ Main App ------------------------------------------ ##
    ... #code froma  previouse step
    token = CreateAuthorization(username,password,appid)
    print('Token = %s'%(token))

     

  5. In the CreateAuthorization() function, we create the authentication (authenMsg), the url (authenURL) and headers variables

    ## Perform authentication
    def CreateAuthorization(username, password, appid):
    	token = None
    	##create authentication request URL, message and header
    	authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
    	authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
    	headers = {'content-type': 'application/json;charset=utf-8'}

     

  6. Next, we send this authentication request message, URL and header to the doSendRequest() function. Once the application receives the response Service Token, we send the incoming Service Token back to the caller to prints out the token.

    ##Perform authentication
    def CreateAuthorization(username, password, appid):
    	... #code froma  previouse step
    	print('############### Sending Authentication request message to TRKD ###############')
    	authenResult = doSendRequest(authenURL, authenMsg, headers)
    	if authenResult and authenResult.status_code == 200:
    		print('Authen success')
    		print('response status %s'%(authenResult.status_code))
    		##get Token
    		token = authenResult.json()['CreateServiceToken_Response_1']['Token']
    	return token

     

  7. After we received the Service Token and that Token is not null, we pass the Service Token and application id to the RetrieveNewsHeadline() function for requesting the news headlines data from TRKD RetrieveHeadlineML_1 operation.

    ## Perform Quote request 
    def RetrieveNewsHeadline(token, appid):
    	pass
    
    ##------------------------------------------ Main App ------------------------------------------##
    token = CreateAuthorization(username,password,appid)
    print('Token = %s'%(token))
    ## if authentiacation success, continue subscribing Quote
    if token:
    	RetrieveNewsHeadline(token,appid)

     

  8. The RetrieveNewsHeadline() function creates the news headlines request message and sends it to the TRKD RetrieveHeadlineML_1 operation via doSendRequest() function.

    ##Perform News Headline request  
    def RetrieveNewsHeadline(token, appid):
    	##construct news headline URL and header
    	newsURL = 'https://api.trkd.thomsonreuters.com/api/News/News.svc/REST/News_1/RetrieveHeadlineML_1'
    	headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}

     

  9. Next, we create the JSON news headlines request message to request latest 10 news headlines of interested RIC. We get user input for the interested RIC code from a console and set it to the request message.

    ## Perform news headlines request
      def RetrieveNewsHeadline(token, appid): 
        ##construct a news headlines request message
        ricName = raw_input('Please input Symbol: ')
        newsRequestMsg = {
          'RetrieveHeadlineML_Request_1': {
          'HeadlineMLRequest': {
            'MaxCount': 10,
            'Filter': [{
            'MetaDataConstraint': {
              'class': 'any',
              'Value': {
              'Text': ricName
              }
            }
            }]
          }
          }
        }
    

     

  10. Then we send the news headlines request message to TRKD News service. If the response HTTP status code is 200 (OK), it prints out result to a console.

    ##Perform News Headline request  
    def RetrieveNewsHeadline(token, appid):
    	##construct news headline URL and header
    	...
    	##construct a news headline request message
    	...
    	print('############### Sending News Headline request message to TRKD ###############')
    	newsResult = doSendRequest(newsURL, newsRequestMsg,headers)
    	if newsResult is not None and newsResult.status_code == 200:
    		print('News Headline response message: ')
    		print(newsResult.json())

     

Running the application

 The application source code is available at GitHub. You can get it via the following git command:

$>git clone git@github.com:TR-API-Samples/Example.TRKD.Python.HTTPJSON.git

All TRKD HTTP JSON python examples support Python 3 and they require the requests library.

Please check the README.md file in the source code project for more detail.

You can run trkd_newsheadline.py application via the following command:

$>python trkd_newsheadline.py

Then input your user name, password and application id. The result is shown in the Figure-1 below

Figure-1: The trkd_newsheadline receives username, password and appid via console

Then, input the interested Symbol for 10 latest news headlines.

Figure-2: The trkd_newsheadline asks for interested Symbol

Then the TRKD News application subscribes news headlines from TRKD News service and displays the latest 10 news headlines data in console.

Figure-3: The trkd_newsheadline diplsay news headline results

Conclusion

TRKD News service provides static (real-time and history) news headlines and news story data  content for users. It supports various search Filters (incorporate keywords, companies, products, time, etc) that let users query based on their interest. Users need to authenticate permission with TRKD Service Token first, then send the news headlines request message with Service Token and App ID in the request message header to TRKD News Server.

References

For further details, please check out the following resources:

For any question related to this article or Elektron WebSocket API page, please use the Developer Community Q&A Forum.