From efc1cb33b688a5f8471b9f575048b6f258387161 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Fri, 16 Jun 2017 18:22:00 +0100 Subject: [PATCH] Use new Beta universe Companion API server. --- EDMC.py | 4 ++-- EDMarketConnector.py | 37 ++++++++++++++++++------------------- companion.py | 30 +++++++++++++++++------------- coriolis.py | 4 ++-- eddn.py | 12 ++++++------ edshipyard.py | 4 ++-- prefs.py | 18 +++++++++--------- stats.py | 4 ++-- 8 files changed, 58 insertions(+), 55 deletions(-) diff --git a/EDMC.py b/EDMC.py index 126e1b5a..06377fe6 100755 --- a/EDMC.py +++ b/EDMC.py @@ -84,9 +84,9 @@ try: session = companion.Session() if config.get('cmdrs'): username = config.get('fdev_usernames')[0] - session.login(username, config.get_password(username)) + session.login(username, config.get_password(username), False) else: # <= 2.25 not yet migrated - session.login(config.get('username'), config.get('password')) + session.login(config.get('username'), config.get('password'), False) querytime = int(time()) data = session.query() config.set('querytime', querytime) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 8ce8eb56..b9c84705 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -301,7 +301,7 @@ class AppWindow: # Migration from <= 2.25 if not config.get('cmdrs') and config.get('username') and config.get('password'): try: - self.session.login(config.get('username'), config.get('password')) + self.session.login(config.get('username'), config.get('password'), False) data = self.session.query() prefs.migrate(data['commander']['name']) except: @@ -374,7 +374,7 @@ class AppWindow: raise companion.CredentialsError() idx = config.get('cmdrs').index(monitor.cmdr) username = config.get('fdev_usernames')[idx] - self.session.login(username, config.get_password(username)) + self.session.login(username, config.get_password(username), monitor.is_beta) self.status['text'] = '' except companion.VerificationRequired: return prefs.AuthenticationDialog(self.w, partial(self.verify, self.login)) @@ -403,8 +403,8 @@ class AppWindow: play_sound = (auto_update or int(event.type) == self.EVENT_VIRTUAL) and not config.getint('hotkey_mute') play_bad = False - if not monitor.cmdr or not monitor.mode or monitor.is_beta or monitor.captain: - return # In CQC, Beta or on crew - do nothing + if not monitor.cmdr or not monitor.mode or monitor.captain: + return # In CQC or on crew - do nothing if auto_update and monitor.carrying_rares(): # https://github.com/Marginal/EDMarketConnector/issues/92 @@ -522,18 +522,18 @@ class AppWindow: if not old_status: self.status['text'] = _('Sending data to EDDN...') self.w.update_idletasks() - self.eddn.export_commodities(data) - self.eddn.export_outfitting(data) + self.eddn.export_commodities(data, monitor.is_beta) + self.eddn.export_outfitting(data, monitor.is_beta) if has_shipyard and not data['lastStarport'].get('ships'): # API is flakey about shipyard info - silently retry if missing (<1s is usually sufficient - 5s for margin). self.w.after(int(SERVER_RETRY * 1000), self.retry_for_shipyard) else: - self.eddn.export_shipyard(data) + self.eddn.export_shipyard(data, monitor.is_beta) if not old_status: self.status['text'] = '' # Update credits and ship info and send to EDSM - if config.getint('output') & config.OUT_SYS_EDSM: + if not monitor.is_beta and config.getint('output') & config.OUT_SYS_EDSM: try: if data['commander'].get('credits') is not None: monitor.state['Credits'] = data['commander']['credits'] @@ -544,8 +544,8 @@ class AppWindow: self.edsm.updateship(monitor.state['ShipID'], monitor.state['ShipType'], [ - ('linkToCoriolis', coriolis.url(data)), - ('linkToEDShipyard', edshipyard.url(data)), + ('linkToCoriolis', coriolis.url(data, monitor.is_beta)), + ('linkToEDShipyard', edshipyard.url(data, monitor.is_beta)), ]) self.edsm.lastship = ship except Exception as e: @@ -591,7 +591,7 @@ class AppWindow: if __debug__: print 'Retry for shipyard - ' + (data['commander'].get('docked') and (data['lastStarport'].get('ships') and 'Success' or 'Failure') or 'Undocked!') if data['commander'].get('docked'): # might have undocked while we were waiting for retry in which case station data is unreliable - self.eddn.export_shipyard(data) + self.eddn.export_shipyard(data, monitor.is_beta) except: pass @@ -624,8 +624,7 @@ class AppWindow: else: self.cmdr['text'] = monitor.cmdr and monitor.group and ('%s / %s' % (monitor.cmdr, monitor.group)) or monitor.cmdr or '' self.ship_label['text'] = _('Ship') + ':' # Main window - self.ship.configure(state = monitor.is_beta and tk.DISABLED or tk.NORMAL, - text = monitor.state['ShipName'] or companion.ship_map.get(monitor.state['ShipType'], monitor.state['ShipType']) or '', + self.ship.configure(text = monitor.state['ShipName'] or companion.ship_map.get(monitor.state['ShipType'], monitor.state['ShipType']) or '', url = self.shipyard_url) self.station['text'] = monitor.station or (EDDB.system(monitor.system) and self.STATION_UNDOCKED or '') @@ -686,7 +685,7 @@ class AppWindow: self.edsmpoll() # Companion login - do this after EDSM so any EDSM errors don't mask login errors - if entry['event'] in [None, 'StartUp', 'NewCommander', 'LoadGame'] and monitor.cmdr and not monitor.is_beta: + if entry['event'] in [None, 'StartUp', 'NewCommander', 'LoadGame'] and monitor.cmdr: if config.get('cmdrs') and monitor.cmdr in config.get('cmdrs'): prefs.make_current(monitor.cmdr) self.login() @@ -712,7 +711,7 @@ class AppWindow: plug.notify_system_changed(timegm(strptime(entry['timestamp'], '%Y-%m-%dT%H:%M:%SZ')), monitor.system, monitor.coordinates) # Auto-Update after docking - if station_changed and monitor.mode and not monitor.is_beta and not config.getint('output') & config.OUT_MKT_MANUAL and config.getint('output') & config.OUT_STATION_ANY: + if station_changed and monitor.mode and not config.getint('output') & config.OUT_MKT_MANUAL and config.getint('output') & config.OUT_STATION_ANY: self.w.after(int(SERVER_RETRY * 1000), self.getandsend) # Send interesting events to EDDN @@ -779,8 +778,8 @@ class AppWindow: def shipyard_url(self, shipname=None): - if not monitor.cmdr or not monitor.mode or monitor.is_beta: - return False # In CQC or Beta - do nothing + if not monitor.cmdr or not monitor.mode: + return False # In CQC - do nothing self.status['text'] = _('Fetching data...') self.w.update_idletasks() @@ -807,9 +806,9 @@ class AppWindow: else: self.status['text'] = '' if config.getint('shipyard') == config.SHIPYARD_EDSHIPYARD: - return edshipyard.url(data) + return edshipyard.url(data, monitor.is_beta) elif config.getint('shipyard') == config.SHIPYARD_CORIOLIS: - return coriolis.url(data) + return coriolis.url(data, monitor.is_beta) else: assert False, config.getint('shipyard') return False diff --git a/companion.py b/companion.py index 14dde1f8..b596d454 100644 --- a/companion.py +++ b/companion.py @@ -17,9 +17,11 @@ from config import config holdoff = 60 # be nice timeout = 10 # requests timeout -URL_LOGIN = 'https://companion.orerve.net/user/login' -URL_CONFIRM = 'https://companion.orerve.net/user/confirm' -URL_QUERY = 'https://companion.orerve.net/profile' +SERVER_LIVE = 'https://companion.orerve.net' +SERVER_BETA = 'https://pts-companion.orerve.net' +URL_LOGIN = '/user/login' +URL_CONFIRM = '/user/confirm' +URL_QUERY = '/profile' # Map values reported by the Companion interface to names displayed in-game @@ -189,19 +191,20 @@ class Session: if platform=='win32' and getattr(sys, 'frozen', False): os.environ['REQUESTS_CA_BUNDLE'] = join(dirname(sys.executable), 'cacert.pem') - def login(self, username=None, password=None): + def login(self, username, password, is_beta): if (not username or not password): if not self.credentials: raise CredentialsError() else: credentials = self.credentials else: - credentials = { 'email' : username, 'password' : password } + credentials = { 'email' : username, 'password' : password, 'beta' : is_beta } if self.credentials == credentials and self.state == Session.STATE_OK: return # already logged in - if not self.credentials or self.credentials['email'] != credentials['email']: # changed account + if not self.credentials or self.credentials['email'] != credentials['email'] or self.credentials['beta'] != credentials['beta']: + # changed account self.close() self.session = requests.Session() self.session.headers['User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257' @@ -214,10 +217,11 @@ class Session: except IOError: pass + self.server = credentials['beta'] and SERVER_BETA or SERVER_LIVE self.credentials = credentials self.state = Session.STATE_INIT try: - r = self.session.post(URL_LOGIN, data = self.credentials, timeout=timeout) + r = self.session.post(self.server + URL_LOGIN, data = self.credentials, timeout=timeout) except: if __debug__: print_exc() raise ServerError() @@ -225,10 +229,10 @@ class Session: if r.status_code != requests.codes.ok or 'server error' in r.text: self.dump(r) raise ServerError() - elif r.url == URL_LOGIN: # would have redirected away if success + elif r.url == self.server + URL_LOGIN: # would have redirected away if success self.dump(r) raise CredentialsError() - elif r.url == URL_CONFIRM: # redirected to verification page + elif r.url == self.server + URL_CONFIRM: # redirected to verification page self.state = Session.STATE_AUTH raise VerificationRequired() else: @@ -238,8 +242,8 @@ class Session: def verify(self, code): if not code: raise VerificationRequired() - r = self.session.post(URL_CONFIRM, data = {'code' : code}, timeout=timeout) - if r.status_code != requests.codes.ok or r.url == URL_CONFIRM: # would have redirected away if success + r = self.session.post(self.server + URL_CONFIRM, data = {'code' : code}, timeout=timeout) + if r.status_code != requests.codes.ok or r.url == self.server + URL_CONFIRM: # would have redirected away if success raise VerificationRequired() self.session.cookies.save() # Save cookies now for use by command-line app self.login() @@ -252,12 +256,12 @@ class Session: elif self.state == Session.STATE_AUTH: raise VerificationRequired() try: - r = self.session.get(URL_QUERY, timeout=timeout) + r = self.session.get(self.server + URL_QUERY, timeout=timeout) except: if __debug__: print_exc() raise ServerError() - if r.status_code == requests.codes.forbidden or r.url == URL_LOGIN: + if r.status_code == requests.codes.forbidden or r.url == self.server + URL_LOGIN: # Start again - maybe our session cookie expired? self.state = Session.STATE_INIT return self.query() diff --git a/coriolis.py b/coriolis.py index 7cc340ba..d419d054 100755 --- a/coriolis.py +++ b/coriolis.py @@ -239,14 +239,14 @@ def export(data, filename=None): # Return a URL for the current ship -def url(data): +def url(data, is_beta): string = json.dumps(companion.ship(data), ensure_ascii=False, sort_keys=True, separators=(',', ':')) # most compact representation out = StringIO.StringIO() with gzip.GzipFile(fileobj=out, mode='w') as f: f.write(string) - return 'https://coriolis.edcd.io/import?data=' + base64.urlsafe_b64encode(out.getvalue()).replace('=', '%3D') + return (is_beta and 'http://beta.coriolis.edcd.io/import?data=' or 'https://coriolis.edcd.io/import?data=') + base64.urlsafe_b64encode(out.getvalue()).replace('=', '%3D') # diff --git a/eddn.py b/eddn.py index 3dc9f1ae..68cdcd34 100644 --- a/eddn.py +++ b/eddn.py @@ -139,7 +139,7 @@ class EDDN: self.parent.w.after(self.REPLAYPERIOD, self.sendreplay) - def export_commodities(self, data): + def export_commodities(self, data, is_beta): commodities = [] for commodity in data['lastStarport'].get('commodities') or []: if category_map.get(commodity['categoryname'], True): # Check marketable @@ -159,7 +159,7 @@ class EDDN: # Don't send empty commodities list - schema won't allow it if commodities: self.send(data['commander']['name'], { - '$schemaRef' : 'http://schemas.elite-markets.net/eddn/commodity/3', + '$schemaRef' : 'http://schemas.elite-markets.net/eddn/commodity/3' + (is_beta and '/test' or ''), 'message' : { 'systemName' : data['lastSystem']['name'], 'stationName' : data['lastStarport']['name'], @@ -167,11 +167,11 @@ class EDDN: } }) - def export_outfitting(self, data): + def export_outfitting(self, data, is_beta): # Don't send empty modules list - schema won't allow it if data['lastStarport'].get('modules'): self.send(data['commander']['name'], { - '$schemaRef' : 'http://schemas.elite-markets.net/eddn/outfitting/2', + '$schemaRef' : 'http://schemas.elite-markets.net/eddn/outfitting/2' + (is_beta and '/test' or ''), 'message' : { 'systemName' : data['lastSystem']['name'], 'stationName' : data['lastStarport']['name'], @@ -179,11 +179,11 @@ class EDDN: } }) - def export_shipyard(self, data): + def export_shipyard(self, data, is_beta): # Don't send empty ships list - shipyard data is only guaranteed present if user has visited the shipyard. if data['lastStarport'].get('ships'): self.send(data['commander']['name'], { - '$schemaRef' : 'http://schemas.elite-markets.net/eddn/shipyard/2', + '$schemaRef' : 'http://schemas.elite-markets.net/eddn/shipyard/2' + (is_beta and '/test' or ''), 'message' : { 'systemName' : data['lastSystem']['name'], 'stationName' : data['lastStarport']['name'], diff --git a/edshipyard.py b/edshipyard.py index 4bc38af1..511405a6 100644 --- a/edshipyard.py +++ b/edshipyard.py @@ -152,11 +152,11 @@ def export(data, filename=None): # Return a URL for the current ship -def url(data): +def url(data, is_beta): string = json.dumps(companion.ship(data), ensure_ascii=False, sort_keys=True, separators=(',', ':')) # most compact representation out = StringIO.StringIO() with gzip.GzipFile(fileobj=out, mode='w') as f: f.write(string) - return 'http://www.edshipyard.com/#/I=' + base64.urlsafe_b64encode(out.getvalue()).replace('=', '%3D') + return (is_beta and 'http://www.edshipyard.com/beta/#/I=' or 'http://www.edshipyard.com/#/I=') + base64.urlsafe_b64encode(out.getvalue()).replace('=', '%3D') diff --git a/prefs.py b/prefs.py index 0ed15779..e2a52918 100644 --- a/prefs.py +++ b/prefs.py @@ -111,7 +111,7 @@ class PreferencesDialog(tk.Toplevel): self.cmdr_text.grid(row=10, column=1, padx=PADX, pady=PADY, sticky=tk.W) self.username = nb.Entry(credframe) self.username.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW) - if not monitor.is_beta and monitor.cmdr: + if monitor.cmdr: self.username.focus_set() self.password = nb.Entry(credframe, show=u'•') self.password.grid(row=12, column=1, padx=PADX, pady=PADY, sticky=tk.EW) @@ -400,7 +400,7 @@ class PreferencesDialog(tk.Toplevel): self.edsm_apikey.insert(0, config.get('edsm_apikey') or '') self.cmdr = monitor.cmdr - cmdr_state = not monitor.is_beta and monitor.cmdr and tk.NORMAL or tk.DISABLED + cmdr_state = monitor.cmdr and tk.NORMAL or tk.DISABLED self.cred_label['state'] = self.cmdr_label['state'] = self.username_label['state'] = self.password_label['state'] = cmdr_state self.cmdr_text['state'] = self.username['state'] = self.password['state'] = cmdr_state @@ -410,20 +410,20 @@ class PreferencesDialog(tk.Toplevel): logdir = self.logdir.get() logvalid = logdir and exists(logdir) - self.out_label['state'] = self.out_csv_button['state'] = self.out_td_button['state'] = self.out_ship_button['state'] = not monitor.is_beta and tk.NORMAL or tk.DISABLED - local = not monitor.is_beta and (self.out_td.get() or self.out_csv.get() or self.out_ship.get()) + self.out_label['state'] = self.out_csv_button['state'] = self.out_td_button['state'] = self.out_ship_button['state'] = tk.NORMAL or tk.DISABLED + local = self.out_td.get() or self.out_csv.get() or self.out_ship.get() self.out_auto_button['state'] = local and logvalid and tk.NORMAL or tk.DISABLED self.outdir_label['state'] = local and tk.NORMAL or tk.DISABLED self.outbutton['state'] = local and tk.NORMAL or tk.DISABLED self.outdir_entry['state'] = local and 'readonly' or tk.DISABLED - self.eddn_station_button['state'] = not monitor.is_beta and tk.NORMAL or tk.DISABLED - self.eddn_auto_button['state'] = self.eddn_station.get() and logvalid and not monitor.is_beta and tk.NORMAL or tk.DISABLED + self.eddn_station_button['state'] = tk.NORMAL or tk.DISABLED + self.eddn_auto_button['state'] = self.eddn_station.get() and logvalid and tk.NORMAL or tk.DISABLED self.eddn_system_button['state']= logvalid and tk.NORMAL or tk.DISABLED self.eddn_delay_button['state'] = logvalid and eddn.replayfile and self.eddn_system.get() and tk.NORMAL or tk.DISABLED - self.edsm_log_button['state'] = logvalid and not monitor.is_beta and tk.NORMAL or tk.DISABLED - edsm_state = logvalid and monitor.cmdr and not monitor.is_beta and self.edsm_log.get() and tk.NORMAL or tk.DISABLED + self.edsm_log_button['state'] = logvalid and tk.NORMAL or tk.DISABLED + edsm_state = logvalid and monitor.cmdr and self.edsm_log.get() and tk.NORMAL or tk.DISABLED self.edsm_label['state'] = self.edsm_cmdr_label['state'] = self.edsm_user_label['state'] = self.edsm_apikey_label['state'] = edsm_state self.edsm_cmdr_text['state'] = self.edsm_user['state'] = self.edsm_apikey['state'] = edsm_state @@ -555,7 +555,7 @@ class PreferencesDialog(tk.Toplevel): def apply(self): - if self.cmdr and not monitor.is_beta: + if self.cmdr: if self.password.get().strip(): config.set_password(self.username.get().strip(), self.password.get().strip()) # Can fail if keyring not unlocked else: diff --git a/stats.py b/stats.py index 19f7e4d5..c3a089c2 100644 --- a/stats.py +++ b/stats.py @@ -176,8 +176,8 @@ class StatsDialog(): self.showstats() def showstats(self): - if not monitor.cmdr or monitor.is_beta: - return # In Beta - do nothing + if not monitor.cmdr: + return self.status['text'] = _('Fetching data...') self.parent.update_idletasks()