SHARE
TWEET

minecraft-upgrade

Snuggledash Jun 4th, 2020 4,371 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. config_file = '/etc/minecraft.conf'
  4. manifest_url = 'https://launchermeta.mojang.com/mc/game/version_manifest.json'
  5.  
  6. import configparser, os, os.path, zipfile, io, json, sys
  7. import requests
  8.  
  9. cparser = configparser.ConfigParser()
  10. with open(config_file) as f:
  11.     # ConfigParser needs INI-style config with at least one section
  12.     cparser.read_string('[a]\n' + f.read())
  13.  
  14. mc_dir = cparser['a']['srvdir']
  15. build_type = cparser['a']['build_type']
  16.  
  17. mc_jar = os.path.join(mc_dir, 'server.jar')
  18. with zipfile.ZipFile(mc_jar) as z:
  19.     with z.open('version.json') as f:
  20.         mc_ver_local = json.load(io.TextIOWrapper(f, 'utf-8'))['id']
  21.  
  22. all_manifest = requests.get(manifest_url).json()
  23. mc_ver_remote = all_manifest['latest'][build_type]
  24.  
  25. if mc_ver_local != mc_ver_remote:
  26.     print('upgrade from', mc_ver_local, 'to', mc_ver_remote)
  27.     new_manifest_url = [x['url'] for x in all_manifest['versions'] if x['id'] == mc_ver_remote][0]
  28.     new_manifest = requests.get(new_manifest_url).json()
  29.     download_url = new_manifest['downloads']['server']['url']
  30.     download_file = os.path.join('versions', mc_ver_remote + '.jar')
  31.     download_file_full = os.path.join(mc_dir, download_file)
  32.     resp = requests.get(download_url, stream=True)
  33.     if resp.ok:
  34.         with open(download_file_full, 'wb') as f:
  35.             for chunk in resp.iter_content(None):
  36.                 f.write(chunk)
  37.         mc_jar_new = os.path.join(mc_dir, 'server.jar.new')
  38.         os.symlink(download_file, mc_jar_new)
  39.         os.rename(mc_jar_new, mc_jar)
  40.     else:
  41.         print('download failed:', resp.status_code, file=sys.stderr)
  42.         exit(1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top