-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
updateMojang.py
executable file
·72 lines (60 loc) · 2.52 KB
/
updateMojang.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
import requests
from cachecontrol import CacheControl
import json
from metautil import *
from cachecontrol.caches import FileCache
forever_cache = FileCache('http_cache', forever=True)
sess = CacheControl(requests.Session(), forever_cache)
def get_version_file(path, url):
with open(path, 'w', encoding='utf-8') as f:
r = sess.get(url)
r.raise_for_status()
version_json = r.json()
assetId = version_json["assetIndex"]["id"]
assetUrl = version_json["assetIndex"]["url"]
json.dump(version_json, f, sort_keys=True, indent=4)
return assetId, assetUrl
def get_file(path, url):
with open(path, 'w', encoding='utf-8') as f:
r = sess.get(url)
r.raise_for_status()
version_json = r.json()
json.dump(version_json, f, sort_keys=True, indent=4)
# get the local version list
localVersionlist = None
try:
with open("upstream/mojang/version_manifest_v2.json", 'r', encoding='utf-8') as localIndexFile:
localVersionlist = MojangIndexWrap(json.load(localIndexFile))
except:
localVersionlist = MojangIndexWrap({})
localIDs = set(localVersionlist.versions.keys())
# get the remote version list
r = sess.get('https://launchermeta.mojang.com/mc/game/version_manifest_v2.json')
r.raise_for_status()
main_json = r.json()
remoteVersionlist = MojangIndexWrap(main_json)
remoteIDs = set(remoteVersionlist.versions.keys())
# versions not present locally but present remotely are new
newIDs = remoteIDs.difference(localIDs)
# versions present both locally and remotely need to be checked
checkedIDs = remoteIDs.difference(newIDs)
# versions that actually need to be updated have updated timestamps or are new
updatedIDs = newIDs
for id in checkedIDs:
remoteVersion = remoteVersionlist.versions[id]
localVersion = localVersionlist.versions[id]
if remoteVersion.time > localVersion.time:
updatedIDs.add(id)
# update versions and the linked assets files
assets = {}
for id in updatedIDs:
version = remoteVersionlist.versions[id]
print("Updating " + version.id + " to timestamp " + version.releaseTime.strftime('%s'))
assetId, assetUrl = get_version_file( "upstream/mojang/versions/" + id + '.json', version.url)
assets[assetId] = assetUrl
for assetId, assetUrl in iter(assets.items()):
print("assets", assetId, assetUrl)
get_file( "upstream/mojang/assets/" + assetId + '.json', assetUrl)
with open("upstream/mojang/version_manifest_v2.json", 'w', encoding='utf-8') as f:
json.dump(main_json, f, sort_keys=True, indent=4)