BirdyNet/addons/0.pageDownloader.py
2021-03-25 20:45:33 +01:00

57 lines
1.7 KiB
Python

global urllib
import urllib.request
global downloadPage
def downloadPage(window,downloadId,url,headers = False):
global browserWindowsLock
global browserWindows
if not headers: headers = {}
response = {
"url": url,
"body": "",
"headers": []
}
class rdrh(urllib.request.HTTPRedirectHandler):
def redirect_request(self,req,fp,code,msg,hdrs,newurl):
response["url"] = newurl
try:
return urllib.request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, hdrs, newurl)
except urllib.error.HTTPError as e:
return e
opener = urllib.request.build_opener(rdrh)
opener.addheaders = headers
try:
requestHandler = opener.open(url)
# Check whether to transfer the page to the viewer, or if to open downloader
response["headers"] = requestHandler.getheaders()
contentType, contentTypeArguments = getContentType(response["headers"],"application/octet-stream")
if not contentType.startswith("text/"): # Make the fileDownloader handle the request instead
return
response["body"] = requestHandler.read()
requestHandler.close()
except urllib.error.HTTPError as e:
response["headers"] = e.getheaders()
response["body"] = e.read()
e.close()
except Exception as e:
response["body"] = str(e).encode("utf-8")
response["headers"] = [["content-type","text; charset=utf-8"]]
browserWindowsLock.acquire()
if not window in browserWindows:
browserWindowsLock.release()
return
window.cTaskLock.acquire()
if window.cDownloadId != downloadId:
window.cTaskLock.release()
browserWindowsLock.release()
return
window.cTasks.append((window.cHandleResponse,[response],{}))
window.cTaskLock.release()
browserWindowsLock.release()