From 84ceccd60998a319ea9b4bc14d97ce31c166429e Mon Sep 17 00:00:00 2001 From: Noiredd Date: Fri, 7 Dec 2018 19:58:52 +0100 Subject: [PATCH] some degree of connection loss resistance --- fw-local/database.py | 7 ++++++- fw-local/filmweb.py | 24 ++++++++++++++++-------- fw-local/gui.py | 14 +++++++++----- fw-local/posterman.py | 5 ++++- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/fw-local/database.py b/fw-local/database.py index cb8f52c..60c721e 100644 --- a/fw-local/database.py +++ b/fw-local/database.py @@ -35,7 +35,12 @@ def storeToString(self): def softUpdate(self): self.callback(0) #display the progress bar # ask the API how many items should there be and how many are there per page - first_request = self.api.getNumOf(self.itemtype) + try: + # in case there are network problems + first_request = self.api.getNumOf(self.itemtype) + except self.api.ConnectionError: + self.callback(-1) + return None if first_request is None: #this will happen if the user fails to log in return None diff --git a/fw-local/filmweb.py b/fw-local/filmweb.py index 3680495..2646c63 100644 --- a/fw-local/filmweb.py +++ b/fw-local/filmweb.py @@ -18,19 +18,23 @@ def getUserPage(username): def getUserMoviePage(username, page=1): userpage = FilmwebAPI.Constants.getUserPage(username) return userpage + '/films?page=' + str(page) + ConnectionError = requests_html.requests.ConnectionError @staticmethod def login(username, password): session = requests_html.HTMLSession() - log = session.post( - FilmwebAPI.Constants.login_path, - data={'j_username': username, 'j_password': password} - ) + try: + log = session.post( + FilmwebAPI.Constants.login_path, + data={'j_username': username, 'j_password': password} + ) + except FilmwebAPI.ConnectionError: + return (False, True) # login error, connection error if FilmwebAPI.Constants.auth_error in log.text: print('BŁĄD LOGOWANIA') - return None + return (False, False) else: - return session + return (True, session) #@staticmethod -- but not actually a static method, see: # https://stackoverflow.com/q/21382801/6919631 @@ -152,10 +156,14 @@ def getMoviesPage(self, page=1): def __fetchPage(self, url): #fetch the page and return its parsed representation - page = self.session.get(url) + try: + page = self.session.get(url) + except: + raise ConnectionError if not page.ok: status = page.status_code - #we should probably do something about this + print("FETCH ERROR {}".format(status)) + raise ConnectionError else: return BS(page.html.html, 'lxml') diff --git a/fw-local/gui.py b/fw-local/gui.py index bac3773..2277c44 100644 --- a/fw-local/gui.py +++ b/fw-local/gui.py @@ -16,6 +16,8 @@ class Login(object): # the API. That function blocks until user enters data and clicks to log in. # It then calls API again to do the actual backend login stuff. default_message = 'Zaloguj się do filmweb.pl' + logerr_message = 'Niepowodzenie logowania!' + conerr_message = 'Sprawdź połączenie z internetem!' def __init__(self, root): self.__construct() @@ -83,8 +85,9 @@ def centerWindow(self): self.window.geometry('+{:.0f}+{:.0f}'.format(x, y)) #CALLBACKS - def _setStateBad(self, event=None): - self.infoLabel['text'] = 'Niepowodzenie logowania!' + def _setStateBad(self, event=None, connection=False): + message = self.conerr_message if connection else self.logerr_message + self.infoLabel['text'] = message self.stateGood = False def _setStateGood(self, event=None): if not self.stateGood: @@ -99,9 +102,10 @@ def _loginClick(self): password = self.passwordEntry.get() self.passwordEntry.delete(0, tk.END) #always clear the password field #attempt to log in - session = FilmwebAPI.login(username, password) - if session is None: - self._setStateBad() + success, session = FilmwebAPI.login(username, password) + if not success: + # in this case, "session" is actually a connection error flag + self._setStateBad(connection=session) else: #clear&hide, store session/username and pass control back to the caller self.usernameEntry.delete(0, tk.END) diff --git a/fw-local/posterman.py b/fw-local/posterman.py index 7b0517c..92ed7ba 100644 --- a/fw-local/posterman.py +++ b/fw-local/posterman.py @@ -24,7 +24,10 @@ def makePosterPath(self, pid:int): return os.path.join(self.CACHE_DIR, '{}.png'.format(pid)) def downloadPoster(self, url:str, path:str): # downloads from url, stores under path - response = self.session.get(url) + try: + response = self.session.get(url) + except: + return False if not response.ok: return False with open(path, 'wb') as ifile: