Snuggledash icon

minecraft-upgrade

Snuggledash | PRO | 06/04/20 06:25:54 PM UTC | 0 ⭐ | 11990 👁️ | Never ⏰ | []
Python |

1.64 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env python3
 
config_file = '/etc/minecraft.conf'
manifest_url = 'https://launchermeta.mojang.com/mc/game/version_manifest.json'
 
import configparser, os, os.path, zipfile, io, json, sys
import requests
 
cparser = configparser.ConfigParser()
with open(config_file) as f:
    # ConfigParser needs INI-style config with at least one section
    cparser.read_string('[a]\n' + f.read())
 
mc_dir = cparser['a']['srvdir']
build_type = cparser['a']['build_type']
 
mc_jar = os.path.join(mc_dir, 'server.jar')
with zipfile.ZipFile(mc_jar) as z:
    with z.open('version.json') as f:
        mc_ver_local = json.load(io.TextIOWrapper(f, 'utf-8'))['id']
 
all_manifest = requests.get(manifest_url).json()
mc_ver_remote = all_manifest['latest'][build_type]
 
if mc_ver_local != mc_ver_remote:
    print('upgrade from', mc_ver_local, 'to', mc_ver_remote)
    new_manifest_url = [x['url'] for x in all_manifest['versions'] if x['id'] == mc_ver_remote][0]
    new_manifest = requests.get(new_manifest_url).json()
    download_url = new_manifest['downloads']['server']['url']
    download_file = os.path.join('versions', mc_ver_remote + '.jar')
    download_file_full = os.path.join(mc_dir, download_file)
    resp = requests.get(download_url, stream=True)
    if resp.ok:
        with open(download_file_full, 'wb') as f:
            for chunk in resp.iter_content(None):
                f.write(chunk)
        mc_jar_new = os.path.join(mc_dir, 'server.jar.new')
        os.symlink(download_file, mc_jar_new)
        os.rename(mc_jar_new, mc_jar)
    else:
        print('download failed:', resp.status_code, file=sys.stderr)
        exit(1)

Comments