|
| 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 | +def doSendRequest(url, requestMsg, headers): |
| 17 | + result = None |
| 18 | + try: |
| 19 | + ##send request |
| 20 | + result = requests.post(url, data=json.dumps(requestMsg), headers=headers) |
| 21 | + if result.status_code == 500: |
| 22 | + print 'Request fail' |
| 23 | + print 'response status %s' % result.status_code |
| 24 | + print 'Error: %s' % result.json() |
| 25 | + sys.exit(1) |
| 26 | + except requests.exceptions.RequestException, e: |
| 27 | + print 'Exception!!!' |
| 28 | + print e |
| 29 | + sys.exit(1) |
| 30 | + return result |
| 31 | + |
| 32 | + |
| 33 | +## Perform authentication |
| 34 | +def CreateAuthorization(username, password, appid): |
| 35 | + token = None |
| 36 | + ##create authentication request URL, message and header |
| 37 | + authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }} |
| 38 | + authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1' |
| 39 | + headers = {'content-type': 'application/json;charset=utf-8'} |
| 40 | + print '############### Sending Authentication request message to TRKD ###############' |
| 41 | + authenResult = doSendRequest(authenURL, authenMsg, headers) |
| 42 | + if authenResult is not None and authenResult.status_code == 200: |
| 43 | + print 'Authen success' |
| 44 | + print 'response status %s'%(authenResult.status_code) |
| 45 | + ##get Token |
| 46 | + token = authenResult.json()['CreateServiceToken_Response_1']['Token'] |
| 47 | + |
| 48 | + return token |
| 49 | + |
| 50 | +## Perform News Story request |
| 51 | +def RetrieveNewsStory(token, appid): |
| 52 | + ##construct news headline URL and header |
| 53 | + newsURL = 'http://api.rkd.reuters.com/api/News/News.svc/REST/News_1/RetrieveStoryML_1' |
| 54 | + headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token} |
| 55 | + ##construct a news headline story message |
| 56 | + storyid = raw_input('Please input news story id: ') |
| 57 | + newsRequestMsg = {'RetrieveStoryML_Request_1': { |
| 58 | + 'StoryMLRequest':{ |
| 59 | + 'StoryId':[storyid] |
| 60 | + } |
| 61 | + }} |
| 62 | + |
| 63 | + print '############### Sending News Story request message to TRKD ###############' |
| 64 | + newsResult = doSendRequest(newsURL, newsRequestMsg,headers) |
| 65 | + if newsResult is not None and newsResult.status_code == 200: |
| 66 | + print 'News Story response message: ' |
| 67 | + print newsResult.json() |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +## ------------------------------------------ Main App ------------------------------------------ ## |
| 72 | +##Get username, password and applicationid |
| 73 | +username = raw_input('Please input username: ') |
| 74 | +##use getpass.getpass to hide user inputted password |
| 75 | +password = getpass.getpass(prompt='Please input password: ') |
| 76 | +appid = raw_input('Please input appid: ') |
| 77 | + |
| 78 | +token = CreateAuthorization(username,password,appid) |
| 79 | +print 'Token = %s'%(token) |
| 80 | + |
| 81 | +## if authentiacation success, continue subscribing Quote |
| 82 | +if token is not None: |
| 83 | + RetrieveNewsStory(token,appid) |
| 84 | + |
0 commit comments