summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorexislow <exislow@users.noreply.github.com>2025-02-28 17:28:19 +0100
committerGitHub <noreply@github.com>2025-02-28 17:28:19 +0100
commit326246cdcc7882e8224f5c30c17fe59b74a47bb3 (patch)
tree41f3f53ae904e2588cc2b06c6fefaff9807ea08c
parent72f68ab73534f8c820de9388f101c3215d783f0e (diff)
parent303435ad1e9d5110e3f5b5e6d486a220edcd77db (diff)
downloadtidal-dl-ng-326246cdcc7882e8224f5c30c17fe59b74a47bb3.tar.gz
Merge pull request #377 from exislow/371-close-connection-after-download
👷 Close network connections explicitly. Fixes #371
-rw-r--r--tidal_dl_ng/download.py52
1 files changed, 30 insertions, 22 deletions
diff --git a/tidal_dl_ng/download.py b/tidal_dl_ng/download.py
index c72977b..73305b0 100644
--- a/tidal_dl_ng/download.py
+++ b/tidal_dl_ng/download.py
@@ -144,11 +144,16 @@ class Download:
progress_total: int = urls_count
block_size: int | None = None
elif urls_count == 1:
- # Get file size and compute progress steps
- r = requests.head(urls[0], timeout=REQUESTS_TIMEOUT_SEC)
- total_size_in_bytes: int = int(r.headers.get("content-length", 0))
- block_size: int | None = 1048576
- progress_total: float = total_size_in_bytes / block_size
+ try:
+ # Get file size and compute progress steps
+ r = requests.head(urls[0], timeout=REQUESTS_TIMEOUT_SEC)
+ total_size_in_bytes: int = int(r.headers.get("content-length", 0))
+ block_size: int | None = 1048576
+ progress_total: float = total_size_in_bytes / block_size
+ except:
+ raise
+ finally:
+ r.close()
else:
raise ValueError
@@ -236,27 +241,27 @@ class Download:
error: HTTPError | None = None
# Retry download on failed segments, with an exponential delay between retries
- s = requests.Session()
- retries = Retry(total=5, backoff_factor=1) # , status_forcelist=[ 502, 503, 504 ])
+ with requests.Session() as s:
+ retries = Retry(total=5, backoff_factor=1) # , status_forcelist=[ 502, 503, 504 ])
- s.mount("https://", HTTPAdapter(max_retries=retries))
+ s.mount("https://", HTTPAdapter(max_retries=retries))
- try:
- # Create the request object with stream=True, so the content won't be loaded into memory at once.
- r = s.get(url, stream=True, timeout=REQUESTS_TIMEOUT_SEC)
+ try:
+ # Create the request object with stream=True, so the content won't be loaded into memory at once.
+ r = s.get(url, stream=True, timeout=REQUESTS_TIMEOUT_SEC)
- r.raise_for_status()
+ r.raise_for_status()
- # Write the content to disk. If `chunk_size` is set to `None` the whole file will be written at once.
- with path_segment.open("wb") as f:
- for data in r.iter_content(chunk_size=block_size):
- f.write(data)
- # Advance progress bar.
- self.progress.advance(p_task)
+ # Write the content to disk. If `chunk_size` is set to `None` the whole file will be written at once.
+ with path_segment.open("wb") as f:
+ for data in r.iter_content(chunk_size=block_size):
+ f.write(data)
+ # Advance progress bar.
+ self.progress.advance(p_task)
- result = True
- except Exception:
- self.progress.advance(p_task)
+ result = True
+ except Exception:
+ self.progress.advance(p_task)
# To send the progress to the GUI, we need to emit the percentage.
if not progress_to_stdout:
@@ -585,10 +590,13 @@ class Download:
if url:
try:
- result = requests.get(url, timeout=REQUESTS_TIMEOUT_SEC).content
+ response: requests.Response = requests.get(url, timeout=REQUESTS_TIMEOUT_SEC)
+ result = response.content
except Exception as e:
# TODO: Implement propper logging.
print(e)
+ finally:
+ response.close()
elif path_file:
try:
with open(path_file, "rb") as f: