Skip to content

Commit

Permalink
use ssl context instead of cafile option
Browse files Browse the repository at this point in the history
Fix #96
  • Loading branch information
fnep committed Jan 9, 2025
1 parent 1498705 commit 48a91bf
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions mtv_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from itertools import chain
from os import chdir
from pathlib import Path
from ssl import create_default_context
from tempfile import NamedTemporaryFile
from typing import Annotated
from typing import Any
Expand Down Expand Up @@ -74,7 +75,7 @@

# global state
HIDE_PROGRESSBAR = True
CAFILE: str | None = None
SSL_CONTEXT = create_default_context()
SHOWLIST: "Database"

logger = logging.getLogger("mtv_dl")
Expand Down Expand Up @@ -392,7 +393,11 @@ def _showlist(self, retries: int = 3) -> Iterator[BytesIO]:
retries -= 1
try:
logger.debug("Opening database from %r.", FILMLISTE_URL)
response: http.client.HTTPResponse = urllib.request.urlopen(FILMLISTE_URL, timeout=9, cafile=CAFILE)
response: http.client.HTTPResponse = urllib.request.urlopen(
FILMLISTE_URL,
timeout=9,
context=SSL_CONTEXT,
)
total_size = int(response.getheader("content-length") or 0)
with BytesIO() as buffer:
with progress_bar() as progress:
Expand Down Expand Up @@ -784,7 +789,7 @@ def _download_files(self, destination_dir_path: Path, target_urls: list[str]) ->
bar_id = progress.add_task(description=f"Downloading {self.label}")

for url in target_urls:
response: http.client.HTTPResponse = urllib.request.urlopen(url, timeout=60, cafile=CAFILE)
response: http.client.HTTPResponse = urllib.request.urlopen(url, timeout=60, context=SSL_CONTEXT)

# determine file size for progressbar
file_sizes.append(int(response.getheader("content-length") or 0))
Expand Down Expand Up @@ -1647,9 +1652,9 @@ def setup(
global HIDE_PROGRESSBAR
HIDE_PROGRESSBAR = bool(logfile) or no_bar or quiet

global CAFILE
global SSL_CONTEXT
if use_certifi:
CAFILE = certifi.where()
SSL_CONTEXT = create_default_context(cafile=certifi.where())

if verbose:
logger.setLevel(logging.DEBUG)
Expand Down

0 comments on commit 48a91bf

Please sign in to comment.