Skip to content

Instantly share code, notes, and snippets.

@AdamG100
Created February 22, 2024 08:51
Show Gist options
  • Save AdamG100/ac1906d53194796db44dd86234ff184c to your computer and use it in GitHub Desktop.
Save AdamG100/ac1906d53194796db44dd86234ff184c to your computer and use it in GitHub Desktop.
Minecraft bedrock Download
import re, urllib2, json
import clr
clr.AddReference('TCAdmin.GameHosting.SDK')
from TCAdmin.GameHosting.SDK.Objects import GameUpdate, GameScript, ServiceEvent
gameID = 193
scripts = [
{
"Name": "Backup server.properties",
"Event": ServiceEvent.BeforeUpdateInstall,
'Contents': "from os import path, rename, remove\nserverproperties = path.join(ThisService.RootDirectory, 'server.properties')\nif path.isfile(serverproperties):\n if path.isfile(serverproperties+'.backup'):\n remove(serverproperties+'.backup')\n rename(serverproperties, serverproperties+'.backup')"
},
{
"Name": "Delete new server.properties, use old file instead",
"Event": ServiceEvent.AfterUpdateInstall,
"Contents": "from os import path, rename, remove\nserverproperties = path.join(ThisService.RootDirectory, 'server.properties')\nif path.isfile(serverproperties):\n remove(serverproperties)\nrename(serverproperties+'.backup', serverproperties)"
}
]
updates = GameUpdate.GetUpdates(gameID)
version_page = urllib2.urlopen('https://minecraft.fandom.com/wiki/Bedrock_Dedicated_Server').read()
regex_pattern = '<a target=\"_self\" rel=\"nofollow\" class=\"external text\" href=\"https:\/\/minecraft\.azureedge\.net\/bin-win(\-preview)?\/bedrock-server-(?<Version>.*)\.zip\">Windows<\/a>'
matches = re.findall(regex_pattern, version_page)
if matches.Count > 0:
for version in matches:
release = version[0] # '' or '-preview'
version = version[1]
#Generate view order
parts = version.split('.')
for i in range(4):
parts[i] = parts[i].zfill(3)
view_order=-int(''.join(parts))
#Skip if an update already exists with the same name
skip = False
for gameUpdate in updates:
if gameUpdate.Name == "Bedrock Edition "+version:
Script.WriteToConsole('Skipping version {}'.format(version))
skip = True
if not skip:
update = GameUpdate()
update.Name = "Bedrock Edition "+version
update.Comments = ""
update.ImageUrl = "https://www.minecraft.net/etc.clientlibs/minecraft/clientlibs/main/resources/img/header/logo.png"
update.GameId = gameID
update.GroupName = "Minecraft: Bedrock Edition" if release == '' else "Minecraft: Bedrock Edition Preview"
update.ExtractPath = "/"
update.WindowsFileName = "https://minecraft.azureedge.net/bin-win{}/bedrock-server-{}.zip".format(release, version)
update.LinuxFileName = "https://minecraft.azureedge.net/bin-linux{}/bedrock-server-{}.zip".format(release, version)
update.Reinstallable = True
update.DefaultInstall = False
update.UserAccess = True
update.SubAdminAccess = True
update.ResellerAccess = True
update.ViewOrder = view_order
update.GenerateKey()
update.Save()
Script.WriteToConsole("Added update for Minecraft: Bedrock Edition v{}".format(version))
#Add custom scripts
for script in scripts:
updateScript = GameScript()
updateScript.Description = script["Name"]
updateScript.ScriptContents = script["Contents"]
updateScript.ServiceEvent = script["Event"]
updateScript.ExecuteAsServiceUser = False
updateScript.ExecuteInPopup = False
updateScript.ExecutionOrder = 0
updateScript.GameId = gameID
updateScript.IgnoreErrors = False
updateScript.OperatingSystem = 0 #0 = Any, 2 = Windows, 4 = Linux
updateScript.PromptVariables = False
updateScript.UserAccess = True
updateScript.SubAdminAccess = True
updateScript.ResellerAccess = True
updateScript.ScriptEngineId = 1 #IronPython
updateScript.UpdateId = update.UpdateId
updateScript.GenerateKey()
updateScript.Save()
#Change the links for the "Latest" update
gameUpdates = set(updates)
for update in gameUpdates:
if 'Latest Bedrock Update' in update.Notes:
Script.WriteToConsole('Changing the links for the latest Bedrock update')
update.WindowsFileName = "https://minecraft.azureedge.net/bin-win/bedrock-server-{}.zip".format(version)
update.LinuxFileName = "https://minecraft.azureedge.net/bin-linux/bedrock-server-{}.zip".format(version)
update.Save()
@YawnyNZ
Copy link

YawnyNZ commented Apr 5, 2025

Fandom wiki is no longer regularly updated.
Unfortunately the other wiki blocks urllib2: https://minecraft.wiki/w/Bedrock_Dedicated_Server
You can use the official site and replace urllib2 with .NET HttpWebRequest

