Skip to content

Commit 35f3208

Browse files
author
Wasin Waeosri
committed
online report done
1 parent 32cb1bf commit 35f3208

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

trkd_onlinereport.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Headline request
51+
def RetrieveOnlineReport(token, appid):
52+
##construct news headline URL and header
53+
onlinereportURL = 'http://api.rkd.reuters.com/api/OnlineReports/OnlineReports.svc/REST/OnlineReports_1/GetSummaryByTopic_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 request message
56+
onelinereportRequestMsg = {'GetSummaryByTopic_Request_1': {
57+
'Topic': 'OLRUTOPNEWS',
58+
'MaxCount': 20,
59+
'ReturnPrivateNetworkURL': False
60+
}
61+
}
62+
print '############### Sending News - Online Report request message to TRKD ###############'
63+
onlinereportResult = doSendRequest(onlinereportURL, onelinereportRequestMsg,headers)
64+
if onlinereportResult is not None and onlinereportResult.status_code == 200:
65+
print 'News Headline response message: '
66+
print onlinereportResult.json()
67+
68+
69+
70+
## ------------------------------------------ Main App ------------------------------------------ ##
71+
##Get username, password and applicationid
72+
username = raw_input('Please input username: ')
73+
##use getpass.getpass to hide user inputted password
74+
password = getpass.getpass(prompt='Please input password: ')
75+
appid = raw_input('Please input appid: ')
76+
77+
token = CreateAuthorization(username,password,appid)
78+
print 'Token = %s'%(token)
79+
80+
## if authentiacation success, continue subscribing Quote
81+
if token is not None:
82+
RetrieveOnlineReport(token,appid)
83+

0 commit comments

Comments
 (0)