diff --git a/EDMarketConnector.py b/EDMarketConnector.py index d4253753..fe2fdf09 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -106,9 +106,12 @@ class AppWindow: self.session.login(config.get('username'), config.get('password')) self.status['text'] = '' except companion.VerificationRequired: - # don't worry about authentication now + # don't worry about authentication now - prompt on query self.status['text'] = '' + except companion.ServerError as e: + self.status['text'] = str(e) except Exception as e: + if __debug__: print_exc() self.status['text'] = str(e) self.cooldown() @@ -144,36 +147,34 @@ class AppWindow: # Validation if not data.get('commander') or not data['commander'].get('name','').strip(): - raise Exception("Who are you?!") # Shouldn't happen + self.status['text'] = "Who are you?!" # Shouldn't happen elif not data['commander'].get('docked'): - raise Exception("You're not docked at a station!") + self.status['text'] = "You're not docked at a station!" elif not data.get('lastSystem') or not data['lastSystem'].get('name','').strip(): - raise Exception("Where are you?!") # Shouldn't happen + self.status['text'] = "Where are you?!" # Shouldn't happen elif not data.get('lastStarport') or not data['lastStarport'].get('commodities'): - raise Exception("Station doesn't have a market!") - - 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: - eddn.export(data, self.setstatus) + 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 config.getint('output') & config.OUT_EDDN: + eddn.export(data, self.setstatus) + self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime)) except companion.VerificationRequired: return prefs.AuthenticationDialog(self.w, self.verify) + except companion.ServerError as e: + self.status['text'] = str(e) + except Exception as e: if __debug__: print_exc() self.status['text'] = str(e) - else: - self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime)) - self.cooldown() def cooldown(self): diff --git a/companion.py b/companion.py index bfb328a9..e3746ddf 100644 --- a/companion.py +++ b/companion.py @@ -40,6 +40,10 @@ bracketmap = { 1: 'Low', 3: 'High', } +class ServerError(Exception): + def __str__(self): + return 'Error: Server is down' + class CredentialsError(Exception): def __str__(self): return 'Error: Invalid Credentials' @@ -79,9 +83,15 @@ class Session: else: self.credentials = { 'email' : username, 'password' : password } r = self.session.post('https://companion.orerve.net/user/login', data = self.credentials) + if r.status_code != requests.codes.ok: + self.dump(r) r.raise_for_status() - if 'Password' in r.text: + if 'server error' in r.text: + self.dump(r) + raise ServerError() + elif 'Password' in r.text: + self.dump(r) raise CredentialsError() elif 'Verification Code' in r.text: self.state = Session.STATE_AUTH @@ -104,18 +114,24 @@ class Session: if self.state == Session.STATE_NONE: raise Exception('General error') # Shouldn't happen elif self.state == Session.STATE_INIT: - raise CredentialsError() + self.login() elif self.state == Session.STATE_AUTH: raise VerificationRequired() r = self.session.get('https://companion.orerve.net/profile') + if r.status_code != requests.codes.ok: + self.dump(r) if r.status_code == requests.codes.forbidden: - # Maybe our session cookie expired? + # Start again - maybe our session cookie expired? self.login() - r = self.session.get('https://companion.orerve.net/profile') + self.query() r.raise_for_status() - return json.loads(r.text) + try: + return json.loads(r.text) + except: + self.dump(r) + raise ServerError() def close(self): self.state = Session.STATE_NONE @@ -125,3 +141,9 @@ class Session: except: pass self.session = None + + def dump(self, r): + if __debug__: + print 'Status\t%s' % r.status_code + print 'Headers\t%s' % r.headers + print ('Content:\n%s' % r.text).encode('utf-8')