diff --git a/clashroyalebuildabot/state/updater.py b/clashroyalebuildabot/state/updater.py deleted file mode 100644 index 8b9856a..0000000 --- a/clashroyalebuildabot/state/updater.py +++ /dev/null @@ -1,82 +0,0 @@ -import requests -import os -import zipfile -import shutil -from loguru import logger - -GITHUB_REPO = "Pbatch/ClashRoyaleBuildABot" -LOCAL_VERSION = "3b1fb24a35f664fcab4ada324ea4ee4a84084742" -DOWNLOAD_PATH = "update.zip" -EXTRACT_PATH = "." - -def get_latest_commit(): - url = f"https://api.github.com/repos/{GITHUB_REPO}/commits/main" - response = requests.get(url) - response.raise_for_status() - return response.json()["sha"] - -def download_update(commit_sha): - url = f"https://github.com/{GITHUB_REPO}/archive/{commit_sha}.zip" - response = requests.get(url) - response.raise_for_status() - with open(DOWNLOAD_PATH, "wb") as file: - file.write(response.content) - -def extract_update(): - with zipfile.ZipFile(DOWNLOAD_PATH, "r") as zip_ref: - zip_ref.extractall(EXTRACT_PATH) - -def replace_old_version(commit_sha): - new_folder_name = f"{GITHUB_REPO.split('/')[-1]}-{commit_sha}" - new_folder_path = os.path.join(EXTRACT_PATH, new_folder_name) - - if os.path.exists(new_folder_path): - logger.info(f"Replacing old version with new version: {new_folder_name}") - for item in os.listdir(EXTRACT_PATH): - item_path = os.path.join(EXTRACT_PATH, item) - if item != new_folder_name and item != "bot.log": - if os.path.isdir(item_path): - shutil.rmtree(item_path) - else: - os.remove(item_path) - - for item in os.listdir(new_folder_path): - shutil.move(os.path.join(new_folder_path, item), EXTRACT_PATH) - - shutil.rmtree(new_folder_path) - -def update_local_version(new_version): - global LOCAL_VERSION - LOCAL_VERSION = new_version - with open(__file__, "r") as file: - lines = file.readlines() - with open(__file__, "w") as file: - for line in lines: - if line.startswith("LOCAL_VERSION"): - file.write(f'LOCAL_VERSION = "{new_version}"\n') - else: - file.write(line) - -def check_for_update(): - logger.info("Checking for updates...") - latest_commit_sha = get_latest_commit() - logger.info(f"Local version: {LOCAL_VERSION}") - logger.info(f"Latest commit SHA: {latest_commit_sha}") - - if LOCAL_VERSION != latest_commit_sha: - user_input = input(f"A new update is available. Do you want to update? (Y/N): ") - if user_input.lower() == 'y': - logger.info("Downloading update...") - download_update(latest_commit_sha) - logger.info(f"Extracting update from {DOWNLOAD_PATH}...") - extract_update() - logger.info("Replacing old version with new version...") - replace_old_version(latest_commit_sha) - update_local_version(latest_commit_sha) - if os.path.exists(DOWNLOAD_PATH): - os.remove(DOWNLOAD_PATH) - logger.info("Update successful!") - else: - logger.info("Update canceled.") - else: - logger.info("You are already using the latest version.") diff --git a/clashroyalebuildabot/updater.py b/clashroyalebuildabot/updater.py new file mode 100644 index 0000000..a6010c1 --- /dev/null +++ b/clashroyalebuildabot/updater.py @@ -0,0 +1,100 @@ +import os +import shutil +import subprocess +import zipfile + +from loguru import logger +import requests + + +class Updater: + GITHUB_REPO = "Pbatch/ClashRoyaleBuildABot" + DOWNLOAD_PATH = "update.zip" + EXTRACT_PATH = "." + + @staticmethod + def _get_current_sha(): + result = subprocess.run( + ["git", "rev-parse", "HEAD"], + capture_output=True, + text=True, + check=True, + ) + sha = result.stdout.strip() + return sha + + def _get_latest_sha(self): + url = f"https://api.github.com/repos/{self.GITHUB_REPO}/commits/main" + response = requests.get(url, timeout=300) + response.raise_for_status() + sha = response.json()["sha"] + return sha + + def _download_update(self, commit_sha): + url = f"https://github.com/{self.GITHUB_REPO}/archive/{commit_sha}.zip" + response = requests.get(url, timeout=300) + response.raise_for_status() + with open(self.DOWNLOAD_PATH, "wb") as file: + file.write(response.content) + + def _extract_update(self): + with zipfile.ZipFile(self.DOWNLOAD_PATH, "r") as zip_ref: + zip_ref.extractall(self.EXTRACT_PATH) + + def _replace_old_version(self, commit_sha): + new_folder_name = ( + f"{self.GITHUB_REPO.rsplit('/', maxsplit=1)[-1]}-{commit_sha}" + ) + new_folder_path = os.path.join(self.EXTRACT_PATH, new_folder_name) + + if not os.path.exists(new_folder_path): + return + + logger.info( + f"Replacing old version with new version: {new_folder_name}" + ) + for item in os.listdir(self.EXTRACT_PATH): + item_path = os.path.join(self.EXTRACT_PATH, item) + if item in {new_folder_name, "bot.log", ".git"}: + continue + + if os.path.isdir(item_path): + shutil.rmtree(item_path) + else: + os.remove(item_path) + + for item in os.listdir(new_folder_path): + shutil.move(os.path.join(new_folder_path, item), self.EXTRACT_PATH) + + shutil.rmtree(new_folder_path) + + def check_for_update(self): + logger.info("Checking for updates...") + + current_sha = self._get_current_sha() + logger.info(f"Current commit SHA: {current_sha}") + + latest_sha = self._get_latest_sha() + logger.info(f"Latest commit SHA: {latest_sha}") + + if current_sha == latest_sha: + logger.info("You are already using the latest version.") + return + + user_input = input( + "A new update is available. Do you want to update? (Y/N): " + ) + if user_input.lower() == "y": + logger.info("Downloading update...") + self._download_update(latest_sha) + + logger.info(f"Extracting update from {self.DOWNLOAD_PATH}...") + self._extract_update() + + logger.info("Replacing old version with new version...") + self._replace_old_version(latest_sha) + if os.path.exists(self.DOWNLOAD_PATH): + os.remove(self.DOWNLOAD_PATH) + logger.info("Update successful!") + else: + logger.info("Update cancelled.") diff --git a/main.py b/main.py index 181070e..a4bfa60 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,16 @@ import sys import threading import time + +from loguru import logger + from clashroyalebuildabot.bot.example.custom_bot import CustomBot from clashroyalebuildabot.namespaces.cards import Cards -from clashroyalebuildabot.state.updater import check_for_update -from loguru import logger +from clashroyalebuildabot.updater import Updater start_time = datetime.now() + def update_terminal_title(): while True: elapsed_time = datetime.now() - start_time @@ -19,8 +22,10 @@ def update_terminal_title(): sys.stdout.flush() time.sleep(1) + def main(): - check_for_update() + updater = Updater() + updater.check_for_update() cards = [ Cards.MINIONS, Cards.ARCHERS, @@ -34,8 +39,9 @@ def main(): bot = CustomBot(cards, debug=False) bot.run() + if __name__ == "__main__": - logger.add("bot.log", rotation="500 MB") # Fügt die Logdatei hinzu + logger.add("bot.log", rotation="500 MB") title_thread = threading.Thread(target=update_terminal_title, daemon=True) title_thread.start() main()