diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 25845cb1..be0167b5 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -499,18 +499,45 @@ class AppWindow: if system_changed or station_changed: self.status['text'] = '' - if config.getint('output') & config.OUT_SYS_EDSM and not monitor.is_beta and monitor.ranks and (not entry or entry['event'] in ['Progress', 'Promotion']): + if config.getint('output') & config.OUT_SYS_EDSM and not monitor.is_beta: + # Send credits to EDSM on startup + if monitor.credits and (not entry or entry['event'] == 'LoadGame'): + try: + self.status['text'] = _('Sending balance/loan to EDSM...') + self.w.update_idletasks() + self.edsm.setcredits(monitor.credits) + self.status['text'] = '' + except Exception as e: + if __debug__: print_exc() + self.status['text'] = unicode(e) + if not config.getint('hotkey_mute'): + hotkeymgr.play_bad() + + # Send shipid to EDSM on startup or change + if monitor.shipid and (not entry or entry['event'] in ['LoadGame', 'ShipyardSwap']): + try: + self.status['text'] = _('Sending shipId to EDSM...') + self.w.update_idletasks() + self.edsm.setshipid(monitor.shipid) + self.status['text'] = '' + except Exception as e: + if __debug__: print_exc() + self.status['text'] = unicode(e) + if not config.getint('hotkey_mute'): + hotkeymgr.play_bad() + # Send rank info to EDSM on startup or change - try: - self.status['text'] = _('Sending data to EDSM...') - self.w.update_idletasks() - self.edsm.setranks(monitor.ranks) - self.status['text'] = '' - except Exception as e: - if __debug__: print_exc() - self.status['text'] = unicode(e) - if not config.getint('hotkey_mute'): - hotkeymgr.play_bad() + if monitor.ranks and (not entry or entry['event'] in ['Progress', 'Promotion']): + try: + self.status['text'] = _('Sending ranks to EDSM...') + self.w.update_idletasks() + self.edsm.setranks(monitor.ranks) + self.status['text'] = '' + except Exception as e: + if __debug__: print_exc() + self.status['text'] = unicode(e) + if not config.getint('hotkey_mute'): + hotkeymgr.play_bad() if not entry or not monitor.mode: return # Fake event or in CQC @@ -527,7 +554,7 @@ class AppWindow: # Update EDSM if we have coordinates - i.e. Location or FSDJump events if config.getint('output') & config.OUT_SYS_EDSM and monitor.coordinates: try: - self.status['text'] = _('Sending data to EDSM...') + self.status['text'] = _('Sending flight log to EDSM...') self.w.update_idletasks() self.edsm.writelog(timestamp, monitor.system, monitor.coordinates) self.status['text'] = '' diff --git a/edsm.py b/edsm.py index ede7087a..311cc305 100644 --- a/edsm.py +++ b/edsm.py @@ -167,3 +167,52 @@ class EDSM: if msgnum // 100 not in (1,4): raise Exception(_('Error: EDSM {MSG}').format(MSG=msg)) + + def setcredits(self, credits): + if not credits: + return + + try: + url = 'https://www.edsm.net/api-commander-v1/set-credits?commanderName=%s&apiKey=%s&fromSoftware=%s&fromSoftwareVersion=%s' % ( + urllib2.quote(config.get('edsm_cmdrname').encode('utf-8')), + urllib2.quote(config.get('edsm_apikey')), + urllib2.quote(applongname), + urllib2.quote(appversion) + ) + + url += '&%s=%s' % ('balance', urllib2.quote('%d;%d' % credits['balance'])) + url += '&%s=%s' % ('loan', urllib2.quote('%d;%d' % credits['loan'])) + + r = self.opener.open(url, timeout=EDSM._TIMEOUT) + reply = json.loads(r.read()) + (msgnum, msg) = reply['msgnum'], reply['msg'] + except: + if __debug__: print_exc() + raise Exception(_("Error: Can't connect to EDSM")) + + if msgnum // 100 not in (1,4): + raise Exception(_('Error: EDSM {MSG}').format(MSG=msg)) + + def setshipid(self, shipid): + if not shipid: + return + + try: + url = 'https://www.edsm.net/api-commander-v1/set-ship-id?commanderName=%s&apiKey=%s&fromSoftware=%s&fromSoftwareVersion=%s' % ( + urllib2.quote(config.get('edsm_cmdrname').encode('utf-8')), + urllib2.quote(config.get('edsm_apikey')), + urllib2.quote(applongname), + urllib2.quote(appversion) + ) + + url += '&%s=%s' % ('shipId', urllib2.quote('%d;%d' % shipid)) + + r = self.opener.open(url, timeout=EDSM._TIMEOUT) + reply = json.loads(r.read()) + (msgnum, msg) = reply['msgnum'], reply['msg'] + except: + if __debug__: print_exc() + raise Exception(_("Error: Can't connect to EDSM")) + + if msgnum // 100 not in (1,4): + raise Exception(_('Error: EDSM {MSG}').format(MSG=msg)) diff --git a/monitor.py b/monitor.py index affade34..1be69a13 100644 --- a/monitor.py +++ b/monitor.py @@ -77,10 +77,12 @@ class EDLogs(FileSystemEventHandler): self.is_beta = False self.mode = None self.cmdr = None + self.shipid = None self.system = None self.station = None self.coordinates = None self.ranks = None + self.credits = None def set_callback(self, name, callback): if name in self.callbacks: @@ -218,10 +220,14 @@ class EDLogs(FileSystemEventHandler): self.ranks = None elif entry['event'] == 'LoadGame': self.cmdr = entry['Commander'] + self.shipid = entry['ShipID'] self.mode = entry.get('GameMode') # 'Open', 'Solo', 'Group', or None for CQC self.ranks = { "Combat": None, "Trade": None, "Explore": None, "Empire": None, "Federation": None, "CQC": None } + self.credits = { "balance": entry['Credits'], "loan": entry['Loan'] } elif entry['event'] == 'NewCommander': self.cmdr = entry['Name'] + elif entry['event'] in ['ShipyardSwap']: + self.shipid = entry['ShipID'] elif entry['event'] in ['Undocked']: self.station = None elif entry['event'] in ['Location', 'FSDJump', 'Docked']: