1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-08 19:32:15 +03:00

Merge branch 'outfitting'

This commit is contained in:
Jonathan Harris 2015-07-01 13:44:23 +01:00
commit be65aa6f75
2 changed files with 87 additions and 36 deletions

View File

@ -171,13 +171,14 @@ class AppWindow:
else: else:
return self.getandsend() # try again return self.getandsend() # try again
def getandsend(self, event=None): def getandsend(self, event=None, retrying=False):
if time() < self.holdofftime: return # Was invoked by Return key while in cooldown
self.cmdr['text'] = self.system['text'] = self.station['text'] = '' if not retrying:
self.status['text'] = 'Fetching market data...' if time() < self.holdofftime: return # Was invoked by Return key while in cooldown
self.button['state'] = tk.DISABLED self.cmdr['text'] = self.system['text'] = self.station['text'] = ''
self.w.update_idletasks() self.status['text'] = 'Fetching station data...'
self.button['state'] = tk.DISABLED
self.w.update_idletasks()
try: try:
querytime = int(time()) querytime = int(time())
@ -198,7 +199,13 @@ class AppWindow:
self.status['text'] = "Where are you?!" # Shouldn't happen self.status['text'] = "Where are you?!" # Shouldn't happen
elif not data.get('ship') or not data['ship'].get('modules') or not data['ship'].get('name','').strip(): elif not data.get('ship') or not data['ship'].get('modules') or not data['ship'].get('name','').strip():
self.status['text'] = "What are you flying?!" # Shouldn't happen self.status['text'] = "What are you flying?!" # Shouldn't happen
elif (config.getint('output') & config.OUT_EDDN) and not data['lastStarport'].get('ships') and not retrying:
# API is flakey about shipyard info - retry if missing (<1s is usually sufficient - 2.5s for margin).
self.w.after(2500, lambda:self.getandsend(retrying=True))
return
else: else:
if __debug__ and retrying: print data['lastStarport'].get('ships') and 'Retry for shipyard - Success' or 'Retry for shipyard - Fail'
# stuff we can do when not docked # stuff we can do when not docked
if __debug__: # Recording if __debug__: # Recording
with open('%s%s.%s.json' % (data['lastSystem']['name'], data['commander'].get('docked') and '.'+data['lastStarport']['name'] or '', strftime('%Y-%m-%dT%H.%M.%S', localtime())), 'wt') as h: with open('%s%s.%s.json' % (data['lastSystem']['name'], data['commander'].get('docked') and '.'+data['lastStarport']['name'] or '', strftime('%Y-%m-%dT%H.%M.%S', localtime())), 'wt') as h:
@ -214,23 +221,30 @@ class AppWindow:
elif not data['commander'].get('docked'): elif not data['commander'].get('docked'):
self.status['text'] = "You're not docked at a station!" self.status['text'] = "You're not docked at a station!"
elif not data['lastStarport'].get('commodities'):
self.status['text'] = "Station doesn't have a market!"
else: else:
# Fixup anomalies in the commodity data if data['lastStarport'].get('commodities'):
self.session.fixup(data['lastStarport']['commodities']) # Fixup anomalies in the commodity data
self.session.fixup(data['lastStarport']['commodities'])
if config.getint('output') & config.OUT_CSV:
bpc.export(data, True)
if config.getint('output') & config.OUT_TD:
td.export(data)
if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_CSV:
bpc.export(data, True)
if config.getint('output') & config.OUT_TD:
td.export(data)
if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_EDDN: if config.getint('output') & config.OUT_EDDN:
self.status['text'] = 'Sending data to EDDN...' if data['lastStarport'].get('commodities') or data['lastStarport'].get('modules') or data['lastStarport'].get('ships'):
self.w.update_idletasks() self.status['text'] = 'Sending data to EDDN...'
eddn.export(data) self.w.update_idletasks()
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime)) eddn.export(data)
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
else:
self.status['text'] = "Station doesn't have anything!"
elif not data['lastStarport'].get('commodities'):
self.status['text'] = "Station doesn't have a market!"
else:
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
except companion.VerificationRequired: except companion.VerificationRequired:
return prefs.AuthenticationDialog(self.w, self.verify) return prefs.AuthenticationDialog(self.w, self.verify)

69
eddn.py
View File

@ -9,9 +9,10 @@ from sys import platform
import time import time
from config import applongname, appversion, config from config import applongname, appversion, config
from companion import ship_map
import outfitting
upload = 'http://eddn-gateway.elite-markets.net:8080/upload/' upload = 'http://eddn-gateway.elite-markets.net:8080/upload/'
schema = 'http://schemas.elite-markets.net/eddn/commodity/2'
timeout= 10 # requests timeout timeout= 10 # requests timeout
@ -38,27 +39,63 @@ def export(data):
'uploaderID' : config.getint('anonymous') and hashlib.md5(data['commander']['name'].strip().encode('utf-8')).hexdigest() or data['commander']['name'].strip(), 'uploaderID' : config.getint('anonymous') and hashlib.md5(data['commander']['name'].strip().encode('utf-8')).hexdigest() or data['commander']['name'].strip(),
} }
commodities = [] # Don't send empty commodities list - schema won't allow it
for commodity in data['lastStarport']['commodities']: if data['lastStarport'].get('commodities'):
commodities.append({ commodities = []
'name' : commodity['name'], for commodity in data['lastStarport'].get('commodities', []):
'buyPrice' : commodity['buyPrice'], commodities.append({
'supply' : int(commodity['stock']), 'name' : commodity['name'],
'sellPrice' : commodity['sellPrice'], 'buyPrice' : commodity['buyPrice'],
'demand' : int(commodity['demand']), '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' : 'http://schemas.elite-markets.net/eddn/commodity/2',
'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,
}
}) })
if commodity['stockBracket']:
commodities[-1]['supplyLevel'] = bracketmap[commodity['stockBracket']]
if commodity['demandBracket']:
commodities[-1]['demandLevel'] = bracketmap[commodity['demandBracket']]
# *Do* send empty modules list - implies station has no outfitting
modules = []
for v in data['lastStarport'].get('modules', {}).itervalues():
try:
module = outfitting.lookup(v)
if module:
modules.append(module)
except AssertionError as e:
if __debug__: print 'Outfitting: %s' % e # Silently skip unrecognized modules
except:
if __debug__: raise
send({ send({
'$schemaRef' : schema, '$schemaRef' : 'http://schemas.elite-markets.net/eddn/outfitting/1',
'header' : header, 'header' : header,
'message' : { 'message' : {
'systemName' : data['lastSystem']['name'].strip(), 'systemName' : data['lastSystem']['name'].strip(),
'stationName' : data['lastStarport']['name'].strip(), 'stationName' : data['lastStarport']['name'].strip(),
'timestamp' : time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime)), 'timestamp' : time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime)),
'commodities' : commodities, 'modules' : modules,
} }
}) })
# Don't send empty ships list - shipyard data is only guaranteed present if user has visited the shipyard.
if data['lastStarport'].get('ships'):
send({
'$schemaRef' : 'http://schemas.elite-markets.net/eddn/shipyard/1',
'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)),
'ships' : [ship_map.get(ship['name'],ship['name']) for ship in data['lastStarport']['ships'].get('shipyard_list', {}).values() + data['lastStarport']['ships'].get('unavailable_list', [])],
}
})