From 829951d8e7ba114c89370688b5f75fa0bf1cd3af Mon Sep 17 00:00:00 2001 From: ducoterra Date: Sat, 30 Oct 2021 19:19:31 -0400 Subject: [PATCH] init --- Dockerfile | 9 +++++++++ IMAGE | 1 + Makefile | 18 ++++++++++++++++++ README.md | 27 +++++++++++++++++++++++++++ SERVER_VERSION | 1 + VERSION | 1 + get_server.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 99 insertions(+) create mode 100644 Dockerfile create mode 100644 IMAGE create mode 100644 Makefile create mode 100644 README.md create mode 100644 SERVER_VERSION create mode 100644 VERSION create mode 100644 get_server.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..524b825 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:latest + +RUN pip install requests +COPY get_server.py get_server.py +RUN chmod +x get_server.py +WORKDIR /downloads + +CMD [ "/get_server.py" ] +ENTRYPOINT ["python"] diff --git a/IMAGE b/IMAGE new file mode 100644 index 0000000..658e90c --- /dev/null +++ b/IMAGE @@ -0,0 +1 @@ +ducoterra/get-minecraft diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..aa3a1ba --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +SHELL := /bin/bash + +IMAGE ?= $(shell cat IMAGE):$(shell cat VERSION) +IMAGE_LATEST ?= $(shell cat IMAGE):latest + +.PHONY: buildx-context +buildx-context: + docker buildx create --name arm64 --use --platform linux/amd64,linux/arm64,linux/arm/v8 + +.PHONY: build +build: + @docker buildx build --platform linux/amd64,linux/arm64 . -t $(IMAGE) + @docker buildx build --platform linux/amd64,linux/arm64 . -t $(IMAGE_LATEST) + +.PHONY: push +push: + @docker push $(IMAGE) + @docker push $(IMAGE_LATEST) diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa2eddb --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Minecraft Server Getter + +Get the url for any minecraft server version + +## Usage + +### Python + +Since this script is designed to be used in Docker it works with +environment variables. Set SERVER_VERSION= and +get_server.py will write a file called "SERVER_VERSION" with the url +to download the version requested. + +```bash +export SERVER_VERSION=1.16.4 +python get_server.py +``` + +### Docker + +This is the intended way to run get_server. The get_server image saves +the SERVER_VERSION file to /downloads. Mounting /downloads to `pwd` will +allow the image to write to your current directory. + +```bash +docker run -it -e SERVER_VERSION=1.16.4 -v $(pwd):/downloads ducoterra/get-minecraft:latest +``` diff --git a/SERVER_VERSION b/SERVER_VERSION new file mode 100644 index 0000000..d9f6a13 --- /dev/null +++ b/SERVER_VERSION @@ -0,0 +1 @@ +https://launchermeta.mojang.com/v1/packages/584f4f319f0acc2f8c9a7d992ae20b95f425c1c5/1.16.4.json \ No newline at end of file diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..3eefcb9 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.0 diff --git a/get_server.py b/get_server.py new file mode 100644 index 0000000..2eaccc5 --- /dev/null +++ b/get_server.py @@ -0,0 +1,42 @@ +import requests +import json +import os + +LATEST = "latest" +RELEASE = "release" +SNAPSHOT = "snapshot" +REQUESTED_VERSION = os.getenv("SERVER_VERSION", LATEST) +VERSION_FILE = "SERVER_VERSION" + +def get_latest_version(latest): + return latest.get(RELEASE) + +def get_version(versions, latest, id): + if id == LATEST: + id = get_latest_version(latest) + + result = list(filter( + lambda item: item.get('id') == id, + versions + )) + if len(result) == 1: + return result[0] + elif len(result) > 1: + raise ValueError(f"Version {id} didn't return 1 result, it returned {len(result)}!") + else: + raise ValueError(f"Version {id} had no matches.") + +def write_version_file(version): + with open(VERSION_FILE, "w") as f: + return f.write(version.get('url')) + +if __name__ == "__main__": + version_manifest = requests.get("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json") + version_json = json.loads(version_manifest.text) + versions = version_json.get('versions') + latest = version_json.get(LATEST) + + print(f"Attempting to get version {REQUESTED_VERSION}.") + version = get_version(versions, latest, REQUESTED_VERSION) + print(f"Found, writing url to VERSION file.") + write_version_file(version)