Created
February 19, 2020 03:00
-
-
Save MarkKoz/96443eb290075406a57e9620e15d6212 to your computer and use it in GitHub Desktop.
Downloads the latest binary for BnS Buddy, bypassing the updater application.
This file contains 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
import shutil | |
import subprocess | |
from pathlib import Path | |
from tempfile import NamedTemporaryFile, TemporaryDirectory | |
import requests | |
UNRAR_PATH = r"C:\Program Files\WinRAR\UnRAR.exe" | |
def version_exists(version: str) -> bool: | |
files = Path(".").glob(f"Bns Buddy {version}.exe") | |
if next(files, None): | |
return True | |
else: | |
return False | |
def main(): | |
with requests.Session() as session: | |
session.headers.update({"User-Agent": "BnSBuddy/2.5.4.0 (compatible;)"}) | |
# Get the version so it can be used in the file name. | |
response = session.get("https://updates.bnsbuddy.com/BuddyVersion.txt") | |
response.raise_for_status() | |
version = response.text | |
if version_exists(version): | |
print(f"Version {version} already exists locally.") | |
return | |
# Get the rar file. | |
url = "https://updates.bnsbuddy.com/BnS%20Buddy%20[By%20Kogaru].rar" | |
response = session.get(url) | |
response.raise_for_status() | |
extract_file(response.content, "BnS Buddy.exe", f"Bns Buddy {version}.exe") | |
def extract_file(rar_archive: bytes, file_name: str, dest: str) -> None: | |
"""Extract the named file from a RAR archive.""" | |
with NamedTemporaryFile(delete=False, suffix=".rar") as tf, TemporaryDirectory() as td: | |
# Write archive to a temp file because the unrar tool doesn't support | |
# piping it in. | |
tf.write(rar_archive) | |
tf.close() | |
# Extract the file to a temporary directory. | |
args = (UNRAR_PATH, "e", "-idq", tf.name, file_name, td) | |
proc = subprocess.run(args, stderr=subprocess.PIPE, text=True) | |
print(proc.stderr) | |
proc.check_returncode() | |
# Move the file from the temp dir to the destination. | |
shutil.move(Path(td, file_name), dest) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment