Last active
April 11, 2020 22:38
-
-
Save Qwerty-Space/d8af26066e5be6dd6abe3967bea398ad to your computer and use it in GitHub Desktop.
Checks current version, and updates bnetlauncher: https://github.com/dafzor/bnetlauncher/releases
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
""" | |
requirements: | |
• requests | |
• PyGitHub: https://github.com/PyGithub/PyGithub | |
""" | |
import os | |
import time | |
import requests | |
from github import Github | |
from zipfile import ZipFile | |
### Setup Variables ### | |
g = Github("") # Put your access token here | |
bnetl = g.get_repo("dafzor/bnetlauncher") | |
latest_release = bnetl.get_latest_release() | |
bnet_asset = latest_release.get_assets()[0] | |
dl_link = bnet_asset.browser_download_url | |
lr_title = latest_release.title | |
game_dir = "G:/Games/Blizzard/Overwatch/" # Change to your desired location | |
lr_file_name = "last_download.txt" | |
lr_file = os.path.join(game_dir, lr_file_name) | |
def unzip(file_name): | |
"""Unzips the executable to the correct location, and deletes the zip file""" | |
with ZipFile(file_name, "r") as zip_file: | |
zip_file.extract("bnetlauncher.exe", game_dir) | |
print(f"Unzipped bnetlauncher.exe from {file_name}") | |
os.remove(file_name) | |
print(f"Deleted {file_name}") | |
print("Finished.") | |
input("Press <ENTER> to exit.") | |
def download_latest(): | |
"""Checks if it's the right asset before downloading. Downloads the file, writes the | |
latest downloaded version to file, then runs unzip()""" | |
if "bnetlauncher" not in bnet_asset.name: | |
return | |
r = requests.get(dl_link, stream=True) | |
file_name = dl_link.split("/")[-1] | |
file_location = os.path.join(game_dir, file_name) | |
print(f"Downloading {lr_title}") | |
with open(file_location, "wb") as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
f.write(chunk) | |
with open(lr_file, "w") as f: | |
f.write(lr_title) | |
print("Downloaded") | |
unzip(file_location) | |
def read_file(): | |
"""Read last_download.txt, and compare it to the latest release on github. If there is a new version out, run download_latest()""" | |
with open(lr_file) as f: | |
read_data = f.read() | |
installed_version = read_data.lstrip("v") | |
latest_version = lr_title.lstrip("v") | |
if not read_data: | |
print("No installed version stored") | |
download_latest() | |
if installed_version < latest_version: | |
print(f"Current installed version: {read_data}") | |
download_latest() | |
elif installed_version > latest_version: | |
print(f"Error! Invalid Version: {read_data}") | |
input("Press <ENTER> to exit.") | |
elif read_data == lr_title: | |
print("You already have the lastest version") | |
time.sleep(15) | |
try: | |
# Try making the file, then download the latest version | |
print(f"Checking for {lr_file}") | |
open(lr_file, "x") | |
print(f"First time running bnetlauncher_updater... Creating {lr_file}") | |
download_latest() | |
except FileExistsError: | |
# If the file already exists, read it | |
read_file() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment