Threaded downloader
This commit is contained in:
parent
4613e7f956
commit
435026dc11
51
BirdyNet.py
51
BirdyNet.py
@ -90,6 +90,15 @@ class browserWindow(QMainWindow):
|
||||
"baseUrl": ""
|
||||
}
|
||||
self.cUserAgent = config["default"]["useragent"].replace("$OS",sys.platform).replace("$VER",versionString).replace("$DIST",distro)
|
||||
|
||||
self.cDownloadId = 0
|
||||
self.cTasks = []
|
||||
self.cTaskLock = threading.Lock()
|
||||
self.cTaskTimer = QTimer()
|
||||
self.cTaskTimer.setInterval(100)
|
||||
self.cTaskTimer.timeout.connect(self.cRunTasks)
|
||||
self.cTaskTimer.start()
|
||||
|
||||
self.cCreateElements()
|
||||
|
||||
def cCreateElements(self):
|
||||
@ -139,34 +148,42 @@ class browserWindow(QMainWindow):
|
||||
self.destroy()
|
||||
browserWindowsLock.release()
|
||||
|
||||
def cRunTasks(self):
|
||||
self.cTaskLock.acquire()
|
||||
for task in self.cTasks:
|
||||
task[0](*task[1],**task[2])
|
||||
self.cTasks = []
|
||||
self.cTaskLock.release()
|
||||
|
||||
def cNavigate(self,event = None):
|
||||
try:
|
||||
#print(prettyJson(parseUrl(self.cUrlBar.text())))
|
||||
self.cStatusBar.showMessage("Downloading...")
|
||||
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))
|
||||
self.cStatusBar.repaint()
|
||||
|
||||
infoFetcher(response)
|
||||
self.cDocumentInfo = response
|
||||
|
||||
self.cStatusBar.showMessage("Rendering...")
|
||||
self.update()
|
||||
start = time.time()
|
||||
self.cDoc.cRenderHtml(response["body"].decode("utf-8",errors="ignore"))
|
||||
end = time.time()
|
||||
print("Rendering page: " +str(end - start))
|
||||
self.cTaskLock.acquire()
|
||||
self.cDownloadId += 1
|
||||
url = self.cUrlBar.text().replace(" ","%20") # dirty
|
||||
threading.Thread(target=downloadPage, args=(self,self.cDownloadId,url,[("User-Agent",self.cUserAgent)])).start()
|
||||
self.cTaskLock.release()
|
||||
self.cDoc.setFocus()
|
||||
self.cStatusBar.showMessage("Ready")
|
||||
self.update()
|
||||
#print(prettyJson(response["headers"]))
|
||||
except Exception as e:
|
||||
self.cDoc.cRenderHtml(html.escape(str(e)))
|
||||
raise
|
||||
|
||||
def cHandleResponse(self,response):
|
||||
infoFetcher(response)
|
||||
self.cUrlBar.setText(response["url"])
|
||||
self.cDocumentInfo = response
|
||||
self.cStatusBar.showMessage("Rendering...")
|
||||
self.cStatusBar.repaint()
|
||||
start = time.time()
|
||||
self.cDoc.cRenderHtml(response["body"].decode("utf-8",errors="ignore"))
|
||||
end = time.time()
|
||||
print("Rendering page: " +str(end - start))
|
||||
self.cStatusBar.showMessage("Ready")
|
||||
|
||||
def cNewWindow(self):
|
||||
w = browserWindow()
|
||||
browserWindowsLock.acquire()
|
||||
|
@ -5,11 +5,13 @@ global html
|
||||
import html
|
||||
|
||||
global downloadPage
|
||||
def downloadPage(url,headers = False):
|
||||
def downloadPage(window,downloadId,url,headers = False):
|
||||
global browserWindowsLock
|
||||
global browserWindows
|
||||
if not headers: headers = {}
|
||||
response = {
|
||||
"url": url,
|
||||
"body": None,
|
||||
"body": "",
|
||||
"headers": []
|
||||
}
|
||||
|
||||
@ -31,6 +33,19 @@ def downloadPage(url,headers = False):
|
||||
except urllib.error.HTTPError as e:
|
||||
response["body"] = e.read()
|
||||
except Exception as e:
|
||||
response["body"] = html.escape(e)
|
||||
response["body"] = html.escape(str(e)).encode("utf-8")
|
||||
|
||||
return response
|
||||
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()
|
Loading…
Reference in New Issue
Block a user