add dynmap update script
This commit is contained in:
55
python/dynmap-update-checker.py
Normal file
55
python/dynmap-update-checker.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
def must_get_env(name):
|
||||
v = os.getenv(name)
|
||||
if not v:
|
||||
raise Exception(f"missing environment variable '{name}'")
|
||||
return v
|
||||
|
||||
|
||||
PROJECT = must_get_env("PROJECT")
|
||||
VERSION = must_get_env("VERSION")
|
||||
NTFY_ADDRESS = must_get_env("NTFY_ADDRESS")
|
||||
NTFY_TOKEN = must_get_env("NTFY_TOKEN")
|
||||
|
||||
|
||||
def split_at(s, sep):
|
||||
split = s.split(sep, 1)
|
||||
split.extend([None, None][:2-len(split)])
|
||||
return split
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
versions = requests.get(f"https://api.modrinth.com/v2/project/{PROJECT}/version").json()
|
||||
|
||||
(version, loader) = split_at(VERSION, '@')
|
||||
|
||||
v = find_version(versions, version, loader)
|
||||
|
||||
if v:
|
||||
res = requests.put(
|
||||
NTFY_ADDRESS,
|
||||
headers={
|
||||
"Authorization": f"Bearer {NTFY_TOKEN}",
|
||||
"Title": f"{PROJECT} update available!",
|
||||
},
|
||||
data=f"Version {version} is now available for {PROJECT}!")
|
||||
print(res)
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def find_version(versions, desired_version, loader):
|
||||
for version in versions:
|
||||
if desired_version not in version.get("game_versions"):
|
||||
continue
|
||||
if loader and loader not in version.get("loaders"):
|
||||
continue
|
||||
return version
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user