Skip to content

Instantly share code, notes, and snippets.

@z0z0r4
Last active January 6, 2023 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z0z0r4/97edd9cc8d62301f7c4706268ee0eb49 to your computer and use it in GitHub Desktop.
Save z0z0r4/97edd9cc8d62301f7c4706268ee0eb49 to your computer and use it in GitHub Desktop.
Minecraft mirror for server
from distutils import log
import os
from tkinter import E
import multitasking
import signal
import requests
import logging
import time
import datetime
import re
import shutil
signal.signal(signal.SIGINT, multitasking.killall)
requests.packages.urllib3.disable_warnings()
# 日志
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
logging.basicConfig(filename=str(datetime.date.today())+'.log', level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
# 文件夹初始化
def initfolder():
print("path: " + str(os.path.realpath(".")))
if not os.path.exists("papermc"):
os.makedirs("papermc")
if not os.path.exists("papermc/paper"):
os.makedirs("papermc/paper")
if not os.path.exists("papermc/waterfull"):
os.makedirs("papermc/waterfull")
if not os.path.exists("papermc/velocity"):
os.makedirs("papermc/velocity")
if not os.path.exists("spigot"):
os.makedirs("spigot")
if os.path.exists("bungeecord"):
shutil.rmtree("bungeecord")
os.makedirs("bungeecord")
if not os.path.exists("craftbukkit"):
os.makedirs("craftbukkit")
if not os.path.exists("vanilla"):
os.makedirs("vanilla")
if not os.path.exists("vanilla/server"):
os.makedirs("vanilla/server")
if not os.path.exists("vanilla/client"):
os.makedirs("vanilla/client")
if not os.path.exists("mohistmc"):
os.makedirs("mohistmc")
if not os.path.exists("sponge"):
os.makedirs("sponge")
if not os.path.exists("catserver"):
os.makedirs("catserver")
logging.info("init folders")
def reget(url):
proxies = {"http":"http://127.0.0.1:7890","https":"http://127.0.0.1:7890"}
#proxies = {"http":None,"https":None}
for i in range(3):
res = requests.get(url, verify=False,proxies=proxies)
if res.status_code == 200:
return res
return None
@multitasking.task #多线程用
def tfilereget(url, filename):
res = reget(url)
if res is None:
return False
d = os.path.dirname(filename)
if not os.path.exists(d):
os.makedirs(d)
with open(filename, "wb") as f:
for c in res.iter_content(chunk_size=1024 * 128): # 128KB
f.write(c)
return True
def filereget(url, filename):
res = reget(url)
if res is None:
return False
d = os.path.dirname(filename)
if not os.path.exists(d):
os.makedirs(d)
with open(filename, "wb") as f:
for c in res.iter_content(chunk_size=1024 * 128): # 128KB
f.write(c)
return True
def getvanilla(): #vanilla
start_time = time.time()
MCserver_url = 'https://download.mcbbs.net/mc/game/version_manifest.json'
# 获取URL
server_json = reget(MCserver_url).json()
# 遍历Json
for val in server_json['versions']:
# 获取URL 替换为 bmclapi源
download_json_url = val['url'].replace("launchermeta.mojang.com", 'download.mcbbs.net')
# 获取版本Json
download_server_json = reget(download_json_url).json()
# server download
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ' 开始下载 URL: ' +
download_server_json['downloads']['server']['url'].replace('https://launcher.mojang.com',
'https://download.mcbbs.net'))
if 'server' in download_server_json['downloads'].keys():
tfilereget(download_server_json['downloads']['server']['url'].replace("launchermeta.mojang.com",
'download.mcbbs.net'),'vanilla/server/{0}.jar'.format(val['id']))
# client download
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ' 开始下载 URL: ' +
download_server_json['downloads']['client']['url'].replace('https://launcher.mojang.com',
'https://download.mcbbs.net'))
if 'client' in download_server_json['downloads'].keys():
tfilereget(download_server_json['downloads']['client']['url'].replace('https://launcher.mojang.com',
'https://download.mcbbs.net'),'vanilla/client/{0}.jar'.format(val['id']))
multitasking.wait_for_tasks()
end_time = time.time()
print('vanilla 耗时: {:.2f}秒'.format(end_time - start_time))
def getpapermc(): #papermc
baseapi = "https://papermc.io/api/v2/projects/"
projects = [
"paper",
"waterfall",
"velocity"]
start_time = time.time()
for project in projects: #遍历每个project
projectapi = baseapi + project + "/"
versions = reget(projectapi).json()["versions"] #获取project的版本列表
for version in versions: #遍历每个版本
jarfilelist = reget(f"https://papermc.io/api/v2/projects/{project}/versions/{version}").json()["builds"] #每个mc版本的build号列表
jarfilename = reget(f"https://papermc.io/api/v2/projects/{project}/versions/{version}/builds/" + str(jarfilelist[-1])).json()["downloads"]["application"]["name"] #每个mc版本的最新build名字
jarfileurl = f"https://papermc.io/api/v2/projects/{project}/versions/{version}/builds/{str(jarfilelist[-1])}/downloads/{jarfilename}"
tfilereget(jarfileurl, os.path.join("papermc", project, jarfileurl.rsplit('/', 1)[-1]))
multitasking.wait_for_tasks()
end_time = time.time()
print('papermc 耗时: {:.2f}秒'.format(end_time - start_time))
def getspigot(): #spigot
start_time = time.time()
getbukkiturl = "https://getbukkit.org/download/spigot"
html = reget(getbukkiturl).text
pattren = re.compile(r'getbukkit.org/get/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]')
url_lst = pattren.findall(html)
for versionurl in url_lst:
html = reget("http://"+versionurl).text
pattren = re.compile(r'[download | cdn]+.getbukkit.org/spigot/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]')
version_url_lst = pattren.findall(html)
print(version_url_lst[0])
tfilereget("http://"+version_url_lst[0],"spigot/"+version_url_lst[0].rsplit('/', 1)[-1])
multitasking.wait_for_tasks()
end_time = time.time()
print('spigot 耗时: {:.2f}秒'.format(end_time - start_time))
def getcraftbukkit():
start_time = time.time()
getbukkiturl = "https://getbukkit.org/download/craftbukkit"
html = reget(getbukkiturl).text
pattren = re.compile(r'getbukkit.org/get/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]')
url_lst = pattren.findall(html)
for versionurl in url_lst:
html = reget("http://"+versionurl).text
pattren = re.compile(r'[download | cdn]+.getbukkit.org/craftbukkit/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]')
version_url_lst = pattren.findall(html)
print(version_url_lst[0])
tfilereget("http://"+version_url_lst[0],"craftbukkit/"+version_url_lst[0].rsplit('/', 1)[-1])
multitasking.wait_for_tasks()
end_time = time.time()
print('craftbukkit 耗时: {:.2f}秒'.format(end_time - start_time))
def getBungeecord(): #bungeecord
start_time = time.time()
jenkins_api = "https://ci.md-5.net/job/BungeeCord/api/json"
api_list = reget(jenkins_api).json()["builds"]
for n in range(0,10):
build_No = api_list[n]["url"].rsplit('/', -1)[-2]
os.makedirs(os.path.join("bungeecord",api_list[n]["url"].rsplit('/', -1)[-2]))
build_api = api_list[n]["url"] + "api/json"
build_list = reget(build_api).json()["artifacts"]
for jarinfo in build_list:
filereget(api_list[n]["url"]+"artifact/"+jarinfo["relativePath"],os.path.join("bungeecord", build_No, jarinfo["fileName"]))
multitasking.wait_for_tasks()
end_time = time.time()
print('bungeecord 耗时: {:.2f}秒'.format(end_time - start_time))
def getmohist(): #mohist
start_time = time.time()
api_url = "https://mohistmc.com/api/"
versions_url = "https://mohistmc.com/api/versions/"
versions_json = reget(versions_url).json()
for version in versions_json:
versionjar_url = api_url + version + "/latest/download"
versionjar_json = reget(api_url+version+"/latest").json()
if versionjar_json["status"] == "SUCCESS":
print(versionjar_json["name"])
tfilereget(versionjar_url,os.path.join("mohistmc",versionjar_json["name"]))
else:
print("not SUCCESS")
multitasking.wait_for_tasks()
end_time = time.time()
print('mohist 耗时: {:.2f}秒'.format(end_time - start_time))
def getsponge():
start_time = time.time()
group_api = "https://dl-api-new.spongepowered.org/api/v2/groups/org.spongepowered/artifacts/"
group_name_list = reget(group_api[:-1]).json()["artifactIds"]
for group in group_name_list: #获取group的version列表
group_versions = reget(group_api+group).json()["tags"]["minecraft"]
print(group_versions)
for version in group_versions: #根据version获得build列表
version_info = reget(group_api+group+"/versions?offset=0&limit=1&tags=minecraft:"+version)
if version_info != None:
build_name_list = version_info.json()["artifacts"] #是正式版则获取build列表
build_name = next(iter(build_name_list))
print("get "+build_name+"\n")
if not os.path.exists("sponge/"+version):
os.makedirs("sponge/"+version)
build_jar_list= reget(group_api+group+"/versions/"+build_name).json()["assets"]
for build_jar_dict in build_jar_list:
build_jar_url = build_jar_dict["downloadUrl"]
print("get "+build_jar_url)
tfilereget(build_jar_url,os.path.join("sponge", version,build_jar_url.rsplit('/', 1)[-1]))
multitasking.wait_for_tasks()
end_time = time.time()
print('sponge 耗时: {:.2f}秒'.format(end_time - start_time))
def getcatserver():
start_time = time.time()
url = "https://catmc.org/download/universal"
name = reget(url).url
print(name)
filereget(url,"catserver/"+name.rsplit('/', 1)[-1]+".jar")
end_time = time.time()
print('catserver 耗时: {:.2f}秒'.format(end_time - start_time))
initfolder()
getcatserver()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment