|
19 | 19 | import threading |
20 | 20 | from threading import Thread, Event |
21 | 21 |
|
| 22 | +from datetime import datetime, timezone, timedelta |
| 23 | +import dateutil.parser |
| 24 | + |
22 | 25 |
|
23 | 26 | # Global Default Variables |
24 | 27 | ws_address = 'wss://streaming.trkd.thomsonreuters.com/WebSocket/' |
|
35 | 38 | token = None |
36 | 39 | expiration = None |
37 | 40 |
|
| 41 | +expire_time_in_seconds = None |
| 42 | +time_before_expire_in_seconds = 15 * 60 # 15 Minutes to Seconds |
| 43 | + |
38 | 44 | ## ------------------------------------------ TRKD HTTP REST functions ------------------------------------------ ## |
39 | 45 |
|
40 | 46 | # Send HTTP request for all services |
@@ -72,9 +78,16 @@ def CreateAuthorization(username, password, appid): |
72 | 78 | print('Authentication response %s'%json.dumps(authenResult.json(), sort_keys=True, indent=2, separators=(',', ':'))) |
73 | 79 | ##get Token |
74 | 80 | 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 |
76 | 89 |
|
77 | | - return token, expiration |
| 90 | + return token, expiration, time_difference_in_seconds |
78 | 91 |
|
79 | 92 | ## ------------------------------------------ TRKD WebSocket functions ------------------------------------------ ## |
80 | 93 |
|
@@ -177,8 +190,10 @@ def on_open(ws): |
177 | 190 | password = getpass.getpass(prompt='Please input password: ') |
178 | 191 | appid = input('Please input appid: ') |
179 | 192 |
|
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)) |
182 | 197 | ## if authentiacation success, continue subscribing Quote |
183 | 198 | if token and expiration: |
184 | 199 | print('Do WS here') |
|
0 commit comments