def test_download_webfile(static_files_server, util, url, expected_result): filename = os.path.join(util.TMP_DIR, "moviepy_downloader_test.mp4") if os.path.isfile(filename): try: os.remove(filename) except PermissionError: pass if hasattr(expected_result, "__traceback__") or len(url) == 11: if not shutil.which("youtube-dl"): with pytest.raises(expected_result): download_webfile(url, filename) assert not os.path.isfile(filename) elif len(url) != 11: with pytest.raises(OSError) as exc: download_webfile(url, filename) assert "Error running youtube-dl." in str(exc.value) assert not os.path.isfile(filename) else: download_webfile(url, filename) assert os.path.isfile(filename) else: # network files with static_files_server(): download_webfile(url, filename) assert filecmp.cmp(filename, expected_result) if os.path.isfile(filename): try: os.remove(filename) except PermissionError: pass
def download_url(url, filename): """Download a file.""" if not os.path.exists(filename): print('Downloading {} ...'.format(filename)) download_webfile(url, filename) print('Downloading complete.')