Skip to content

Instantly share code, notes, and snippets.

@stephanlachnit
Last active January 20, 2022 22:39
Show Gist options
  • Save stephanlachnit/68ec64cfae80293875c9c84da02d85cd to your computer and use it in GitHub Desktop.
Save stephanlachnit/68ec64cfae80293875c9c84da02d85cd to your computer and use it in GitHub Desktop.
Get the latest Minecraft server.jar (including snapshots)
#!/usr/bin/python3
#
# SPDX-FileCopyrightText: 2021 Stephan Lachnit <stephanlachnit@debian.org>
#
# SPDX-License-Identifier: MIT
import hashlib
import os.path
import sys
import requests
# set false if release version should be selected by default
prefer_snapshot = True
# download folder for server.jar
download_folder = '.'
# url for version_manifest_v2.json
version_manifest_url = 'https://launchermeta.mojang.com/mc/game/version_manifest_v2.json'
# chunk size for download and checksum iterations
iter_chunk_size = 65536
if __name__ == '__main__':
print('Fetching version manifest')
# get the version manifest
_response = requests.get(version_manifest_url)
if _response.status_code != requests.codes.ok:
raise Exception('Could not download version manifest, please check your internet connection')
version_manifest = _response.json()
# get latest version
version_type = 'release'
if prefer_snapshot:
version_type = 'snapshot'
version_id_latest = version_manifest['latest'][version_type]
# ask which version to download
version_id_requested = input(f'Download version [{version_id_latest}]: ')
if version_id_requested == '':
version_id_requested = version_id_latest
# loop versions for requested version
version_found = False
version_url = str()
for _version in version_manifest['versions']:
if _version['id'] == version_id_requested:
version_found = True
version_url = _version['url']
break
if not version_found:
raise Exception(f'Version {version_id_requested} not found')
# download version information
version_json = requests.get(version_url).json()
# get server.jar download url, size and checksum
server_jar_url = version_json['downloads']['server']['url']
server_jar_size = version_json['downloads']['server']['size']
server_jar_sha1 = version_json['downloads']['server']['sha1']
print(f'Downloading server.jar for version {version_id_requested} ({float(server_jar_size)/(1024**2):.0f} MB)')
download_server_jar_size = 0
download_server_jar_sha1 = hashlib.sha1()
# download server jar in chunks
with open(os.path.join(download_folder, 'server.jar'), 'wb') as _server_jar:
_response = requests.get(server_jar_url, stream=True)
# write chunks and update progress
for chunk in _response.iter_content(chunk_size=iter_chunk_size):
_server_jar.write(chunk)
download_server_jar_size += len(chunk)
# update progress bar
downloaded_perc_n50 = int(50 * download_server_jar_size / server_jar_size)
sys.stdout.write('\r['+('=' * downloaded_perc_n50)+(' ' * (50 - downloaded_perc_n50))+f'] ({download_server_jar_size} / {server_jar_size})')
sys.stdout.flush()
# update sha1
download_server_jar_sha1.update(chunk)
print('\nDownload complete')
print(f'Verifying server.jar (sha1 {server_jar_sha1})')
# sanity check the size
if download_server_jar_size != server_jar_size:
raise Warning(f'Size mismatch ({download_server_jar_size} != {server_jar_size})')
# check the checksum
download_server_jar_sha1 = download_server_jar_sha1.hexdigest()
if download_server_jar_sha1 != server_jar_sha1:
raise Warning(f'Checksum mismatch ({download_server_jar_sha1} != {server_jar_sha1})')
print('Verification complete')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment