-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcmirror.py
145 lines (114 loc) · 3.92 KB
/
mcmirror.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python
"""
MCMirror
Method from: http://wiki.vg/Game_Files
This will download all of the Minecraft files, including
libraries, assets, etc. for ALL of the versions, laying them
out in the same directory structure as on Mojang's (or M$'s)
servers. This is useful for having a local copy, as you can
just start a web server with the files, and redirect the domains.
"""
import urllib2, json, os
def mkdir_p(dir):
try:
os.makedirs(dir)
except OSError as e:
pass
def write_file(path, content):
mkdir_p(os.path.dirname(path))
f = open(path, "wb")
f.write(content)
f.close()
def download_jars(versionList, outNatives, outAssets):
for ver in versionList:
outClient = "s3.amazonaws.com/Minecraft.Download/versions/%s/%s.jar" % (ver, ver)
outServerJar = "s3.amazonaws.com/Minecraft.Download/versions/%s/minecraft_server.%s.jar" % (ver, ver)
outServerExe = "s3.amazonaws.com/Minecraft.Download/versions/%s/minecraft_server.%s.exe" % (ver, ver)
outInfoJson = "s3.amazonaws.com/Minecraft.Download/versions/%s/%s.json" % (ver, ver)
print "Downloading http://%s" % outClient
try:
clientJar = urllib2.urlopen("http://" + outClient).read()
write_file(outClient, clientJar)
except urllib2.HTTPError as e:
print e
print "Downloading http://%s" % outServerJar
try:
serverJar = urllib2.urlopen("http://" + outServerJar).read()
write_file(outServerJar, serverJar)
except urllib2.HTTPError as e:
print e
print "Downloading http://%s" % outServerExe
try:
serverExe = urllib2.urlopen("http://" + outServerExe).read()
write_file(outServerExe, serverExe)
except urllib2.HTTPError as e:
print e
print "Downloading http://%s" % outInfoJson
try:
infoJson = urllib2.urlopen("http://" + outInfoJson).read()
write_file(outInfoJson, infoJson)
except urllib2.HTTPError as e:
print e
info = json.loads(infoJson)
outNatives.append(info["libraries"])
outAssets.add(info.get("assets", "legacy"))
def download_libraries(verLibs):
libList = set()
for libs in verLibs:
for lib in libs:
package, name, version = lib["name"].split(":")
preUrl = "libraries.minecraft.net/%s/%s/%s/%s-%s" % (package.replace(".", "/"), name, version, name, version)
if "natives" in lib:
for plat in lib["natives"]:
natString = lib["natives"][plat]
for arch in ["32", "64"]:
postUrl = preUrl + "-" + natString.replace("${arch}", arch)
else:
postUrl = preUrl
libList.add(postUrl + ".jar")
for url in libList:
lib = "https://" + url
sha1 = lib + ".sha1"
print "Downloading %s" % sha1
try:
rawSha = urllib2.urlopen(sha1).read()
write_file(url + ".sha1", rawSha)
except urllib2.HTTPError as e:
print e
print "Downloading %s" % lib
try:
rawLib = urllib2.urlopen(lib).read()
write_file(url, rawLib)
except urllib2.HTTPError as e:
print e
def download_assets(assetList):
hashes = set()
# Get the indexes
for idx in assetList:
path = "s3.amazonaws.com/Minecraft.Download/indexes/%s.json" % idx;
# Save the index file
print "Downloading http://%s" % path
try:
raw = urllib2.urlopen("http://" + path).read()
write_file(path, raw)
except urllib2.HTTPError as e:
print e
# Now parse it and get all the hashes
idxHash = json.loads(raw)
for i in idxHash["objects"]:
hashes.add(idxHash["objects"][i]["hash"])
# Now download all the files
for hash in hashes:
dir = "resources.download.minecraft.net/%s/%s" % (hash[0:2], hash)
print "Downloading https://%s" % dir
raw = urllib2.urlopen("https://" + dir).read()
write_file(dir, raw)
if __name__ == "__main__":
versionsRaw = urllib2.urlopen("http://s3.amazonaws.com/Minecraft.Download/versions/versions.json").read()
write_file("s3.amazonaws.com/Minecraft.Download/versions/versions.json", versionsRaw)
versionList = [v["id"] for v in json.loads(versionsRaw)["versions"]]
libs = []
assets = set()
download_jars(versionList, libs, assets)
download_libraries(libs)
download_assets(assets)