Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -134,42 +133,35 @@ 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

# 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] <Error>" % (i, i_len))
i += 1
continue
source_url = urlparse(download_entry['url'])
download_cache_children = Path(source_url.path).parent.relative_to('/')
Expand All @@ -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()
Expand Down