1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-09 11:52:27 +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,11 +171,12 @@ 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
if not retrying:
if time() < self.holdofftime: return # Was invoked by Return key while in cooldown
self.cmdr['text'] = self.system['text'] = self.station['text'] = '' self.cmdr['text'] = self.system['text'] = self.station['text'] = ''
self.status['text'] = 'Fetching market data...' self.status['text'] = 'Fetching station data...'
self.button['state'] = tk.DISABLED self.button['state'] = tk.DISABLED
self.w.update_idletasks() self.w.update_idletasks()
@ -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,9 +221,8 @@ 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:
if data['lastStarport'].get('commodities'):
# Fixup anomalies in the commodity data # Fixup anomalies in the commodity data
self.session.fixup(data['lastStarport']['commodities']) self.session.fixup(data['lastStarport']['commodities'])
@ -226,11 +232,19 @@ class AppWindow:
td.export(data) td.export(data)
if config.getint('output') & config.OUT_BPC: if config.getint('output') & config.OUT_BPC:
bpc.export(data, False) bpc.export(data, False)
if config.getint('output') & config.OUT_EDDN: if config.getint('output') & config.OUT_EDDN:
if data['lastStarport'].get('commodities') or data['lastStarport'].get('modules') or data['lastStarport'].get('ships'):
self.status['text'] = 'Sending data to EDDN...' self.status['text'] = 'Sending data to EDDN...'
self.w.update_idletasks() self.w.update_idletasks()
eddn.export(data) eddn.export(data)
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime)) 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)

45
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,8 +39,10 @@ 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(),
} }
# Don't send empty commodities list - schema won't allow it
if data['lastStarport'].get('commodities'):
commodities = [] commodities = []
for commodity in data['lastStarport']['commodities']: for commodity in data['lastStarport'].get('commodities', []):
commodities.append({ commodities.append({
'name' : commodity['name'], 'name' : commodity['name'],
'buyPrice' : commodity['buyPrice'], 'buyPrice' : commodity['buyPrice'],
@ -51,9 +54,8 @@ def export(data):
commodities[-1]['supplyLevel'] = bracketmap[commodity['stockBracket']] commodities[-1]['supplyLevel'] = bracketmap[commodity['stockBracket']]
if commodity['demandBracket']: if commodity['demandBracket']:
commodities[-1]['demandLevel'] = bracketmap[commodity['demandBracket']] commodities[-1]['demandLevel'] = bracketmap[commodity['demandBracket']]
send({ send({
'$schemaRef' : schema, '$schemaRef' : 'http://schemas.elite-markets.net/eddn/commodity/2',
'header' : header, 'header' : header,
'message' : { 'message' : {
'systemName' : data['lastSystem']['name'].strip(), 'systemName' : data['lastSystem']['name'].strip(),
@ -62,3 +64,38 @@ def export(data):
'commodities' : commodities, 'commodities' : commodities,
} }
}) })
# *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({
'$schemaRef' : 'http://schemas.elite-markets.net/eddn/outfitting/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)),
'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', [])],
}
})