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

Total bytes transferred counter - WIP #2531

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changedetectionio/content_fetchers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Fetcher():
instock_data = None
instock_data_js = ""
status_code = None
total_bytes = None
webdriver_js_execute_code = None
xpath_data = None
xpath_element_js = ""
Expand Down
25 changes: 25 additions & 0 deletions changedetectionio/content_fetchers/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
class fetcher(Fetcher):
fetcher_description = "Basic fast Plaintext/HTTP Client"

def get_total_bytes_received(self, response):
# Calculate the size of the response content
content_size = len(response.content)
# Calculate the size of the response headers
headers_size = sum(len(k) + len(v) for k, v in response.headers.items()) + len(response.headers) * 4 # adding 4 for ': ' and '\r\n'

# Total bytes received
total_received = content_size + headers_size
return total_received

def get_total_bytes_transferred(self, request):
# Calculate the size of the request headers
headers_size = sum(len(k) + len(v) for k, v in request.headers.items()) + len(request.headers) * 4 # adding 4 for ': ' and '\r\n'

# Calculate the size of the request body, if any
body_size = len(request.body or '')

# Total bytes transferred (request + response)
total_transferred = headers_size + body_size
return total_transferred

def __init__(self, proxy_override=None, custom_browser_connection_url=None):
super().__init__()
self.proxy_override = proxy_override
Expand Down Expand Up @@ -60,6 +81,10 @@ def run(self,
proxies=proxies,
verify=False)

total_received = self.get_total_bytes_received(response=r)
request_prepared = r.request
self.total_bytes = self.get_total_bytes_transferred(request_prepared) + total_received

# If the response did not tell us what encoding format to expect, Then use chardet to override what `requests` thinks.
# For example - some sites don't tell us it's utf-8, but return utf-8 content
# This seems to not occur when using webdriver/selenium, it seems to detect the text encoding more reliably.
Expand Down
Loading