Skip to content

Commit

Permalink
Use version 2 of the EDDN schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marginal committed Jun 18, 2015
1 parent 34481c2 commit 98281ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
2 changes: 1 addition & 1 deletion EDMarketConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def getandsend(self, event=None):
if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_EDDN:
eddn.export(data, self.setstatus)
eddn.export(data)
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))

except companion.VerificationRequired:
Expand Down
80 changes: 36 additions & 44 deletions eddn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Export to EDDN
# -*- coding: utf-8 -*-

import hashlib
import json
Expand All @@ -12,59 +11,52 @@
from config import applongname, appversion, config

upload = 'http://eddn-gateway.elite-markets.net:8080/upload/'
schema = 'http://schemas.elite-markets.net/eddn/commodity/1'
schema = 'http://schemas.elite-markets.net/eddn/commodity/2'

bracketmap = { 1: 'Low',
2: 'Med',
3: 'High', }

def export(data, callback):
def export(data):

callback('Sending data to EDDN...')

querytime = config.getint('querytime') or int(time.time())

header = { 'softwareName': '%s [%s]' % (applongname, platform=='darwin' and "Mac OS" or system()),
'softwareVersion': appversion,
'uploaderID': config.getint('anonymous') and hashlib.md5(data['commander']['name'].strip().encode('utf-8')).hexdigest() or data['commander']['name'].strip(),
}
systemName = data['lastSystem']['name'].strip()
stationName = data['lastStarport']['name'].strip()
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime))

# route all requests through a session in the hope of using keep-alive
session = requests.Session()
session.headers['connection'] = 'keep-alive' # can help through a proxy?

commodities = data['lastStarport']['commodities']
i=0
for commodity in commodities:
i = i+1
callback('Sending %d/%d' % (i, len(commodities)))
data = { '$schemaRef': schema,
'header': header,
'message': {
'systemName': systemName,
'stationName': stationName,
'itemName': commodity['name'],
'buyPrice': commodity['buyPrice'],
'stationStock': int(commodity['stock']),
'sellPrice': commodity['sellPrice'],
'demand': int(commodity['demand']),
'timestamp': timestamp,
}
}
if commodity['stockBracket']:
data['message']['supplyLevel'] = bracketmap[commodity['stockBracket']]
if commodity['demandBracket']:
data['message']['demandLevel'] = bracketmap[commodity['demandBracket']]

r = session.post(upload, data=json.dumps(data))
def send(msg):
r = requests.post(upload, data=json.dumps(msg))
if __debug__ and r.status_code != requests.codes.ok:
print 'Status\t%s' % r.status_code
print 'URL\t%s' % r.url
print 'Headers\t%s' % r.headers
print ('Content:\n%s' % r.text).encode('utf-8')
r.raise_for_status()

session.close()
querytime = config.getint('querytime') or int(time.time())

header = {
'softwareName' : '%s [%s]' % (applongname, platform=='darwin' and "Mac OS" or system()),
'softwareVersion' : appversion,
'uploaderID' : config.getint('anonymous') and hashlib.md5(data['commander']['name'].strip().encode('utf-8')).hexdigest() or data['commander']['name'].strip(),
}

commodities = []
for commodity in data['lastStarport']['commodities']:
commodities.append({
'name' : commodity['name'],
'buyPrice' : commodity['buyPrice'],
'supply' : int(commodity['stock']),
'sellPrice' : commodity['sellPrice'],
'demand' : int(commodity['demand']),
})
if commodity['stockBracket']:
commodities[-1]['supplyLevel'] = bracketmap[commodity['stockBracket']]
if commodity['demandBracket']:
commodities[-1]['demandLevel'] = bracketmap[commodity['demandBracket']]

send({
'$schemaRef' : schema,
'header' : header,
'message' : {
'systemName' : data['lastSystem']['name'].strip(),
'stationName' : data['lastStarport']['name'].strip(),
'timestamp' : time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime)),
'commodities' : commodities,
}
})

0 comments on commit 98281ce

Please sign in to comment.