Skip to content

Commit

Permalink
Simplify HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dagwieers committed Sep 17, 2019
1 parent 4e13631 commit 9e2ca88
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/inputstreamhelper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,10 @@ def _has_widevine(self):
return False

@staticmethod
def _http_get(url):
''' Perform an HTTP GET request and return content '''
def _http_request(url):
''' Perform an HTTP request and return request '''
log('Request URL: {url}', url=url)
filename = url.split('/')[-1]

for retry in (False, True):
try:
Expand All @@ -346,9 +347,19 @@ def _http_get(url):
raise HTTPError('HTTP %s Error for url: %s' % (req.getcode(), url), response=req)
break
except HTTPError:
Dialog().ok(localize(30004), localize(30013, filename=filename)) # Failed to retrieve file
return None
except BadStatusLine:
if retry:
Dialog().ok(localize(30004), localize(30013, filename=url.split('/')[-1])) # Failed to retrieve file
Dialog().ok(localize(30004), localize(30013, filename=filename)) # Failed to retrieve file
return None
return req

def _http_get(self, url):
''' Perform an HTTP GET request and return content '''
req = self._http_request(url)
if req is None:
return None

content = req.read()
# NOTE: Do not log reponse (as could be large)
Expand All @@ -357,24 +368,15 @@ def _http_get(url):

def _http_download(self, url, message=None):
"""Makes HTTP request and displays a progress dialog on download."""
log('Request URL: {url}', url=url)
filename = url.split('/')[-1]

for retry in (False, True):
try:
req = urlopen(url)
log('Response code: {code}', code=req.getcode())
if 400 <= req.getcode() < 600:
raise HTTPError('HTTP %s Error for url: %s' % (req.getcode(), url), response=req)
break
except (HTTPError, BadStatusLine):
if retry:
Dialog().ok(localize(30004), localize(30013, filename=filename)) # Failed to retrieve file
return False
req = self._http_request(url)
if req is None:
return None

if not message: # display "downloading [filename]"
message = localize(30015, filename=filename) # Downloading file

filename = url.split('/')[-1]

self._download_path = os.path.join(self._temp_path(), filename)
total_length = float(req.info().get('content-length'))
progress_dialog = DialogProgress()
Expand Down

0 comments on commit 9e2ca88

Please sign in to comment.