diff --git a/downloader.py b/downloader.py index e4fcfdd..6fe2db7 100644 --- a/downloader.py +++ b/downloader.py @@ -117,13 +117,12 @@ def doDownload(manifest): sess = requests.session() - i = 1 iLen = len(manifestJson['files']) print("%d files to download" % (iLen)) programGui.setOutput("%d files to download" % (iLen)) - for dependency in manifestJson['files']: + for i, dependency in enumerate(manifestJson['files'], start=1): depCacheDir = cache_path / str(dependency['projectID']) / str(dependency['fileID']) if depCacheDir.is_dir(): # File is cached @@ -134,8 +133,6 @@ def doDownload(manifest): shutil.copyfile(str(depFile), str(targetFile)) programGui.setOutput("[%d/%d] %s (cached)" % (i, iLen, targetFile.name)) - i += 1 - # Cache access is successful, # Don't download the file continue @@ -143,33 +140,28 @@ def doDownload(manifest): # File is not cached and needs to be downloaded projectResponse = sess.get("http://minecraft.curseforge.com/mc-mods/%s" % (dependency['projectID']), stream=True) fileResponse = sess.get("%s/files/%s/download" % (projectResponse.url, dependency['fileID']), stream=True) - while fileResponse.is_redirect: - source = fileResponse - fileResponse = sess.get(source, stream=True) filePath = Path(fileResponse.url) fileName = unquote(filePath.name) print("[%d/%d] %s" % (i, iLen, fileName)) programGui.setOutput("[%d/%d] %s" % (i, iLen, fileName)) - with open(str(minecraftPath / "mods" / fileName), "wb") as mod: - mod.write(fileResponse.content) + mod_filename = str(minecraftPath / "mods" / fileName) + with open(mod_filename, "wb") as mod: + for chunk in fileResponse.iter_content(chunk_size=1024*512): + if chunk: + mod.write(chunk) # Try to add file to cache. if not depCacheDir.exists(): depCacheDir.mkdir(parents=True) - with open(str(depCacheDir / fileName), "wb") as mod: - mod.write(fileResponse.content) - - i += 1 + shutil.copyfile(mod_filename, str(depCacheDir / fileName)) # This is not available in curse-only packs if 'directDownload' in manifestJson: - i = 1 i_len = len(manifestJson['directDownload']) programGui.setOutput("%d additional files to download." % i_len) - for download_entry in manifestJson['directDownload']: + for i, download_entry in enumerate(manifestJson['directDownload'], start=1): if "url" not in download_entry or "filename" not in download_entry: programGui.setOutput("[%d/%d] " % (i, i_len)) - i += 1 continue source_url = urlparse(download_entry['url']) download_cache_children = Path(source_url.path).parent.relative_to('/') @@ -179,22 +171,16 @@ def doDownload(manifest): # Cached target_file = minecraftPath / "mods" / cache_target.name shutil.copyfile(str(cache_target), str(target_file)) - - i += 1 - # Cache access is successful, # Don't download the file continue # File is not cached and needs to be downloaded file_response = sess.get(source_url, stream=True) - while file_response.is_redirect: - source = file_response - file_response = sess.get(source, stream=True) programGui.setOutput("[%d/%d] %s" % (i, i_len, download_entry['filename'])) with open(str(minecraftPath / "mods" / download_entry['filename']), "wb") as mod: - mod.write(file_response.content) - - i += 1 + for chunk in file_response.iter_content(chunk_size=1024*512): + if chunk: + mod.write(chunk) if args.gui: programGui = downloadUI()