Skip to content

Commit 2dad733

Browse files
author
Wasin Waeosri
committed
changes log:
1. Port all scripts to support Python 3 2. Fix trkd_chart.py, trkd_interday.py, trkd_intraday.py, trkd_newsstory.py and trkd_onlinereport.py to use the new REST endpoint URL. 3. Change README.md for support Python 3 notification
1 parent e1d0f9b commit 2dad733

File tree

9 files changed

+102
-160
lines changed

9 files changed

+102
-160
lines changed

README.md

Lines changed: 6 additions & 12 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 are based on Python 2 but you can modify it to run with Python 3 (see "Optional - How to run with Python 3" section).
24+
All scripts are based on 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)
@@ -41,16 +41,7 @@ The best way is via the pip package management tool
4141
export https_proxy="http://<proxy.server>:<port>"
4242
$>pip install requests
4343
```
44-
## Optional - How to run with Python 3
45-
You can modify the scripts to run with Python 3 (with requests library installed) by just change the code from "**raw_input()**" to "**input()**" as the following example
46-
- Python 2
47-
```
48-
username = raw_input('Please input username: ')
49-
```
50-
- Python 3
51-
```
52-
username = input('Please input username: ')
53-
```
44+
5445

5546
## Release Note
5647
- Version 1: 6 Sep 2016
@@ -80,3 +71,6 @@ username = input('Please input username: ')
8071
- modify the rest of application files
8172
- version 1.0.7: 31 Aug 2017
8273
- revise README.md
74+
- version 1.0.8: 04 Sep 2017
75+
- Port all scripts to support Python 3
76+
- Fix the issue that some scripts still send request message to the old REST endpoint.

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: 10 additions & 24 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):
@@ -28,7 +28,7 @@ def doSendRequest(url, requestMsg, headers):
2828
if result.status_code == 500: ## if username or password or appid is wrong
2929
print('Error: %s'%(result.json()))
3030
result.raise_for_status()
31-
except requests.exceptions.RequestException, e:
31+
except requests.exceptions.RequestException as e:
3232
print('Exception!!!')
3333
print(e)
3434
sys.exit(1)
@@ -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,
@@ -274,7 +273,7 @@ def RetrieveChart(token, appid):
274273
'ReturnPrivateNetworkURL': False,
275274
}}}
276275
##construct Chart URL and header
277-
chartURL = 'http://api.rkd.reuters.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2BB'
276+
chartURL = 'http://api.trkd.thomsonreuters.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2'
278277
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
279278

280279
print('############### Sending Chart request message to TRKD ###############')
@@ -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: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def doSendRequest(url, requestMsg, headers):
2727
if result.status_code == 500: ## if username or password or appid is wrong
2828
print('Error: %s'%(result.json()))
2929
result.raise_for_status()
30-
except requests.exceptions.RequestException, e:
30+
except requests.exceptions.RequestException as e:
3131
print('Exception!!!')
3232
print(e)
3333
sys.exit(1)
@@ -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,7 +74,8 @@ def RetrieveInteraday(token, appid):
7474
}
7575
}
7676
##construct Time Series Interday URL and header
77-
interdayURL = 'http://api.rkd.reuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetInterdayTimeSeries_4'
77+
#interdayURL = 'http://api.rkd.reuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetInterdayTimeSeries_4'
78+
interdayURL = 'http://api.trkd.thomsonreuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetInterdayTimeSeries_4'
7879
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
7980

8081
print('############### Sending Time Series Interday request message to TRKD ###############')
@@ -87,27 +88,13 @@ def RetrieveInteraday(token, appid):
8788
## ------------------------------------------ Main App ------------------------------------------ ##
8889
if __name__ == '__main__':
8990
##Get username, password and applicationid
90-
username = raw_input('Please input username: ')
91+
username = input('Please input username: ')
9192
##use getpass.getpass to hide user inputted password
9293
password = getpass.getpass(prompt='Please input password: ')
93-
appid = raw_input('Please input appid: ')
94+
appid = input('Please input appid: ')
9495

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

trkd_intraday.py

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,100 +15,95 @@
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()
30-
except requests.exceptions.RequestException, e:
33+
except requests.exceptions.RequestException as e:
3134
print('Exception!!!')
3235
print(e)
3336
sys.exit(1)
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
77-
intradayURL = 'http://api.rkd.reuters.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-
85+
# construct Time Series Intraday URL and header
86+
intradayURL = 'http://api.trkd.thomsonreuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetIntradayTimeSeries_4'
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)

0 commit comments

Comments
 (0)