Skip to content

Commit ebfc13e

Browse files
author
Wasin Waeosri
committed
Change logs:
1. Add .gitignore 2. Addd time expiration to trkd_wsstreaming.py
1 parent 6114574 commit ebfc13e

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
chart.png
22
images/
3-
docs/TRKD_REST_with_Python_master.doc
3+
docs/TRKD_REST_with_Python_master.doc
4+
note.txt
5+
test.py
6+
market_price_edpgw_authentication.py
7+
.vscode/

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
###### Requirements Libraries ######
22
requests
3-
websocket-client>0.49
3+
websocket-client>0.49
4+
python-dateutil

trkd_wsstreaming.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import threading
2020
from threading import Thread, Event
2121

22+
from datetime import datetime, timezone, timedelta
23+
import dateutil.parser
24+
2225

2326
# Global Default Variables
2427
ws_address = 'wss://streaming.trkd.thomsonreuters.com/WebSocket/'
@@ -35,6 +38,9 @@
3538
token = None
3639
expiration = None
3740

41+
expire_time_in_seconds = None
42+
time_before_expire_in_seconds = 15 * 60 # 15 Minutes to Seconds
43+
3844
## ------------------------------------------ TRKD HTTP REST functions ------------------------------------------ ##
3945

4046
# Send HTTP request for all services
@@ -72,9 +78,16 @@ def CreateAuthorization(username, password, appid):
7278
print('Authentication response %s'%json.dumps(authenResult.json(), sort_keys=True, indent=2, separators=(',', ':')))
7379
##get Token
7480
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
75-
expiration = authenResult.json()['CreateServiceToken_Response_1']['Expiration']
81+
expiration = authenResult.json()['CreateServiceToken_Response_1']['Expiration'] # Expiration time of this session in UTC
82+
83+
## Calcuate Expiration time
84+
expire_datetime_utc = dateutil.parser.parse(expiration) ## Parse incoming Expiration to Python datetime object (UTC)
85+
utc_time_now = datetime.now(timezone.utc) ## Get current machine datetime in UTC
86+
87+
time_difference = expire_datetime_utc - utc_time_now ## Get time different between now and expiration time value
88+
time_difference_in_seconds = int(round(time_difference / timedelta(seconds=1))) ## convert it to second as a round int
7689

77-
return token, expiration
90+
return token, expiration, time_difference_in_seconds
7891

7992
## ------------------------------------------ TRKD WebSocket functions ------------------------------------------ ##
8093

@@ -177,8 +190,10 @@ def on_open(ws):
177190
password = getpass.getpass(prompt='Please input password: ')
178191
appid = input('Please input appid: ')
179192

180-
token, expiration = CreateAuthorization(username,password,appid)
181-
# print('Token = %s'%(token))
193+
token, expiration, expire_time_in_seconds = CreateAuthorization(username,password,appid)
194+
print('Token = %s'%(token))
195+
print('Expiration = %s'%(expiration))
196+
print('Expiration in next = %d seconds'%(expire_time_in_seconds))
182197
## if authentiacation success, continue subscribing Quote
183198
if token and expiration:
184199
print('Do WS here')

0 commit comments

Comments
 (0)