From fd06bf22966e100dd55a209caa882cbbc972af86 Mon Sep 17 00:00:00 2001 From: mjishnu Date: Sat, 6 Apr 2024 12:12:12 +0530 Subject: [PATCH] retrive metadata using GET if HEAD request fails(#15) --- pypdl/main.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pypdl/main.py b/pypdl/main.py index 42bf5a1..c1c0100 100644 --- a/pypdl/main.py +++ b/pypdl/main.py @@ -112,15 +112,19 @@ def _multi_thread(self, segments, segement_table): def _get_header(self, url): kwargs = self._kwargs.copy() kwargs.pop("params", None) - response = requests.head(url, **kwargs) - if response.status_code != 200: - self._interrupt.set() - raise ConnectionError( - f"Server Returned: {response.reason}({response.status_code}), Invalid URL" - ) + with requests.head(url, **kwargs) as response: + if response.status_code == 200: + return response.headers + + with requests.get(url, stream=True, **self._kwargs) as response: + if response.status_code == 200: + return response.headers - return response.headers + self._interrupt.set() + raise ConnectionError( + f"Server Returned: {response.reason}({response.status_code}), Invalid URL" + ) def _get_info(self, url, file_path, multithread, etag): header = self._get_header(url)