import re, clr
clr.AddReference('System')
clr.AddReference('TCAdmin.GameHosting.SDK')
from System.Net import HttpWebRequest
from System.IO import StreamReader
from TCAdmin.GameHosting.SDK.Objects import GameUpdate, GameScript, ServiceEvent

gameID = 193
scripts = [
    {
        "Name": "Backup server.properties",
        "Event": ServiceEvent.BeforeUpdateInstall,
        'Contents': (
            "from os import path, rename, remove\n"
            "serverproperties = path.join(ThisService.RootDirectory, 'server.properties')\n"
            "if path.isfile(serverproperties):\n"
            "  if path.isfile(serverproperties+'.backup'):\n"
            "    remove(serverproperties+'.backup')\n"
            "  rename(serverproperties, serverproperties+'.backup')"
        )
    },
    {
        "Name": "Delete new server.properties, use old file instead",
        "Event": ServiceEvent.AfterUpdateInstall,
        "Contents": (
            "from os import path, rename, remove\n"
            "serverproperties = path.join(ThisService.RootDirectory, 'server.properties')\n"
            "if path.isfile(serverproperties):\n"
            "  remove(serverproperties)\n"
            "rename(serverproperties+'.backup', serverproperties)"
        )
    }
]

# Fetch existing updates
updates = GameUpdate.GetUpdates(gameID)

# Fetch version page using .NET HttpWebRequest
try:
    url = "https://www.minecraft.net/en-us/download/server/bedrock"
    request = HttpWebRequest.Create(url)
    request.UserAgent = "Mozilla/5.0"
    response = request.GetResponse()
    stream = response.GetResponseStream()
    reader = StreamReader(stream)
    html = reader.ReadToEnd()
except Exception as ex:
    Script.WriteToConsole("Failed to fetch update page: {}".format(str(ex)))
    raise

# Extract versions from the HTML
regex_pattern = r'https://www\.minecraft\.net/bedrockdedicatedserver/bin-win/bedrock-server-(?P<version>[\d\.]+)\.zip'
matches = re.findall(regex_pattern, html)

if len(matches) > 0:
    for version in matches:
        release = ''  # No preview flag in this format

        # Generate view order
        parts = version.split('.')
        for i in range(4):
            parts[i] = parts[i].zfill(3)
        view_order = -int(''.join(parts))

        # Check if update already exists
        skip = False
        for gameUpdate in updates:
            if gameUpdate.Name == "Bedrock Edition " + version:
                Script.WriteToConsole('Skipping version {}'.format(version))
                skip = True
                break

        if not skip:
            update = GameUpdate()
            update.Name = "Bedrock Edition " + version
            update.Comments = ""
            update.ImageUrl = "https://www.minecraft.net/etc.clientlibs/minecraft/clientlibs/main/resources/img/header/logo.png"
            update.GameId = gameID
            update.GroupName = "Minecraft: Bedrock Edition"
            update.ExtractPath = "/"
            update.WindowsFileName = "https://www.minecraft.net/bedrockdedicatedserver/bin-win/bedrock-server-{}.zip".format(version)
            update.LinuxFileName = "https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-{}.zip".format(version)
            update.Reinstallable = True
            update.DefaultInstall = False
            update.UserAccess = True
            update.SubAdminAccess = True
            update.ResellerAccess = True
            update.ViewOrder = view_order
            update.GenerateKey()
            update.Save()
            Script.WriteToConsole("Added update for Minecraft: Bedrock Edition v{}".format(version))

            # Attach the event scripts
            for script in scripts:
                updateScript = GameScript()
                updateScript.Description = script["Name"]
                updateScript.ScriptContents = script["Contents"]
                updateScript.ServiceEvent = script["Event"]
                updateScript.ExecuteAsServiceUser = False
                updateScript.ExecuteInPopup = False
                updateScript.ExecutionOrder = 0
                updateScript.GameId = gameID
                updateScript.IgnoreErrors = False
                updateScript.OperatingSystem = 0  # 0 = Any
                updateScript.PromptVariables = False
                updateScript.UserAccess = True
                updateScript.SubAdminAccess = True
                updateScript.ResellerAccess = True
                updateScript.ScriptEngineId = 1  # IronPython
                updateScript.UpdateId = update.UpdateId
                updateScript.GenerateKey()
                updateScript.Save()

            # Update latest pointer if needed
            for existing in updates:
                if 'Latest Bedrock Update' in existing.Notes:
                    Script.WriteToConsole('Changing the links for the latest Bedrock update')
                    existing.WindowsFileName = "https://www.minecraft.net/bedrockdedicatedserver/bin-win/bedrock-server-{}.zip".format(version)
                    existing.LinuxFileName = "https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-{}.zip".format(version)
                    existing.Save()
else:
    Script.WriteToConsole("No versions found.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment