Write Python script for download Minecraft client with specific version from Mojang…
- User
Write Python script for download Minecraft client with specific version from Mojang servers Usage example: minecraft-download.py 1.19.2 ./clientdir
- Assistant
Write Python script for download Minecraft client with specific version from Mojang servers Usage example: minecraft-download.py 1.19.2 ./clientdir
To download a specific version of the Minecraft client from Mojang's servers, you'll need a couple of things: a way to fetch the Minecraft version manifest which lists all downloadable versions and their respective metadata, and a script to parse this manifest and download the necessary files. Let's write a Python script to achieve this.
First, ensure that you have Python installed on your machine. This script will use the requests library for HTTP requests, so make sure to install it if you haven't already:
pip install requests
Here’s a Python script that accomplishes the task:
import sys
import requests
import os
def download_file(url, path):
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(path, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
return True
return False
def main(version, directory):
# Ensure the target directory exists
if not os.path.exists(directory):
os.makedirs(directory)
# Mojang version manifest URL
manifest_url = "https://launchermeta.mojang.com/mc/game/version_manifest.json"
print("Fetching version manifest...")
manifest_response = requests.get(manifest_url)
if manifest_response.status_code != 200:
print("Failed to fetch version manifest.")
sys.exit(1)
manifest = manifest_response.json()
# Find the specific version download URL from the manifest
version_data = next((item for item in manifest['versions'] if item['id'] == version), None)
if not version_data:
print(f"Version {version} not found.")
sys.exit(1)
print(f"Fetching {version} data...")
version_response = requests.get(version_data['url'])
if version_response.status_code != 200:
print("Failed to fetch version data.")
sys.exit(1)
version_info = version_response.json()
client_url = version_info['downloads']['client']['url']
client_file_path = os.path.join(directory, f"{version}.jar")
print(f"Downloading Minecraft client {version}...")
if download_file(client_url, client_file_path):
print(f"Downloaded {version} successfully to {client_file_path}")
else:
print("Failed to download client.")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: minecraft-download.py <version> <directory>")
sys.exit(1)
minecraft_version = sys.argv[1]
download_directory = sys.argv[2]
main(minecraft_version, download_directory)
Save this code to a file named minecraft-download.py. Make sure you have Python and the requests library installed on your system.
Run the script from the command line with the version number and the target directory path as arguments, like so:
python minecraft-download.py 1.19.2 ./clientdir
This will download the Minecraft 1.19.2 client .jar file into the ./clientdir directory.
requests for HTTP requests.Not yet included in a published release.