Skip to content

Commit 49b4e52

Browse files
author
Wasin Waeosri
committed
done news story
1 parent 43e1011 commit 49b4e52

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ This is an example project that shows how to implement TRKD REST Client with pyt
33
- trkd_authen.py: An example application that shows how to authenticate with TRKD service
44
- trkd_quote.py: An example application that shows how to subscribe (all fields and specific fields) the Quote data from TRKD service
55
- trkd_newsheadline.py: An example application that shows how to subscribe the News Headline data from TRKD service
6+
- trkd_newsstory.py: An example application that shows how to subscribe the News Story data from TRKD service
67

78

89
#prerequisite
@@ -37,4 +38,6 @@ The best way is to get the pip package management tool
3738
- trkd_quote.py
3839
- Version 1.0.1: 7 Sep 2016
3940
- trkd_newsheadline.py
40-
- changed code structure to separate call http request
41+
- changed code structure to separate call http request
42+
- Version 1.0.2: 19 Sep 2016
43+
- trkd_newsstory.py

trkd_newsstory.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)