|
| 1 | +# |----------------------------------------------------------------------------- |
| 2 | +# | This source code is provided under the Apache 2.0 license -- |
| 3 | +# | and is provided AS IS with no warranty or guarantee of fit for purpose. -- |
| 4 | +# | See the project's LICENSE.md for details. -- |
| 5 | +# | Copyright Thomson Reuters 2017. All rights reserved. -- |
| 6 | +# |----------------------------------------------------------------------------- |
| 7 | + |
| 8 | + |
| 9 | +#!/usr/bin/env python |
| 10 | +""" Simple example of outputting Market Price JSON data using Websockets """ |
| 11 | + |
| 12 | +import sys |
| 13 | +import time |
| 14 | +import getopt |
| 15 | +import socket |
| 16 | +import json |
| 17 | +import websocket |
| 18 | +import threading |
| 19 | +from threading import Thread, Event |
| 20 | + |
| 21 | +# Global Default Variables |
| 22 | +hostname = '172.20.33.30' |
| 23 | +port = '15000' |
| 24 | +user = 'root' |
| 25 | +app_id = '256' |
| 26 | +position = socket.gethostbyname(socket.gethostname()) |
| 27 | +mrn_domain = 'NewsTextAnalytics' |
| 28 | +mrn_item = 'MRN_STORY' |
| 29 | + |
| 30 | +# Global Variables |
| 31 | +web_socket_app = None |
| 32 | +web_socket_open = False |
| 33 | + |
| 34 | +''' MRN Process Code ''' |
| 35 | + |
| 36 | + |
| 37 | +def send_mrn_request(ws): |
| 38 | + """ Create and send MRN request """ |
| 39 | + mrn_req_json = { |
| 40 | + 'ID': 2, |
| 41 | + 'Domain': mrn_domain, |
| 42 | + 'Key': { |
| 43 | + 'Name': mrn_item |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + ws.send(json.dumps(mrn_req_json)) |
| 48 | + print("SENT:") |
| 49 | + print(json.dumps(mrn_req_json, sort_keys=True, indent=2, separators=(',', ':'))) |
| 50 | + |
| 51 | + |
| 52 | +def processRefresh(ws): |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def processUpdate(ws): |
| 57 | + return None |
| 58 | + |
| 59 | + |
| 60 | +''' JSON-OMM Process functions ''' |
| 61 | + |
| 62 | + |
| 63 | +def process_message(ws, message_json): |
| 64 | + """ Parse at high level and output JSON of message """ |
| 65 | + message_type = message_json['Type'] |
| 66 | + |
| 67 | + if message_type == "Refresh": |
| 68 | + if 'Domain' in message_json: |
| 69 | + message_domain = message_json['Domain'] |
| 70 | + if message_domain == "Login": |
| 71 | + process_login_response(ws, message_json) |
| 72 | + elif message_type == "Ping": |
| 73 | + pong_json = {'Type': 'Pong'} |
| 74 | + ws.send(json.dumps(pong_json)) |
| 75 | + print("SENT:") |
| 76 | + print(json.dumps(pong_json, sort_keys=True, |
| 77 | + indent=2, separators=(',', ':'))) |
| 78 | + |
| 79 | + |
| 80 | +def process_login_response(ws, message_json): |
| 81 | + """ Send item request """ |
| 82 | + # send_market_price_request(ws) |
| 83 | + send_mrn_request(ws) |
| 84 | + |
| 85 | + |
| 86 | +def send_market_price_request(ws): |
| 87 | + """ Create and send simple Market Price request """ |
| 88 | + mp_req_json = { |
| 89 | + 'ID': 2, |
| 90 | + 'Key': { |
| 91 | + 'Name': ['EUR=', 'JPY=', 'THB='], |
| 92 | + }, |
| 93 | + } |
| 94 | + ws.send(json.dumps(mp_req_json)) |
| 95 | + print("SENT:") |
| 96 | + print(json.dumps(mp_req_json, sort_keys=True, indent=2, separators=(',', ':'))) |
| 97 | + |
| 98 | + |
| 99 | +def send_login_request(ws): |
| 100 | + """ Generate a login request from command line data (or defaults) and send """ |
| 101 | + login_json = { |
| 102 | + 'ID': 1, |
| 103 | + 'Domain': 'Login', |
| 104 | + 'Key': { |
| 105 | + 'Name': '', |
| 106 | + 'Elements': { |
| 107 | + 'ApplicationId': '', |
| 108 | + 'Position': '' |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + login_json['Key']['Name'] = user |
| 114 | + login_json['Key']['Elements']['ApplicationId'] = app_id |
| 115 | + login_json['Key']['Elements']['Position'] = position |
| 116 | + |
| 117 | + ws.send(json.dumps(login_json)) |
| 118 | + print("SENT:") |
| 119 | + print(json.dumps(login_json, sort_keys=True, indent=2, separators=(',', ':'))) |
| 120 | + |
| 121 | + |
| 122 | +''' WebSocket Process functions ''' |
| 123 | + |
| 124 | + |
| 125 | +def on_message(ws, message): |
| 126 | + """ Called when message received, parse message into JSON for processing """ |
| 127 | + print("RECEIVED: ") |
| 128 | + message_json = json.loads(message) |
| 129 | + print(json.dumps(message_json, sort_keys=True, indent=2, separators=(',', ':'))) |
| 130 | + |
| 131 | + for singleMsg in message_json: |
| 132 | + process_message(ws, singleMsg) |
| 133 | + |
| 134 | + |
| 135 | +def on_error(ws, error): |
| 136 | + """ Called when websocket error has occurred """ |
| 137 | + print(error) |
| 138 | + |
| 139 | + |
| 140 | +def on_close(ws): |
| 141 | + """ Called when websocket is closed """ |
| 142 | + global web_socket_open |
| 143 | + print("WebSocket Closed") |
| 144 | + web_socket_open = False |
| 145 | + |
| 146 | + |
| 147 | +def on_open(ws): |
| 148 | + """ Called when handshake is complete and websocket is open, send login """ |
| 149 | + |
| 150 | + print("WebSocket successfully connected!") |
| 151 | + global web_socket_open |
| 152 | + web_socket_open = True |
| 153 | + send_login_request(ws) |
| 154 | + |
| 155 | + |
| 156 | +''' Main Process Code ''' |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + |
| 160 | + # Get command line parameters |
| 161 | + try: |
| 162 | + opts, args = getopt.getopt(sys.argv[1:], "", [ |
| 163 | + "help", "hostname=", "port=", "app_id=", "user=", "position="]) |
| 164 | + except getopt.GetoptError: |
| 165 | + print( |
| 166 | + 'Usage: market_price.py [--hostname hostname] [--port port] [--app_id app_id] [--user user] [--position position] [--help]') |
| 167 | + sys.exit(2) |
| 168 | + for opt, arg in opts: |
| 169 | + if opt in ("--help"): |
| 170 | + print( |
| 171 | + 'Usage: market_price.py [--hostname hostname] [--port port] [--app_id app_id] [--user user] [--position position] [--help]') |
| 172 | + sys.exit(0) |
| 173 | + elif opt in ("--hostname"): |
| 174 | + hostname = arg |
| 175 | + elif opt in ("--port"): |
| 176 | + port = arg |
| 177 | + elif opt in ("--app_id"): |
| 178 | + app_id = arg |
| 179 | + elif opt in ("--user"): |
| 180 | + user = arg |
| 181 | + elif opt in ("--position"): |
| 182 | + position = arg |
| 183 | + |
| 184 | + # Start websocket handshake |
| 185 | + ws_address = "ws://{}:{}/WebSocket".format(hostname, port) |
| 186 | + print("Connecting to WebSocket " + ws_address + " ...") |
| 187 | + web_socket_app = websocket.WebSocketApp(ws_address, header=['User-Agent: Python'], |
| 188 | + on_message=on_message, |
| 189 | + on_error=on_error, |
| 190 | + on_close=on_close, |
| 191 | + subprotocols=['tr_json2']) |
| 192 | + web_socket_app.on_open = on_open |
| 193 | + |
| 194 | + # Event loop |
| 195 | + wst = threading.Thread(target=web_socket_app.run_forever) |
| 196 | + wst.start() |
| 197 | + |
| 198 | + try: |
| 199 | + while True: |
| 200 | + time.sleep(1) |
| 201 | + except KeyboardInterrupt: |
| 202 | + web_socket_app.close() |
0 commit comments