from pathlib import Path


def is_download_finished(temp_folder, fname=None):
    """Check if a file download is finished.

    Parameters
    ----------
    temp_folder : str
        Temporary folder
    fname : str, optional
        File name of the downloaded file.
        If None, any file name will do ('*').

    https://stackoverflow.com/a/53602937
    """
    if fname is None:
        firefox_temp_file = sorted(Path(temp_folder).glob("*.part"))
        chrome_temp_file = sorted(Path(temp_folder).glob("*.crdownload"))
        downloaded_files = sorted(Path(temp_folder).glob("*.*"))
    else:
        firefox_temp_file = sorted(Path(temp_folder).glob(fname + ".part"))
        chrome_temp_file = sorted(Path(temp_folder).glob(fname + ".crdownload"))
        downloaded_files = sorted(Path(temp_folder).glob(fname))
    # do check
    if (
        (len(firefox_temp_file) == 0)
        and (len(chrome_temp_file) == 0)
        and (len(downloaded_files) >= 1)
    ):
        # all good
        return True
    else:
        return False