|
| 1 | +''' |
| 2 | +The TRKD API sample code is provided for informational purposes only |
| 3 | +and without knowledge or assumptions of the end users development environment. |
| 4 | +We offer this code to provide developers practical and useful guidance while developing their own code. |
| 5 | +However, we do not offer support and troubleshooting of issues that are related to the use of this code |
| 6 | +in a particular environment; it is offered solely as sample code for guidance. |
| 7 | +Please see the Thomson Reuters Knowledge Direct product page at http://customers.thomsonreuters.com |
| 8 | +for additional information regarding the TRKD API.''' |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import requests |
| 13 | +import json |
| 14 | +import getpass |
| 15 | + |
| 16 | +## Perform authentication |
| 17 | +def CreateAuthorization(username, password, appid): |
| 18 | + token = None |
| 19 | + ##create authentication request URL, message and header |
| 20 | + authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }} |
| 21 | + authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1' |
| 22 | + headers = {'content-type': 'application/json;charset=utf-8'} |
| 23 | + print '############### Sending Authentication request message to TRKD ###############' |
| 24 | + try: |
| 25 | + ##send request |
| 26 | + result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers) |
| 27 | + if result.status_code == 200: |
| 28 | + print 'Authen success' |
| 29 | + print 'response status %s'%(result.status_code) |
| 30 | + ##get Token |
| 31 | + token = result.json()['CreateServiceToken_Response_1']['Token'] |
| 32 | + elif result.status_code == 500: |
| 33 | + print 'Request fail' |
| 34 | + print 'response status %s'%(result.status_code) |
| 35 | + print 'Error: %s'%(result.json()) |
| 36 | + except requests.exceptions.RequestException as e: |
| 37 | + print 'Exception!!!' |
| 38 | + print e |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + return token |
| 42 | + |
| 43 | +## Perform News Headline request |
| 44 | +def RetrieveNewsHeadline(token, appid): |
| 45 | + ##construct news headline URL and header |
| 46 | + newsURL = 'https://api.trkd.thomsonreuters.com/api/News/News.svc/REST/News_1/RetrieveHeadlineML_1' |
| 47 | + headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token} |
| 48 | + ##construct a news headline request message |
| 49 | + ricName = raw_input('Please input Symbol: ') |
| 50 | + newsRequestMsg = {'RetrieveHeadlineML_Request_1': { |
| 51 | + 'HeadlineMLRequest':{ |
| 52 | + 'MaxCount':25, |
| 53 | + 'Filter':[ |
| 54 | + { |
| 55 | + 'MetaDataConstraint':{ |
| 56 | + 'class': 'any', |
| 57 | + 'Value': { |
| 58 | + 'Text' : ricName |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | + }} |
| 65 | + |
| 66 | + print '############### Sending News Headline request message to TRKD ###############' |
| 67 | + try: |
| 68 | + ##send request |
| 69 | + newsResult = requests.post(newsURL, data = json.dumps(newsRequestMsg), headers=headers) |
| 70 | + if newsResult.status_code == 200: ## success |
| 71 | + print 'News Headline request success' |
| 72 | + print newsResult.json() |
| 73 | + else: ## fail |
| 74 | + print 'Request fail' |
| 75 | + print 'response status %s'%(newsResult.status_code) |
| 76 | + print 'Error: %s'%(newsResult.json()) |
| 77 | + |
| 78 | + except requests.exceptions.RequestException as e: |
| 79 | + print 'Exception!!!' |
| 80 | + print e |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +## ------------------------------------------ Main App ------------------------------------------ ## |
| 86 | +##Get username, password and applicationid |
| 87 | +username = raw_input('Please input username: ') |
| 88 | +##use getpass.getpass to hide user inputted password |
| 89 | +password = getpass.getpass(prompt='Please input password: ') |
| 90 | +appid = raw_input('Please input appid: ') |
| 91 | + |
| 92 | +token = CreateAuthorization(username,password,appid) |
| 93 | +print 'Token = %s'%(token) |
| 94 | + |
| 95 | +## if authentiacation success, continue subscribing Quote |
| 96 | +if not token == None: |
| 97 | + RetrieveNewsHeadline(token,appid) |
| 98 | + |
0 commit comments