From 8b457f82375afa9135ac0b5dbe0dcde823927afe Mon Sep 17 00:00:00 2001 From: S03D4-164 Date: Mon, 21 Sep 2015 06:00:52 +0000 Subject: [PATCH] Modified unsupported conntent donwload. --- ghost/ghost.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/ghost/ghost.py b/ghost/ghost.py index 36d3d1f..0ef9bee 100755 --- a/ghost/ghost.py +++ b/ghost/ghost.py @@ -197,7 +197,7 @@ def __init__(self, session, reply, content): self.http_status = reply.attribute( QNetworkRequest.HttpStatusCodeAttribute) self.session.logger.info( - "Resource loaded: %s %s" % (self.url, self.http_status) + "Resource loaded: %s %s %s" % (self.url, self.http_status, len(self.content)) ) self.headers = {} for header in reply.rawHeaderList(): @@ -229,8 +229,9 @@ class NetworkAccessManager(QNetworkAccessManager): :param exclude_regex: A regex use to determine wich url exclude when sending a request """ - def __init__(self, exclude_regex=None, *args, **kwargs): + def __init__(self, exclude_regex=None, logger=None, *args, **kwargs): self._regex = re.compile(exclude_regex) if exclude_regex else None + self.logger = logger super(self.__class__, self).__init__(*args, **kwargs) def createRequest(self, operation, request, data): @@ -245,9 +246,19 @@ def createRequest(self, operation, request, data): data ) reply.readyRead.connect(lambda reply=reply: replyReadyRead(reply)) + reply.error.connect(self._reply_error) + if self.logger.isEnabledFor(logging.DEBUG): + reply.downloadProgress.connect(self._reply_download_progress) time.sleep(0.001) return reply + def _reply_error(self, arg): + self.logger.error("Reply error: %s" % arg) + + def _reply_download_progress(self, sent, total): + self.logger.debug("Download Progress: %s sent / %s total" % (sent, total)) + time.sleep(0.1) + class Ghost(object): """`Ghost` manages a Qt application. @@ -394,7 +405,7 @@ def __init__( if network_access_manager_class is not None: self.page.setNetworkAccessManager( - network_access_manager_class(exclude_regex=exclude)) + network_access_manager_class(exclude_regex=exclude, logger=self.logger)) QtWebKit.QWebSettings.setMaximumPagesInCache(0) QtWebKit.QWebSettings.setObjectCacheCapacities(0, 0, 0) @@ -1194,6 +1205,7 @@ def wait_for_page_loaded(self, timeout=None): """ self.wait_for(lambda: self.loaded, 'Unable to load requested page', timeout) + self.sleep(1) resources = self._release_last_resources() page = None @@ -1263,7 +1275,6 @@ def _page_loaded(self): """Called back when page is loaded. """ self.loaded = True - self.sleep() def _page_load_started(self): """Called back when page load started. @@ -1276,6 +1287,7 @@ def _release_last_resources(self): :return: The released resources. """ last_resources = self.http_resources + self.logger.info(str(len(last_resources)) + " resources loaded.") self.http_resources = [] return last_resources @@ -1306,9 +1318,13 @@ def _unsupported_content(self, reply): self.logger.info("Unsupported content %s" % ( str(reply.url()), )) - + reply.readyRead.disconnect(replyReadyRead(reply)) + if hasattr(reply, 'data'): + reply.data = '' reply.readyRead.connect( lambda reply=reply: self._reply_download_content(reply)) + while reply.isRunning(): + self.sleep() def _reply_download_content(self, reply): """Adds an HttpResource object to http_resources with unsupported @@ -1317,11 +1333,14 @@ def _reply_download_content(self, reply): :param reply: The QNetworkReply object. """ if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute): + reply.data = reply.data + reply.readAll() + """ self.http_resources.append(HttpResource( self, reply, reply.readAll(), )) + """ def _on_manager_ssl_errors(self, reply, errors): url = unicode(reply.url().toString())