|
1 | 1 | """Download files from sharing links.""" |
2 | 2 |
|
3 | 3 | import base64 |
4 | | -import cgi |
5 | 4 | import os |
6 | 5 | import re |
7 | 6 | import traceback |
8 | 7 | import warnings |
9 | 8 | import zipfile |
10 | 9 | from abc import ABC, abstractmethod |
| 10 | +from email.message import EmailMessage |
11 | 11 | from typing import Optional |
12 | 12 | from urllib.parse import parse_qs, urlparse |
13 | 13 | from urllib.request import urlopen, urlretrieve |
@@ -164,9 +164,20 @@ def _get_filename(self) -> str: # pragma: no cover |
164 | 164 | print(f"Could not open {self._url} for reading filename: {e}") |
165 | 165 | raise ValueError(f"Could not open {self._url} for reading filename") from e |
166 | 166 |
|
167 | | - info = remotefile.info()["Content-Disposition"] |
168 | | - value, params = cgi.parse_header(info) |
169 | | - return params["filename"] |
| 167 | + # Parse remotefile info (recommended by PEP594) |
| 168 | + msg = EmailMessage() |
| 169 | + content_disp = remotefile.info().get("content-disposition") |
| 170 | + if content_disp is None: |
| 171 | + raise KeyError( |
| 172 | + "Content-Disposition header not found, which is necessary to extract the filename." |
| 173 | + ) |
| 174 | + msg["content-disposition"] = content_disp |
| 175 | + |
| 176 | + filename = msg["content-disposition"].params.get("filename") |
| 177 | + if filename is None: |
| 178 | + raise KeyError("Could not extract filename from remote file.") |
| 179 | + |
| 180 | + return filename |
170 | 181 |
|
171 | 182 | def _download_file( |
172 | 183 | self, max_size_kb: Optional[int] = None |
@@ -221,7 +232,7 @@ class OnedriveDownloader(FileDownloader): |
221 | 232 | def _encode_url(self) -> str: # pragma: no cover |
222 | 233 | """Encode onedrive sharing link as url for downloading files.""" |
223 | 234 | b64_string = base64.urlsafe_b64encode(str.encode(self._url)).decode("utf-8") |
224 | | - encoded_url = f'https://api.onedrive.com/v1.0/shares/u!{b64_string.replace("=", "")}/root/content' |
| 235 | + encoded_url = f"https://api.onedrive.com/v1.0/shares/u!{b64_string.replace('=', '')}/root/content" |
225 | 236 | return encoded_url |
226 | 237 |
|
227 | 238 |
|
|
0 commit comments