|
21 | 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 22 | THE SOFTWARE.
|
23 | 23 | """
|
| 24 | +import os |
24 | 25 | import six.moves.urllib.request
|
25 | 26 |
|
26 | 27 | try:
|
|
31 | 32 | VERSION_MANIFEST = "https://launchermeta.mojang.com/mc/game/version_manifest.json"
|
32 | 33 | LEGACY_VERSION_META = "https://s3.amazonaws.com/Minecraft.Download/versions/%(version)s/%(version)s.json" # DEPRECATED
|
33 | 34 |
|
| 35 | +_cached_version_manifest = None |
| 36 | +_cached_version_metas = {} |
| 37 | + |
34 | 38 | def _load_json(url):
|
35 | 39 | stream = six.moves.urllib.request.urlopen(url)
|
36 | 40 | try:
|
37 | 41 | return json.load(stream)
|
38 | 42 | finally:
|
39 | 43 | stream.close()
|
40 | 44 |
|
41 |
| -class Website(object): |
42 |
| - def __init__(self, username, password, version=999999): |
43 |
| - self.username = username |
44 |
| - self.password = password |
45 |
| - self.version = version |
46 |
| - |
47 |
| - @staticmethod |
48 |
| - def get_version_meta(version, verbose): |
49 |
| - """ |
50 |
| - Gets a version JSON file, first attempting the to use the version manifest |
51 |
| - and then falling back to the legacy site if that fails. |
52 |
| - Note that the main manifest should include all versions as of august 2018. |
53 |
| - """ |
54 |
| - version_manifest = _load_json(VERSION_MANIFEST) |
55 |
| - for version_info in version_manifest["versions"]: |
56 |
| - if version_info["id"] == version: |
57 |
| - address = version_info["url"] |
58 |
| - break |
59 |
| - else: |
60 |
| - if verbose: |
61 |
| - print("Failed to find %s in the main version manifest; using legacy site" % version) |
62 |
| - address = LEGACY_VERSION_META % {'version': version} |
63 |
| - if verbose: |
64 |
| - print("Loading version manifest for %s from %s" % (version, address)) |
65 |
| - return _load_json(address) |
66 |
| - |
67 |
| - @staticmethod |
68 |
| - def get_asset_index(version_meta, verbose): |
69 |
| - """Downloads the Minecraft asset index""" |
70 |
| - if "assetIndex" not in version_meta: |
71 |
| - raise Exception("No asset index defined in the version meta") |
72 |
| - asset_index = version_meta["assetIndex"] |
| 45 | +def get_version_manifest(): |
| 46 | + global _cached_version_manifest |
| 47 | + if _cached_version_manifest: |
| 48 | + return _cached_version_manifest |
| 49 | + |
| 50 | + _cached_version_manifest = _load_json(VERSION_MANIFEST) |
| 51 | + return _cached_version_manifest |
| 52 | + |
| 53 | +def get_version_meta(version, verbose): |
| 54 | + """ |
| 55 | + Gets a version JSON file, first attempting the to use the version manifest |
| 56 | + and then falling back to the legacy site if that fails. |
| 57 | + Note that the main manifest should include all versions as of august 2018. |
| 58 | + """ |
| 59 | + if version in _cached_version_metas: |
| 60 | + return _cached_version_metas[version] |
| 61 | + |
| 62 | + version_manifest = get_version_manifest() |
| 63 | + for version_info in version_manifest["versions"]: |
| 64 | + if version_info["id"] == version: |
| 65 | + address = version_info["url"] |
| 66 | + break |
| 67 | + else: |
73 | 68 | if verbose:
|
74 |
| - print("Assets: id %(id)s, url %(url)s" % asset_index) |
75 |
| - return _load_json(asset_index["url"]) |
| 69 | + print("Failed to find %s in the main version manifest; using legacy site" % version) |
| 70 | + address = LEGACY_VERSION_META % {'version': version} |
| 71 | + if verbose: |
| 72 | + print("Loading version manifest for %s from %s" % (version, address)) |
| 73 | + meta = _load_json(address) |
76 | 74 |
|
| 75 | + _cached_version_metas[version] = meta |
| 76 | + return meta |
| 77 | + |
| 78 | +def get_asset_index(version_meta, verbose): |
| 79 | + """Downloads the Minecraft asset index""" |
| 80 | + if "assetIndex" not in version_meta: |
| 81 | + raise Exception("No asset index defined in the version meta") |
| 82 | + asset_index = version_meta["assetIndex"] |
| 83 | + if verbose: |
| 84 | + print("Assets: id %(id)s, url %(url)s" % asset_index) |
| 85 | + return _load_json(asset_index["url"]) |
| 86 | + |
| 87 | + |
| 88 | +def client_jar(version, verbose): |
| 89 | + """Downloads a specific version, by name""" |
| 90 | + filename = version + ".jar" |
| 91 | + if not os.path.exists(filename): |
| 92 | + meta = get_version_meta(version, verbose) |
| 93 | + url = meta["downloads"]["client"]["url"] |
| 94 | + if verbose: |
| 95 | + print("Downloading %s from %s" % (version, url)) |
| 96 | + six.moves.urllib.request.urlretrieve(url, filename=filename) |
| 97 | + return filename |
77 | 98 |
|
78 |
| - @staticmethod |
79 |
| - def client_jar(path=None, reporthook=None, version="1.9"): |
80 |
| - url = "http://s3.amazonaws.com/Minecraft.Download/versions/%s/%s.jar" % (version, version) |
81 |
| - #url = "http://s3.amazonaws.com/MinecraftDownload/minecraft.jar" # 1.5.2 |
82 |
| - r = six.moves.urllib.request.urlretrieve(url, filename=path, reporthook=reporthook) |
83 |
| - return r[0] |
| 99 | +def latest_client_jar(verbose): |
| 100 | + manifest = get_version_manifest() |
| 101 | + return client_jar(manifest["latest"]["snapshot"], verbose) |
0 commit comments