Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified unsupported conntent donwload. #265

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions ghost/ghost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down