diff options
| author | Sefa Eyeoglu <contact@scrumplex.net> | 2022-04-02 14:57:19 +0200 |
|---|---|---|
| committer | Sefa Eyeoglu <contact@scrumplex.net> | 2022-04-05 14:30:30 +0200 |
| commit | 7cd825c67f288cfec366cb1e38f389e769307be9 (patch) | |
| tree | e3caedaa30c2249427e0fcc92d62294af09f4954 | |
| parent | 367518371fef6c4b7d655875be6853a4c283d482 (diff) | |
| download | Project-Tick-7cd825c67f288cfec366cb1e38f389e769307be9.tar.gz Project-Tick-7cd825c67f288cfec366cb1e38f389e769307be9.zip | |
refactor: move updateMojang to pydantic models
| -rw-r--r-- | meta/common/mojang.py | 10 | ||||
| -rw-r--r-- | meta/model/mojang.py | 52 | ||||
| -rwxr-xr-x | updateMojang.py | 126 |
3 files changed, 129 insertions, 59 deletions
diff --git a/meta/common/mojang.py b/meta/common/mojang.py new file mode 100644 index 0000000000..953e3d1d37 --- /dev/null +++ b/meta/common/mojang.py @@ -0,0 +1,10 @@ +from os.path import join + +BASE_DIR = "mojang" + +VERSION_MANIFEST_FILE = join(BASE_DIR, "version_manifest_v2.json") +VERSIONS_DIR = join(BASE_DIR, "versions") +ASSETS_DIR = join(BASE_DIR, "assets") + +MINECRAFT_COMPONENT = "" +LWJGL_COMPONENT = "" diff --git a/meta/model/mojang.py b/meta/model/mojang.py new file mode 100644 index 0000000000..37a14b4435 --- /dev/null +++ b/meta/model/mojang.py @@ -0,0 +1,52 @@ +from datetime import datetime +from typing import Optional, List + +from . import MetaBase + +''' +Mojang index files look like this: +{ + "latest": { + "release": "1.11.2", + "snapshot": "17w06a" + }, + "versions": [ + ... + { + "id": "17w06a", + "releaseTime": "2017-02-08T13:16:29+00:00", + "time": "2017-02-08T13:17:20+00:00", + "type": "snapshot", + "url": "https://launchermeta.mojang.com/mc/game/7db0c61afa278d016cf1dae2fba0146edfbf2f8e/17w06a.json" + }, + ... + ] +} +''' + + +class MojangLatestVersion(MetaBase): + release: str + snapshot: str + + +class MojangIndexEntry(MetaBase): + id: Optional[str] + releaseTime: Optional[datetime] + time: Optional[datetime] + type: Optional[str] + url: Optional[str] + sha1: Optional[str] + complianceLevel: Optional[int] + + +class MojangIndex(MetaBase): + latest: MojangLatestVersion + versions: List[MojangIndexEntry] + + +class MojangIndexWrap: + def __init__(self, index: MojangIndex): + self.index = index + self.latest = index.latest + self.versions = dict((x.id, x) for x in index.versions) diff --git a/updateMojang.py b/updateMojang.py index 9f106d735a..965ddb5b2f 100755 --- a/updateMojang.py +++ b/updateMojang.py @@ -1,74 +1,82 @@ +import json +import os.path + import requests from cachecontrol import CacheControl from cachecontrol.caches import FileCache -from meta.metautil import * -UPSTREAM_DIR = os.environ["UPSTREAM_DIR"] +from meta.common import upstream_path, ensure_upstream_dir +from meta.common.mojang import BASE_DIR, VERSION_MANIFEST_FILE, VERSIONS_DIR, ASSETS_DIR +from meta.model.mojang import MojangIndexWrap, MojangIndex + +UPSTREAM_DIR = upstream_path() + +ensure_upstream_dir(BASE_DIR) +ensure_upstream_dir(VERSIONS_DIR) +ensure_upstream_dir(ASSETS_DIR) forever_cache = FileCache('caches/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 fetch_version_file(path, url): + version_json = fetch_file(path, url) + asset_id = version_json["assetIndex"]["id"] + asset_url = version_json["assetIndex"]["url"] + return asset_id, asset_url -def get_file(path, url): +def fetch_file(path, url): + r = sess.get(url) + r.raise_for_status() + version_json = r.json() + 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) + return version_json + + +def main(): + # get the remote version list + r = sess.get('https://launchermeta.mojang.com/mc/game/version_manifest_v2.json') + r.raise_for_status() + + remote_versions = MojangIndexWrap(MojangIndex(**r.json())) + remote_ids = set(remote_versions.versions.keys()) + + version_manifest = os.path.join(UPSTREAM_DIR, VERSION_MANIFEST_FILE) + + if os.path.exists(version_manifest): + # get the local version list + current_versions = MojangIndexWrap(MojangIndex.parse_file(version_manifest)) + local_ids = set(current_versions.versions.keys()) + + # versions not present locally but present remotely are new + pending_ids = remote_ids.difference(local_ids) + + for x in local_ids: + remote_version = remote_versions.versions[x] + local_version = current_versions.versions[x] + if remote_version.time > local_version.time: + pending_ids.add(x) + else: + pending_ids = remote_ids + + # update versions and the linked assets files + assets = {} + for x in pending_ids: + version = remote_versions.versions[x] + print("Updating " + version.id + " to timestamp " + version.releaseTime.strftime('%s')) + asset_id, asset_url = fetch_version_file(os.path.join(UPSTREAM_DIR, VERSIONS_DIR, f"{x}.json"), version.url) + assets[asset_id] = asset_url + + for asset_id, asset_url in assets.items(): + print("assets", asset_id, asset_url) + fetch_file(os.path.join(UPSTREAM_DIR, ASSETS_DIR, f"{asset_id}.json"), asset_url) + + remote_versions.index.write(version_manifest) + -# get the local version list -localVersionlist = None -try: - with open(UPSTREAM_DIR + "/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_DIR + "/mojang/versions/" + id + '.json', version.url) - assets[assetId] = assetUrl - -for assetId, assetUrl in iter(assets.items()): - print("assets", assetId, assetUrl) - get_file(UPSTREAM_DIR + "/mojang/assets/" + assetId + '.json', assetUrl) - -with open(UPSTREAM_DIR + "/mojang/version_manifest_v2.json", 'w', encoding='utf-8') as f: - json.dump(main_json, f, sort_keys=True, indent=4) +if __name__ == '__main__': + main() |
