-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmc_getjar.py
executable file
·111 lines (101 loc) · 3.43 KB
/
mc_getjar.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""
If the JAR of the current version of Minecraft Java Edition server isn't in ~, download
it, and remove outdated JARs in ~.
"""
import argparse
import pathlib
import re
import sys
import bs4
import requests
import toml
CLOBBER = True
CONFIG_FILE = pathlib.Path("/etc/MCscripts/mc-getjar.toml")
JARS_DIR = pathlib.Path(pathlib.Path.home(), "java_jars")
PARSER = argparse.ArgumentParser(
description=(
"If the JAR of the current version of Minecraft Java Edition server isn't in "
+ "~, download it, and remove outdated JARs in ~."
)
)
CLOBBER_GROUP = PARSER.add_mutually_exclusive_group()
CLOBBER_GROUP.add_argument(
"--clobber", action="store_true", help="remove outdated JARs in ~ (default)"
)
CLOBBER_GROUP.add_argument(
"-n", "--no-clobber", action="store_true", help="don't remove outdated JARs in ~"
)
ARGS = PARSER.parse_args()
if CONFIG_FILE.is_file():
CONFIG = toml.load(CONFIG_FILE)
if "clobber" in CONFIG:
if not isinstance(CONFIG["clobber"], bool):
sys.exit(f"clobber must be TOML boolean, check {CONFIG_FILE}")
CLOBBER = CONFIG["clobber"]
if ARGS.clobber:
CLOBBER = True
elif ARGS.no_clobber:
CLOBBER = False
JARS_DIR.mkdir(parents=True, exist_ok=True)
webpage_res = requests.get(
"https://www.minecraft.net/en-us/download/server",
headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
"Accept-Language": "en-US",
},
timeout=60,
)
webpage_res.raise_for_status()
webpage = bs4.BeautifulSoup(webpage_res.text, "html.parser")
links = webpage.find_all("a")
print(
"Enter Y if you agree to the Minecraft End User License Agreement and Privacy",
"Policy",
)
# Does prompting the EULA seem so official that it violates the EULA?
print("Minecraft End User License Agreement: https://minecraft.net/eula")
print("Privacy Policy: https://go.microsoft.com/fwlink/?LinkId=521839")
if input().lower() != "y":
sys.exit("input != y")
for link in links:
url = link.get("href")
if not url:
continue
url = re.match(r"^https://[^ ]+server\.jar$", url)
if url:
url = url.string
current_ver = link.get_text()
current_ver = pathlib.Path(current_ver).name
break
# Symlink to current jar
if pathlib.Path(JARS_DIR, "current").is_symlink():
INSTALLED_VER = pathlib.Path(JARS_DIR, "current").resolve().name
else:
INSTALLED_VER = None
if not pathlib.Path(JARS_DIR, current_ver).is_file():
jar_res = requests.get(
url,
headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
"Accept-Language": "en-US",
},
timeout=60,
stream=True,
)
jar_res.raise_for_status()
if pathlib.Path(JARS_DIR, current_ver + ".part").is_file():
pathlib.Path(JARS_DIR, current_ver + ".part").unlink()
for chunk in jar_res.iter_content(chunk_size=8192):
pathlib.Path(JARS_DIR, current_ver + ".part").open(mode="ab").write(chunk)
pathlib.Path(JARS_DIR, current_ver + ".part").rename(
pathlib.Path(JARS_DIR, current_ver)
)
if INSTALLED_VER != current_ver:
if pathlib.Path(JARS_DIR, "current").is_symlink():
pathlib.Path(JARS_DIR, "current").unlink()
pathlib.Path(JARS_DIR, "current").symlink_to(pathlib.Path(JARS_DIR, current_ver))
if CLOBBER:
for jarfile in JARS_DIR.glob("minecraft_server.*.jar"):
if not jarfile.samefile(pathlib.Path(JARS_DIR, "current")):
jarfile.unlink()