Last active
April 7, 2019 07:00
-
-
Save GoobyCorp/47d70e05cc54114216232c780fecf124 to your computer and use it in GitHub Desktop.
A script to make updating SX OS (even) easier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
################################################ | |
# SX OS updater by Visual Studio @ Gooby Corp. # | |
################################################ | |
import re | |
from sys import stdout | |
from zipfile import ZipFile | |
from os.path import isfile, isdir | |
from argparse import ArgumentParser | |
from os import remove, rename, linesep | |
import requests | |
#I/O | |
BUFF_SIZE = 4096 | |
#network | |
BASE_URL = "https://sx.xecuter.rocks" | |
SX_LOADER_URL = BASE_URL + "/download/payload.bin" | |
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" | |
#files | |
PAYLOAD_FILE = "payload.bin" | |
SX_OS_ZIP_FILE = "SX_OS.zip" | |
SX_OS_FILE = "boot.dat" | |
#regex | |
RELEASE_FILE_VERSION_EXP = re.compile(r"/download/SXOS_v([\d\w.]{0,}?)\.zip") | |
BETA_FILE_VERSION_EXP = re.compile(r"/download/SXOS_beta_v([\d\w.]{0,}?)\.zip") | |
def calc_percentage(current: int, total: int) -> int: | |
remaining = total - current | |
return int(100 - (remaining / total * 100.0)) | |
def guess_latest_version(beta: bool) -> (str, None): | |
resp = requests.get(BASE_URL, headers={"User-Agent": USER_AGENT}).text | |
if beta: | |
match = BETA_FILE_VERSION_EXP.search(resp) | |
else: | |
match = RELEASE_FILE_VERSION_EXP.search(resp) | |
if match: | |
return match.group(1) | |
def download_file(url: str, path: str) -> None: | |
resp = requests.get(url, headers={"User-Agent": USER_AGENT}, stream=True) | |
# file and progress vars | |
downloaded = 0 | |
last_percentage = None | |
update_percent = 5 | |
file_name = url.split("/")[-1] | |
file_size = int(resp.headers.get("Content-Length")) | |
with open(path, "wb") as f: | |
for block in resp.iter_content(BUFF_SIZE): | |
if block: | |
# write the block to the file | |
f.write(block) | |
# track progress (the SX OS download takes a while) | |
downloaded += len(block) | |
percentage = calc_percentage(downloaded, file_size) | |
if percentage % update_percent == 0 and percentage != last_percentage: # update the progress bar every 5% | |
num_progress = int(percentage / update_percent) | |
padding = int(100 / update_percent) - num_progress | |
#stdout.write("\r%s/%s (%s%%)" % (downloaded, file_size, percentage)) | |
stdout.write(("\r0%% <" + ("=" * num_progress) + (" " * padding) + "> 100%% - %s%% - %s") % (percentage, file_name)) | |
last_percentage = percentage | |
if percentage == 100: # start a new line when it's at 100% | |
stdout.write(linesep) | |
def extract_os_zip(zip_path: str, extract_path: str) -> None: | |
with ZipFile(zip_path) as z: | |
z.extract(SX_OS_FILE, extract_path) | |
if __name__ == "__main__": | |
parser = ArgumentParser(description="A tool to make downloading SX OS updates easier") | |
parser.add_argument("-o", "--out-path", default="./", type=str, help="The directory to extract the SX OS file to") | |
parser.add_argument("-p", "--payload", action="store_true", help="Whether or not you want to download the payload if you don't have a SX Pro") | |
parser.add_argument("-b", "--beta", action="store_true", default=True, help="Download betas") | |
args = parser.parse_args() | |
# make sure the output directory exists | |
assert isdir(args.out_path), "The output directory doesn't exist!" | |
# get the latest version from the site | |
latest_version = guess_latest_version(args.beta) | |
# create the file name for the SX OS zip | |
if args.beta: | |
sx_os_file = "SXOS_beta_v%s.zip" % (latest_version) | |
else: | |
sx_os_file = "SXOS_v%s.zip" % (latest_version) | |
# create a URL to download it from | |
sx_os_url = BASE_URL + "/download/" + sx_os_file | |
# download the payload file if it doesn't exist | |
if args.payload: | |
download_file(SX_LOADER_URL, PAYLOAD_FILE) | |
# download, extract, and delete the SX OS archive | |
if not isfile(SX_OS_FILE): | |
download_file(sx_os_url, SX_OS_ZIP_FILE) | |
extract_os_zip(SX_OS_ZIP_FILE, args.out_path) | |
remove(SX_OS_ZIP_FILE) | |
if args.beta: | |
rename(SX_OS_FILE, "boot_beta_v%s.dat" % (latest_version.replace(".", "_"))) | |
else: | |
rename(SX_OS_FILE, "boot_v%s.dat" % (latest_version.replace(".", "_"))) | |
# all finished | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment