Repository: Project-MONAI/MONAI
File: monai/apps/utils.py (Python)

Code before:
    try:
        if "drive.google.com" in data_url:
            response = requests.head(data_url, allow_redirects=True)
            cd = response.headers.get("Content-Disposition")  # Normal size file case
            if cd:
                filename = cd.split('filename="')[1].split('"')[0]
                return filename
            response = requests.get(data_url)
            if "text/html" in response.headers.get("Content-Type", ""):  # Big size file case
                soup = BeautifulSoup(response.text, "html.parser")
                filename_div = soup.find("span", {"class": "uc-name-size"})
                if filename_div:
                    filename = filename_div.find("a").text
                    return filename
            return None
        else:
            response = requests.head(data_url, allow_redirects=True)
            content_disposition = response.headers.get("Content-Disposition")
            if content_disposition:
                filename = re.findall("filename=(.+)", content_disposition)
                return filename[0].strip('"').strip("'")
            else:
                filename = _basename(data_url)
                return filename
    except Exception as e:
        raise Exception(f"Error processing URL: {e}") from e


def download_and_extract(
    url: str,
    filepath: PathLike = "",
    output_dir: PathLike = ".",
    hash_val: str | None = None,
    hash_type: str = "md5",
    file_type: str = "",
    has_base: bool = True,
    progress: bool = True,
) -> None:
    """
    Download file from URL and extract it to the output directory.

    Args:
        url: source URL link to download file.
        filepath: the file path of the downloaded compressed file.
            use this option to keep the directly downloaded compressed file, to avoid further repeated downloads.
        output_dir: target directory to save extracted files.
            default is the current directory.
        hash_val: expected hash value to validate the downloaded file.
            if None, skip hash validation.
        hash_type: 'md5' or 'sha1', defaults to 'md5'.
        file_type: string of file type for decompressing. Leave it empty to infer the type from url's base file name.

Review comment (suggestion):
Thank you for the quick update! It looks much better now. Here’s a simplified version that achieves the same functionality for your reference, could also fix the mypy issue:
```suggestion
def get_filename_from_url(data_url: str) -> str:
    """
    Get the filename from the URL link.
    """
    try:
        response = requests.head(data_url, allow_redirects=True)
        content_disposition = response.headers.get("Content-Disposition")
        if content_disposition:
            filename = re.findall("filename=\"?([^\";]+)\"?", content_disposition)
            if filename:
                return str(filename[0])
        if "drive.google.com" in data_url:
            response = requests.get(data_url)
            if "text/html" in response.headers.get("Content-Type", ""):
                soup = BeautifulSoup(response.text, "html.parser")
                filename_div = soup.find("span", {"class": "uc-name-size"})
                if filename_div:
                    return str(filename_div.find("a").text)
        return _basename(data_url)
    except Exception as e:
        raise Exception(f"Error processing URL: {e}") from e
```

Code after:
    """
    Get the filename from the URL link.
    """
    try:
        response = requests.head(data_url, allow_redirects=True)
        content_disposition = response.headers.get("Content-Disposition")
        if content_disposition:
            filename = re.findall('filename="?([^";]+)"?', content_disposition)
            if filename:
                return str(filename[0])
        if "drive.google.com" in data_url:
            response = requests.get(data_url)
            if "text/html" in response.headers.get("Content-Type", ""):
                soup = BeautifulSoup(response.text, "html.parser")
                filename_div = soup.find("span", {"class": "uc-name-size"})
                if filename_div:
                    return str(filename_div.find("a").text)
        return _basename(data_url)
    except Exception as e:
        raise Exception(f"Error processing URL: {e}") from e


def download_and_extract(
    url: str,
    filepath: PathLike = "",
    output_dir: PathLike = ".",
    hash_val: str | None = None,
    hash_type: str = "md5",
    file_type: str = "",
    has_base: bool = True,
    progress: bool = True,
) -> None:
    """
    Download file from URL and extract it to the output directory.

    Args:
        url: source URL link to download file.
        filepath: the file path of the downloaded compressed file.
            use this option to keep the directly downloaded compressed file, to avoid further repeated downloads.
        output_dir: target directory to save extracted files.
            default is the current directory.
        hash_val: expected hash value to validate the downloaded file.
            if None, skip hash validation.
        hash_type: 'md5' or 'sha1', defaults to 'md5'.
        file_type: string of file type for decompressing. Leave it empty to infer the type from url's base file name.
        has_base: whether the extracted files have a base folder. This flag is used when checking if the existing
            folder is a result of `extractall`, if it is, the extraction is skipped. For example, if A.zip is unzipped
            to folder structure `A/*.png`, this flag should be True; if B.zip is unzipped to `*.png`, this flag should
            be False.
        progress: whether to display progress bar.
    """