From f57deae3f3c54464c0eb3bc5cbb3f5cd3507a187 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Fri, 19 Jun 2015 00:32:08 +0100 Subject: [PATCH 1/2] Send station Shipyard & Outfitting data to EDDN. --- EDMarketConnector.py | 48 +++++++++++++++++++++-------------- companion.py | 7 +++++- eddn.py | 59 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 84 insertions(+), 30 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index a9552282..02a9e096 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -166,13 +166,14 @@ class AppWindow: else: return self.getandsend() # try again - def getandsend(self, event=None): - if time() < self.holdofftime: return # Was invoked by Return key while in cooldown + def getandsend(self, event=None, retrying=False): - self.cmdr['text'] = self.system['text'] = self.station['text'] = '' - self.status['text'] = 'Fetching market data...' - self.button['state'] = tk.DISABLED - self.w.update_idletasks() + 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.status['text'] = 'Fetching station data...' + self.button['state'] = tk.DISABLED + self.w.update_idletasks() try: querytime = int(time()) @@ -193,6 +194,10 @@ class AppWindow: 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(): 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 + self.w.after(2000, lambda:self.getandsend(retrying=True)) + return else: # stuff we can do when not docked if config.getint('output') & config.OUT_LOG: @@ -206,20 +211,27 @@ class AppWindow: elif not data['commander'].get('docked'): 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: - 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 data['lastStarport'].get('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_EDDN: - self.status['text'] = 'Sending data to EDDN...' - self.w.update_idletasks() - eddn.export(data) - self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime)) + if data['lastStarport'].get('commodities') or data['lastStarport'].get('modules') or data['lastStarport'].get('ships'): + self.status['text'] = 'Sending data to EDDN...' + self.w.update_idletasks() + 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: return prefs.AuthenticationDialog(self.w, self.verify) diff --git a/companion.py b/companion.py index 8f01fcb6..bcf3655e 100644 --- a/companion.py +++ b/companion.py @@ -180,7 +180,7 @@ class Session: self.session = None - # Fixup anomalies in the recieved commodity data + # Fixup anomalies in the recieved commodity and shipyard data def fixup(self, data): commodities = data.get('lastStarport') and data['lastStarport'].get('commodities') or [] i=0 @@ -225,6 +225,11 @@ class Session: # Skip the commodity commodities.pop(i) + if data['lastStarport'].get('ships'): + for ship in data['lastStarport']['ships'].get('shipyard_list', {}).values() + data['lastStarport']['ships'].get('unavailable_list', []): + if ship_map.get(ship['name']): + ship['name'] = ship_map[ship['name']] + return data def dump(self, r): diff --git a/eddn.py b/eddn.py index 6c8966d8..f9171926 100644 --- a/eddn.py +++ b/eddn.py @@ -9,9 +9,9 @@ from sys import platform import time from config import applongname, appversion, config +import outfitting upload = 'http://eddn-gateway.elite-markets.net:8080/upload/' -schema = 'http://schemas.elite-markets.net/eddn/commodity/2' bracketmap = { 1: 'Low', 2: 'Med', @@ -37,7 +37,7 @@ def export(data): } commodities = [] - for commodity in data['lastStarport']['commodities']: + for commodity in data['lastStarport'].get('commodities', []): commodities.append({ 'name' : commodity['name'], 'buyPrice' : commodity['buyPrice'], @@ -50,13 +50,50 @@ def export(data): 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, + if data['lastStarport'].get('commodities'): + 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, } - }) + }) + + 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 + + if data['lastStarport'].get('modules'): + 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, + } + }) + + # Shipyard data is only guaranteed present if user has visited the shipyard. Only send if present. + 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['name'] for ship in data['lastStarport']['ships'].get('shipyard_list', {}).values() + data['lastStarport']['ships'].get('unavailable_list', [])], + } + }) From 9dc79f679ce85f3c817a37b9ee3d788cda06e650 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Sat, 20 Jun 2015 12:11:30 +0100 Subject: [PATCH 2/2] In interactive mode notify when retrying for shipyard. --- EDMarketConnector.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 02a9e096..38665c25 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -199,6 +199,8 @@ class AppWindow: self.w.after(2000, lambda:self.getandsend(retrying=True)) return 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 if config.getint('output') & config.OUT_LOG: flightlog.export(data)