Skip to content

Commit

Permalink
some degree of connection loss resistance
Browse files Browse the repository at this point in the history
  • Loading branch information
Noiredd committed Dec 7, 2018
1 parent 00a3784 commit 84ceccd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
7 changes: 6 additions & 1 deletion fw-local/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions fw-local/filmweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down
14 changes: 9 additions & 5 deletions fw-local/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion fw-local/posterman.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 84ceccd

Please sign in to comment.