From ebb20248cbebaf2ec4298437d741b4484b2e33e8 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sat, 20 Mar 2021 17:23:38 +0100 Subject: [PATCH] Add redirect support --- BirdyNet.py | 1 + addons/0.pageDownloader.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/BirdyNet.py b/BirdyNet.py index 0efe3a6..d21ecf1 100644 --- a/BirdyNet.py +++ b/BirdyNet.py @@ -140,6 +140,7 @@ class browserWindow(QMainWindow): self.update() start = time.time() response = downloadPage(self.cUrlBar.text(),{"User-Agent": self.cUserAgent}) + self.cUrlBar.setText(response["url"]) end = time.time() print("Downloading page: " +str(end - start)) diff --git a/addons/0.pageDownloader.py b/addons/0.pageDownloader.py index 110061a..3a097cf 100644 --- a/addons/0.pageDownloader.py +++ b/addons/0.pageDownloader.py @@ -12,10 +12,20 @@ def downloadPage(url,headers = False): "body": None, "headers": [] } - request = urllib.request.Request(url,headers=headers) - requestHandler = None + + class rdrh(urllib.request.HTTPRedirectHandler): + def redirect_request(self,req,fp,code,msg,hdrs,newurl): + response["url"] = newurl + return urllib.request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, hdrs, newurl) + + opener = urllib.request.build_opener(rdrh) + outHeaders = [] + for header in headers: + outHeaders.append((header,headers[header])) + opener.addheaders = outHeaders + requestHandler = opener.open(url) + try: - requestHandler = urllib.request.urlopen(request) response["headers"] = requestHandler.getheaders() response["body"] = requestHandler.read() except urllib.error.HTTPError as e: