mirror of
https://github.com/EDCD/EDDN.git
synced 2025-06-03 09:01:40 +03:00
Add python 3.4 example
This commit is contained in:
parent
58f47222f3
commit
f9376c59ea
@ -103,6 +103,9 @@ while (true)
|
||||
try
|
||||
{
|
||||
$subscriber->connect($relayEDDN);
|
||||
echoLog('Connect to ' . $relayEDDN);
|
||||
echoLog('');
|
||||
echoLog('');
|
||||
|
||||
while (true)
|
||||
{
|
||||
@ -111,6 +114,9 @@ while (true)
|
||||
if ($message === false)
|
||||
{
|
||||
$subscriber->disconnect($relayEDDN);
|
||||
echoLog('Disconnect from ' . $relayEDDN);
|
||||
echoLog('');
|
||||
echoLog('');
|
||||
break;
|
||||
}
|
||||
|
||||
|
3
examples/Python 2.7/Client_Complete.bat
Normal file
3
examples/Python 2.7/Client_Complete.bat
Normal file
@ -0,0 +1,3 @@
|
||||
"C:\Python27\python.exe" "%~dp0\Client_Complete.py"
|
||||
|
||||
pause
|
@ -1,5 +1,5 @@
|
||||
import zlib
|
||||
import zmq.green as zmq
|
||||
import zmq
|
||||
import simplejson
|
||||
import sys, os, datetime, time
|
||||
|
||||
@ -96,12 +96,18 @@ def main():
|
||||
while True:
|
||||
try:
|
||||
subscriber.connect(__relayEDDN)
|
||||
echoLog('Connect to ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
while True:
|
||||
__message = subscriber.recv()
|
||||
|
||||
if __message == False:
|
||||
subscriber.disconnect(__relayEDDN)
|
||||
echoLog('Disconnect from ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
break
|
||||
|
||||
__message = zlib.decompress(__message)
|
||||
@ -112,8 +118,8 @@ def main():
|
||||
# Handle commodity v1
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v1 message...');
|
||||
echoLog(' - Converting to v2...');
|
||||
echoLog('Receiving commodity-v1 message...')
|
||||
echoLog(' - Converting to v2...')
|
||||
|
||||
__temp = {}
|
||||
__temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else '')
|
3
examples/Python 2.7/Client_Simple.bat
Normal file
3
examples/Python 2.7/Client_Simple.bat
Normal file
@ -0,0 +1,3 @@
|
||||
"C:\Python27\python.exe" "%~dp0\Client_Simple.py"
|
||||
|
||||
pause
|
@ -1,5 +1,5 @@
|
||||
import zlib
|
||||
import zmq.green as zmq
|
||||
import zmq
|
||||
import simplejson
|
||||
import sys
|
||||
|
3
examples/Python 3.4/Client_Complete.bat
Normal file
3
examples/Python 3.4/Client_Complete.bat
Normal file
@ -0,0 +1,3 @@
|
||||
"C:\Python34\python.exe" "%~dp0\Client_Complete.py"
|
||||
|
||||
pause
|
218
examples/Python 3.4/Client_Complete.py
Normal file
218
examples/Python 3.4/Client_Complete.py
Normal file
@ -0,0 +1,218 @@
|
||||
import zlib
|
||||
import zmq
|
||||
import simplejson
|
||||
import sys, os, datetime, time
|
||||
|
||||
"""
|
||||
" Configuration
|
||||
"""
|
||||
__relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500'
|
||||
__timeoutEDDN = 600000
|
||||
|
||||
# Set False to listen to production stream
|
||||
__debugEDDN = True;
|
||||
|
||||
# Set to False if you do not want verbose logging
|
||||
__logVerboseFile = os.path.dirname(__file__) + '/Logs_Verbose_EDDN_%DATE%.htm'
|
||||
#__logVerboseFile = False
|
||||
|
||||
# Set to False if you do not want JSON logging
|
||||
__logJSONFile = os.path.dirname(__file__) + '/Logs_JSON_EDDN_%DATE%.log'
|
||||
#__logJSONFile = False
|
||||
|
||||
# A sample list of authorised softwares
|
||||
__authorisedSoftwares = [
|
||||
"EDCE",
|
||||
"ED-TD.SPACE",
|
||||
"EliteOCR",
|
||||
"Maddavo's Market Share",
|
||||
"RegulatedNoise",
|
||||
"RegulatedNoise__DJ"
|
||||
]
|
||||
|
||||
# Used this to excludes yourself for example has you don't want to handle your own messages ^^
|
||||
__excludedSoftwares = [
|
||||
'My Awesome Market Uploader'
|
||||
]
|
||||
|
||||
|
||||
|
||||
"""
|
||||
" Start
|
||||
"""
|
||||
def date(__format):
|
||||
d = datetime.datetime.utcnow()
|
||||
return d.strftime(__format)
|
||||
|
||||
|
||||
__oldTime = False
|
||||
def echoLog(__str):
|
||||
global __oldTime, __logVerboseFile
|
||||
|
||||
if __logVerboseFile != False:
|
||||
__logVerboseFileParsed = __logVerboseFile.replace('%DATE%', str(date('%Y-%m-%d')))
|
||||
|
||||
if __logVerboseFile != False and not os.path.exists(__logVerboseFileParsed):
|
||||
f = open(__logVerboseFileParsed, 'w')
|
||||
f.write('<style type="text/css">html { white-space: pre; font-family: Courier New,Courier,Lucida Sans Typewriter,Lucida Typewriter,monospace; }</style>')
|
||||
f.close()
|
||||
|
||||
if (__oldTime == False) or (__oldTime != date('%H:%M:%S')):
|
||||
__oldTime = date('%H:%M:%S')
|
||||
__str = str(__oldTime) + ' | ' + str(__str)
|
||||
else:
|
||||
__str = ' ' + ' | ' + str(__str)
|
||||
|
||||
print (__str)
|
||||
sys.stdout.flush()
|
||||
|
||||
if __logVerboseFile != False:
|
||||
f = open(__logVerboseFileParsed, 'a')
|
||||
f.write(__str + '\n')
|
||||
f.close()
|
||||
|
||||
|
||||
def echoLogJSON(__json):
|
||||
global __logJSONFile
|
||||
|
||||
if __logJSONFile != False:
|
||||
__logJSONFileParsed = __logJSONFile.replace('%DATE%', str(date('%Y-%m-%d')))
|
||||
|
||||
f = open(__logJSONFileParsed, 'a')
|
||||
f.write(str(__json) + '\n')
|
||||
f.close()
|
||||
|
||||
|
||||
def main():
|
||||
echoLog('Starting EDDN Subscriber')
|
||||
echoLog('')
|
||||
|
||||
context = zmq.Context()
|
||||
subscriber = context.socket(zmq.SUB)
|
||||
|
||||
subscriber.setsockopt(zmq.SUBSCRIBE, b"")
|
||||
subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN)
|
||||
|
||||
while True:
|
||||
try:
|
||||
subscriber.connect(__relayEDDN)
|
||||
echoLog('Connect to ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
while True:
|
||||
__message = subscriber.recv()
|
||||
|
||||
if __message == False:
|
||||
subscriber.disconnect(__relayEDDN)
|
||||
echoLog('Disconnect from ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
break
|
||||
|
||||
__message = zlib.decompress(__message)
|
||||
__json = simplejson.loads(__message)
|
||||
__converted = False
|
||||
|
||||
|
||||
# Handle commodity v1
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v1 message...')
|
||||
echoLog(' - Converting to v2...')
|
||||
|
||||
__temp = {}
|
||||
__temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else '')
|
||||
__temp['header'] = __json['header']
|
||||
|
||||
__temp['message'] = {}
|
||||
__temp['message']['systemName'] = __json['message']['systemName']
|
||||
__temp['message']['stationName'] = __json['message']['stationName']
|
||||
__temp['message']['timestamp'] = __json['message']['timestamp']
|
||||
|
||||
__temp['message']['commodities'] = []
|
||||
|
||||
__commodity = {}
|
||||
|
||||
if 'itemName' in __json['message']:
|
||||
__commodity['name'] = __json['message']['itemName']
|
||||
|
||||
if 'buyPrice' in __json['message']:
|
||||
__commodity['buyPrice'] = __json['message']['buyPrice']
|
||||
if 'stationStock' in __json['message']:
|
||||
__commodity['supply'] = __json['message']['stationStock']
|
||||
if 'supplyLevel' in __json['message']:
|
||||
__commodity['supplyLevel'] = __json['message']['supplyLevel']
|
||||
|
||||
if 'sellPrice' in __json['message']:
|
||||
__commodity['sellPrice'] = __json['message']['sellPrice']
|
||||
if 'demand' in __json['message']:
|
||||
__commodity['demand'] = __json['message']['demand']
|
||||
if'demandLevel' in __json['message']:
|
||||
__commodity['demandLevel'] = __json['message']['demandLevel']
|
||||
|
||||
__temp['message']['commodities'].append(__commodity)
|
||||
__json = __temp
|
||||
del __temp, __commodity
|
||||
|
||||
__converted = True
|
||||
|
||||
# Handle commodity v2
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else ''):
|
||||
if __converted == False:
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v2 message...')
|
||||
|
||||
__authorised = False
|
||||
__excluded = False
|
||||
|
||||
if __json['header']['softwareName'] in __authorisedSoftwares:
|
||||
__authorised = True
|
||||
if __json['header']['softwareName'] in __excludedSoftwares:
|
||||
__excluded = True
|
||||
|
||||
echoLog(' - Software: ' + __json['header']['softwareName'] + ' / ' + __json['header']['softwareVersion'])
|
||||
echoLog(' - ' + 'AUTHORISED' if (__authorised == True) else
|
||||
('EXCLUDED' if (__excluded == True) else 'UNAUTHORISED')
|
||||
)
|
||||
|
||||
if __authorised == True and __excluded == False:
|
||||
# Do what you want with the data...
|
||||
# Have fun !
|
||||
|
||||
# For example
|
||||
echoLog(' - Timestamp: ' + __json['message']['timestamp'])
|
||||
echoLog(' - Uploader ID: ' + __json['header']['uploaderID'])
|
||||
echoLog(' - System Name: ' + __json['message']['systemName'])
|
||||
echoLog(' - Station Name: ' + __json['message']['stationName'])
|
||||
|
||||
for __commodity in __json['message']['commodities']:
|
||||
echoLog(' - Name: ' + __commodity['name'])
|
||||
echoLog(' - Buy Price: ' + str(__commodity['buyPrice']))
|
||||
echoLog(' - Supply: ' + str(__commodity['supply'])
|
||||
+ ((' (' + __commodity['supplyLevel'] + ')') if 'supplyLevel' in __commodity else '')
|
||||
)
|
||||
echoLog(' - Sell Price: ' + str(__commodity['sellPrice']))
|
||||
echoLog(' - Demand: ' + str(__commodity['demand'])
|
||||
+ ((' (' + __commodity['demandLevel'] + ')') if 'demandLevel' in __commodity else '')
|
||||
)
|
||||
# End example
|
||||
|
||||
del __authorised, __excluded
|
||||
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
del __converted
|
||||
|
||||
|
||||
except zmq.ZMQError as e:
|
||||
echoLog('')
|
||||
echoLog('ZMQSocketException: ' + str(e))
|
||||
echoLog('')
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
3
examples/Python 3.4/Client_Simple.bat
Normal file
3
examples/Python 3.4/Client_Simple.bat
Normal file
@ -0,0 +1,3 @@
|
||||
"C:\Python34\python.exe" "%~dp0\Client_Simple.py"
|
||||
|
||||
pause
|
50
examples/Python 3.4/Client_Simple.py
Normal file
50
examples/Python 3.4/Client_Simple.py
Normal file
@ -0,0 +1,50 @@
|
||||
import zlib
|
||||
import zmq
|
||||
import simplejson
|
||||
import sys
|
||||
|
||||
"""
|
||||
" Configuration
|
||||
"""
|
||||
__relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500'
|
||||
__timeoutEDDN = 600000
|
||||
|
||||
|
||||
|
||||
"""
|
||||
" Start
|
||||
"""
|
||||
def main():
|
||||
context = zmq.Context()
|
||||
subscriber = context.socket(zmq.SUB)
|
||||
|
||||
subscriber.setsockopt(zmq.SUBSCRIBE, b"")
|
||||
subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN)
|
||||
|
||||
while True:
|
||||
try:
|
||||
subscriber.connect(__relayEDDN)
|
||||
|
||||
while True:
|
||||
__message = subscriber.recv()
|
||||
|
||||
if __message == False:
|
||||
subscriber.disconnect(__relayEDDN)
|
||||
break
|
||||
|
||||
__message = zlib.decompress(__message)
|
||||
__json = simplejson.loads(__message)
|
||||
|
||||
|
||||
print __json
|
||||
sys.stdout.flush()
|
||||
|
||||
except zmq.ZMQError, e:
|
||||
print 'ZMQSocketException: ' + str(e)
|
||||
sys.stdout.flush()
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,3 +0,0 @@
|
||||
"python" %~dp0\Client_Complete.py
|
||||
|
||||
pause
|
@ -1,3 +0,0 @@
|
||||
"python" %~dp0\Client_Simple.py
|
||||
|
||||
pause
|
Loading…
x
Reference in New Issue
Block a user