forked from TapeWerm/MCscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcbe_getzip.py
executable file
·121 lines (112 loc) · 3.89 KB
/
mcbe_getzip.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
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
"""
If the ZIP of the current version of Minecraft Bedrock Edition server isn't in ~,
download it, and remove outdated ZIPs in ~.
"""
import argparse
import pathlib
import re
import sys
import bs4
import requests
ZIPS_DIR = pathlib.Path(pathlib.Path.home(), "bedrock_zips")
PARSER = argparse.ArgumentParser(
description="If the ZIP of the current version of Minecraft Bedrock Edition server\
isn't in ~, download it, and remove outdated ZIPs in ~."
)
PARSER.add_argument(
"-n", "--no-clobber", action="store_true", help="don't remove outdated ZIPs in ~"
)
GROUP = PARSER.add_mutually_exclusive_group()
GROUP.add_argument(
"-p",
"--preview",
action="store_true",
help="download preview instead of the current version",
)
GROUP.add_argument(
"-b", "--both", action="store_true", help="download current and preview versions"
)
ARGS = PARSER.parse_args()
if ARGS.both:
VERSIONS = ("current", "preview")
elif ARGS.preview:
VERSIONS = ("preview",)
else:
VERSIONS = ("current",)
ZIPS_DIR.mkdir(parents=True, exist_ok=True)
webpage_res = requests.get(
"https://www.minecraft.net/en-us/download/server/bedrock",
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")
urls = [link.get("href") for link in webpage.find_all("a")]
urls = [url for url in urls if url]
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/terms")
print("Privacy Policy: https://go.microsoft.com/fwlink/?LinkId=521839")
if input().lower() != "y":
sys.exit("input != y")
for version in VERSIONS:
if version == "current":
for urlx in urls:
urlx = re.match(r"^https://[^ ]+bin-linux/bedrock-server-[^ ]+\.zip$", urlx)
if urlx:
url = urlx.string
break
elif version == "preview":
for urlx in urls:
urlx = re.match(
r"^https://[^ ]+bin-linux-preview/bedrock-server-[^ ]+\.zip$", urlx
)
if urlx:
url = urlx.string
break
else:
continue
current_ver = pathlib.Path(url).name
# Symlink to current/preview zip
if pathlib.Path(ZIPS_DIR, version).is_symlink():
INSTALLED_VER = pathlib.Path(ZIPS_DIR, version).resolve().name
else:
INSTALLED_VER = None
if not pathlib.Path(ZIPS_DIR, current_ver).is_file():
zip_res = requests.get(
url,
headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
"Accept-Language": "en-US",
},
timeout=60,
stream=True,
)
zip_res.raise_for_status()
if pathlib.Path(ZIPS_DIR, current_ver + ".part").is_file():
pathlib.Path(ZIPS_DIR, current_ver + ".part").unlink()
for chunk in zip_res.iter_content(chunk_size=8192):
pathlib.Path(ZIPS_DIR, current_ver + ".part").open(mode="ab").write(chunk)
pathlib.Path(ZIPS_DIR, current_ver + ".part").rename(
pathlib.Path(ZIPS_DIR, current_ver)
)
if INSTALLED_VER != current_ver:
if pathlib.Path(ZIPS_DIR, version).is_symlink():
pathlib.Path(ZIPS_DIR, version).unlink()
pathlib.Path(ZIPS_DIR, version).symlink_to(pathlib.Path(ZIPS_DIR, current_ver))
if not ARGS.no_clobber:
for zipfile in ZIPS_DIR.glob("bedrock-server-*.zip"):
try:
if not zipfile.samefile(
pathlib.Path(ZIPS_DIR, "current")
) and not zipfile.samefile(pathlib.Path(ZIPS_DIR, "preview")):
zipfile.unlink()
except FileNotFoundError:
pass