Skip to content

Commit a1ffa1b

Browse files
author
Wasin Waeosri
committed
Merge branch 'v14_python3'
1. Update all Python codes to fully support Python3 (only).
2 parents 55f7071 + efaed70 commit a1ffa1b

File tree

10 files changed

+89
-135
lines changed

10 files changed

+89
-135
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ This is an example project that shows how to implement TRKD HTTP JSON Client wit
1818

1919
## Prerequisite
2020
The following softwares are required to use this script
21-
- Python 2.7
21+
- Python 3
2222
- The [requests](http://docs.python-requests.org/en/master/) library
2323

24-
The scripts support Python 2 only, not Python 3.
24+
All scripts support Python 3 and not compatible with Python 2.
2525

2626
## How to run the script
2727
Run the script via the command line (or shell)
@@ -70,3 +70,6 @@ The best way is via the pip package management tool
7070
- modify the rest of application files
7171
- version 1.0.7: 31 Aug 2017
7272
- revise README.md
73+
- version 1.0.8: 04 Sep 2017
74+
- Port all scripts to support Python 3
75+
- Fix the issue that some scripts still send request message to the old REST endpoint.

docs/TRKD_REST_with_Python.docx

763 Bytes
Binary file not shown.

trkd_authen.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
if __name__ == '__main__':
1717
## Get username, password and applicationid
18-
username = raw_input('Please input username: ')
18+
username = input('Please input username: ')
1919
## use getpass.getpass to hide user inputted password
2020
password = getpass.getpass(prompt='Please input password: ')
21-
appid = raw_input('Please input appid: ')
21+
appid = input('Please input appid: ')
2222
print('############### Sending Authentication request message to TRKD ###############')
2323

2424
##create authentication request URL, message and header
@@ -49,10 +49,4 @@
4949
except requests.exceptions.RequestException as e:
5050
print('Exception!!!')
5151
print(e)
52-
sys.exit(1)
53-
54-
55-
56-
57-
58-
52+
sys.exit(1)

trkd_chart.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import requests
1414
import json
1515
import getpass
16-
import urllib2
16+
import urllib
1717

1818
# Send HTTP request for all services
1919
def doSendRequest(url, requestMsg, headers):
@@ -55,8 +55,7 @@ def CreateAuthorization(username, password, appid):
5555
## Perform Chart request
5656
def RetrieveChart(token, appid):
5757
##construct a Chart request message
58-
ricName = raw_input('Please input Symbol: ')
59-
58+
ricName = input('Please input Symbol: ')
6059
chartRequestMsg = {'GetChart_Request_2': {'chartRequest': {
6160
'TimeSeries': {'TimeSeriesRequest_typehint': ['TimeSeriesRequest'],
6261
'TimeSeriesRequest': [{'Symbol': ricName,
@@ -297,9 +296,9 @@ def downloadChartImage(chartURL):
297296
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
298297
headers = { 'User-Agent' : user_agent }
299298
print('\nDownlading chart.png file from %s'%(chartURL))
300-
##download image using Python urllib2
301-
downloadResult = urllib2.Request(chartURL, headers=headers)
302-
imgData = urllib2.urlopen(downloadResult).read()
299+
##download image using Python3 urllib
300+
downloadResult = urllib.request.Request(chartURL, headers=headers)
301+
imgData = urllib.request.urlopen(downloadResult).read()
303302
##write file
304303
fileName = './chart.png'
305304
with open(fileName,'wb') as outfile:
@@ -312,10 +311,10 @@ def downloadChartImage(chartURL):
312311

313312
if __name__ == '__main__':
314313
##Get username, password and applicationid
315-
username = raw_input('Please input username: ')
314+
username = input('Please input username: ')
316315
##use getpass.getpass to hide user inputted password
317316
password = getpass.getpass(prompt='Please input password: ')
318-
appid = raw_input('Please input appid: ')
317+
appid = input('Please input appid: ')
319318

320319
token = CreateAuthorization(username,password,appid)
321320
print('Token = %s'%(token))
@@ -326,17 +325,4 @@ def downloadChartImage(chartURL):
326325
if chartURL is not None:
327326
print('############### Downloading Chart file from TRKD ###############')
328327
downloadChartImage(chartURL)
329-
330-
331-
332-
333-
334-
335-
336-
337-
338-
339-
340-
341-
342-
328+

trkd_interday.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ def CreateAuthorization(username, password, appid):
5454
## Perform Interday request
5555
def RetrieveInteraday(token, appid):
5656
##construct Time Series Interday request message
57-
ricName = raw_input('Please input Symbol: ')
57+
ricName = input('Please input Symbol: ')
5858
interdayRequestMsg = None
5959
fields = ['OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK'] #change your fields (support these 'OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK' fields only)
6060
startTime = '2015-09-22T00:00:00' #change your StartTime
6161
endtime = '2016-09-22T23:59:00' #change your EndTime
6262
#interval = 'DAILY' # change your interval between 'DAILY', 'WEEKLY', 'MONTHLY', 'QUARTERLY' and 'ANNUAL'
63-
interval = raw_input('Input interested interval (\'DAILY\' or \'WEEKLY\' or \'MONTHLY\' or \'QUARTERLY\' or \'ANNUAL\'): ')
63+
interval = input('Input interested interval (\'DAILY\' or \'WEEKLY\' or \'MONTHLY\' or \'QUARTERLY\' or \'ANNUAL\'): ')
6464
interdayRequestMsg = {
6565
'GetInterdayTimeSeries_Request_4':{
6666
'Field': fields,
@@ -74,6 +74,10 @@ def RetrieveInteraday(token, appid):
7474
}
7575
}
7676
##construct Time Series Interday URL and header
77+
<<<<<<< HEAD
78+
=======
79+
#interdayURL = 'http://api.rkd.reuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetInterdayTimeSeries_4'
80+
>>>>>>> v14_python3
7781
interdayURL = 'http://api.trkd.thomsonreuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetInterdayTimeSeries_4'
7882
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
7983

@@ -87,27 +91,13 @@ def RetrieveInteraday(token, appid):
8791
## ------------------------------------------ Main App ------------------------------------------ ##
8892
if __name__ == '__main__':
8993
##Get username, password and applicationid
90-
username = raw_input('Please input username: ')
94+
username = input('Please input username: ')
9195
##use getpass.getpass to hide user inputted password
9296
password = getpass.getpass(prompt='Please input password: ')
93-
appid = raw_input('Please input appid: ')
97+
appid = input('Please input appid: ')
9498

9599
token = CreateAuthorization(username, password, appid)
96100
print('Token = %s'%(token))
97101
## if authentiacation success, continue subscribing Time Series interday
98102
if token is not None:
99103
RetrieveInteraday(token, appid)
100-
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-

trkd_intraday.py

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
import getpass
1616

1717
# Send HTTP request for all services
18+
19+
1820
def doSendRequest(url, requestMsg, headers):
1921
result = None
2022
try:
21-
##send request
22-
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
23-
## handle error
23+
# send request
24+
result = requests.post(
25+
url, data=json.dumps(requestMsg), headers=headers)
26+
# handle error
2427
if result.status_code is not 200:
2528
print('Request fail')
26-
print('response status %s'%(result.status_code))
27-
if result.status_code == 500: ## if username or password or appid is wrong
28-
print('Error: %s'%(result.json()))
29+
print('response status %s' % (result.status_code))
30+
if result.status_code == 500: # if username or password or appid is wrong
31+
print('Error: %s' % (result.json()))
2932
result.raise_for_status()
3033
except requests.exceptions.RequestException as e:
3134
print('Exception!!!')
@@ -34,81 +37,73 @@ def doSendRequest(url, requestMsg, headers):
3437
return result
3538

3639

37-
## Perform authentication
40+
# Perform authentication
3841
def CreateAuthorization(username, password, appid):
3942
token = None
40-
##create authentication request URL, message and header
41-
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
43+
# create authentication request URL, message and header
44+
authenMsg = {'CreateServiceToken_Request_1': {
45+
'ApplicationID': appid, 'Username': username, 'Password': password}}
4246
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
4347
headers = {'content-type': 'application/json;charset=utf-8'}
4448
print('############### Sending Authentication request message to TRKD ###############')
4549
authenResult = doSendRequest(authenURL, authenMsg, headers)
4650
if authenResult is not None and authenResult.status_code == 200:
4751
print('Authen success')
48-
print('response status %s'%(authenResult.status_code))
49-
##get Token
52+
print('response status %s' % (authenResult.status_code))
53+
# get Token
5054
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
51-
55+
5256
return token
5357

54-
## Perform Intraday request
58+
# Perform Intraday request
59+
60+
5561
def RetrieveIntraday(token, appid):
56-
##construct Time Series Intraday request message
57-
ricName = raw_input('Please input Symbol: ')
62+
# construct Time Series Intraday request message
63+
ricName = input('Please input Symbol: ')
5864
intradayRequestMsg = None
59-
fields = ['OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK'] #change your fields (support these 'OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK' fields only)
60-
startTime = '2016-09-12T00:00:00' #change your StartTime
61-
endtime = '2016-09-19T23:59:00' #change your EndTime
62-
#interval = 'MINUTE' # change your interval between 'MINUTE', '5MINUTES', '30MINUTES' and 'HOUR'
63-
interval = raw_input('Input interested interval (\'MINUTE\' or \'5MINUTES\' or \'30MINUTES\' or \'HOUR\'): ')
65+
# change your fields (support these 'OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK' fields only)
66+
fields = ['OPEN', 'HIGH', 'LOW', 'CLOSE',
67+
'CLOSEYIELD', 'VOLUME', 'BID', 'ASK']
68+
startTime = '2016-09-12T00:00:00' # change your StartTime
69+
endtime = '2016-09-19T23:59:00' # change your EndTime
70+
# interval = 'MINUTE' # change your interval between 'MINUTE', '5MINUTES', '30MINUTES' and 'HOUR'
71+
interval = input(
72+
'Input interested interval (\'MINUTE\' or \'5MINUTES\' or \'30MINUTES\' or \'HOUR\'): ')
6473
intradayRequestMsg = {
65-
'GetIntradayTimeSeries_Request_4':{
74+
'GetIntradayTimeSeries_Request_4': {
6675
'Field': fields,
6776
'TrimResponse': True,
6877
'Symbol': ricName,
69-
'StartTime':startTime,
70-
'EndTime':endtime,
71-
'Interval':interval,
78+
'StartTime': startTime,
79+
'EndTime': endtime,
80+
'Interval': interval,
7281
'TrimResponse': True,
73-
'MetaField': ['NAME','QOS','CCY','TZ','TZOFFSET','NAME_LL']
82+
'MetaField': ['NAME', 'QOS', 'CCY', 'TZ', 'TZOFFSET', 'NAME_LL']
7483
}
7584
}
76-
##construct Time Series Intraday URL and header
85+
# construct Time Series Intraday URL and header
7786
intradayURL = 'http://api.trkd.thomsonreuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetIntradayTimeSeries_4'
78-
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
79-
87+
headers = {'content-type': 'application/json;charset=utf-8',
88+
'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token': token}
89+
8090
print('############### Sending Time Series Intraday request message to TRKD ###############')
81-
intradayResult = doSendRequest(intradayURL, intradayRequestMsg,headers)
91+
intradayResult = doSendRequest(intradayURL, intradayRequestMsg, headers)
8292
if intradayResult is not None and intradayResult.status_code == 200:
8393
print('Time Series Intraday response message: ')
8494
print(intradayResult.json())
8595

8696

8797
## ------------------------------------------ Main App ------------------------------------------ ##
8898
if __name__ == '__main__':
89-
##Get username, password and applicationid
90-
username = raw_input('Please input username: ')
91-
##use getpass.getpass to hide user inputted password
99+
# Get username, password and applicationid
100+
username = input('Please input username: ')
101+
# use getpass.getpass to hide user inputted password
92102
password = getpass.getpass(prompt='Please input password: ')
93-
appid = raw_input('Please input appid: ')
94-
103+
appid = input('Please input appid: ')
95104

96-
token = CreateAuthorization(username,password,appid)
97-
print('Token = %s'%(token))
98-
## if authentiacation success, continue subscribing Time Series intraday
105+
token = CreateAuthorization(username, password, appid)
106+
print('Token = %s' % (token))
107+
# if authentiacation success, continue subscribing Time Series intraday
99108
if token is not None:
100-
RetrieveIntraday(token,appid)
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
109+
RetrieveIntraday(token, appid)

trkd_newsheadline.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def RetrieveNewsHeadline(token, appid):
5656
newsURL = 'https://api.trkd.thomsonreuters.com/api/News/News.svc/REST/News_1/RetrieveHeadlineML_1'
5757
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
5858
##construct a news headline request message
59-
ricName = raw_input('Please input Symbol: ')
59+
ricName = input('Please input Symbol: ')
6060
newsRequestMsg = {'RetrieveHeadlineML_Request_1': {
6161
'HeadlineMLRequest':{
6262
'MaxCount':10,
@@ -85,14 +85,14 @@ def RetrieveNewsHeadline(token, appid):
8585

8686
if __name__ == '__main__':
8787
##Get username, password and applicationid
88-
username = raw_input('Please input username: ')
88+
username = input('Please input username: ')
8989
##use getpass.getpass to hide user inputted password
9090
password = getpass.getpass(prompt='Please input password: ')
91-
appid = raw_input('Please input appid: ')
91+
appid = input('Please input appid: ')
9292

9393
token = CreateAuthorization(username,password,appid)
9494
print('Token = %s'%(token))
9595

9696
## if authentiacation success, continue subscribing News Headline
9797
if token is not None:
98-
RetrieveNewsHeadline(token,appid)
98+
RetrieveNewsHeadline(token,appid)

trkd_newsstory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def RetrieveNewsStory(token, appid):
5959
headers = {'content-type': 'application/json;charset=utf-8',
6060
'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token': token}
6161
# construct a news story request message
62-
storyid = raw_input('Please input news story id: ')
62+
storyid = input('Please input news story id: ')
6363
newsRequestMsg = {'RetrieveStoryML_Request_1': {
6464
'StoryMLRequest': {
6565
'StoryId': [storyid]
@@ -78,10 +78,10 @@ def RetrieveNewsStory(token, appid):
7878

7979
if __name__ == '__main__':
8080
# Get username, password and applicationid
81-
username = raw_input('Please input username: ')
81+
username = input('Please input username: ')
8282
# use getpass.getpass to hide user inputted password
8383
password = getpass.getpass(prompt='Please input password: ')
84-
appid = raw_input('Please input appid: ')
84+
appid = input('Please input appid: ')
8585

8686
token = CreateAuthorization(username, password, appid)
8787
print('Token = %s' % (token))

trkd_onlinereport.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def CreateAuthorization(username, password, appid):
4242
'ApplicationID': appid, 'Username': username, 'Password': password}}
4343
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
4444
headers = {'content-type': 'application/json;charset=utf-8'}
45-
print '############### Sending Authentication request message to TRKD ###############'
45+
print('############### Sending Authentication request message to TRKD ###############')
4646
authenResult = doSendRequest(authenURL, authenMsg, headers)
4747
if authenResult is not None and authenResult.status_code == 200:
4848
print('Authen success')
@@ -80,10 +80,10 @@ def RetrieveOnlineReport(token, appid):
8080

8181
if __name__ == '__main__':
8282
# Get username, password and applicationid
83-
username = raw_input('Please input username: ')
83+
username = input('Please input username: ')
8484
# use getpass.getpass to hide user inputted password
8585
password = getpass.getpass(prompt='Please input password: ')
86-
appid = raw_input('Please input appid: ')
86+
appid = input('Please input appid: ')
8787

8888
token = CreateAuthorization(username, password, appid)
8989
print('Token = %s' % (token))

0 commit comments

Comments
 (0)