From 88a62db33e23a56de7c6a3e8b38a7d27bdddc543 Mon Sep 17 00:00:00 2001 From: Hoang Hong Quan Date: Sun, 29 Sep 2024 02:28:32 +0700 Subject: [PATCH] Rewrite code related to GitHub REST API request quota --- Scripts/gathering_files.py | 6 ++++-- Scripts/github.py | 16 +++++----------- updater.py | 23 +++++++++++++++++------ 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Scripts/gathering_files.py b/Scripts/gathering_files.py index 27e252c..55eb4c3 100644 --- a/Scripts/gathering_files.py +++ b/Scripts/gathering_files.py @@ -45,9 +45,11 @@ class gatheringFiles: "url": dortania_builds_data[name]["versions"][0]["links"]["release"] }) else: - download_urls.extend(self.github.get_latest_release(kext.github_repo.get("owner"), kext.github_repo.get("repo"))) + if self.github.check_ratelimit(): + download_urls.extend(self.github.get_latest_release(kext.github_repo.get("owner"), kext.github_repo.get("repo"))) - download_urls.extend(self.github.get_latest_release("wjz304", "OpenCore_Patch_Build")) + if self.github.check_ratelimit(): + download_urls.extend(self.github.get_latest_release("wjz304", "OpenCore_Patch_Build")) sorted_data = sorted(download_urls, key=lambda x:x["product_name"]) diff --git a/Scripts/github.py b/Scripts/github.py index 489fb9d..4ac90f0 100644 --- a/Scripts/github.py +++ b/Scripts/github.py @@ -16,12 +16,10 @@ class Github: url = "https://api.github.com/rate_limit" response = self.fetcher.fetch_and_parse_content(url, "json") - if response.get("rate").get("remaining") == 0: - raise Exception("Please try again later, you have exhausted your GitHub REST API request quota") + + return response.get("rate").get("remaining") != 0 def get_latest_commit(self, owner, repo): - self.check_ratelimit() - url = "https://api.github.com/repos/{}/{}/commits".format(owner, repo) response = self.fetcher.fetch_and_parse_content(url, "json") @@ -31,8 +29,6 @@ class Github: def get_latest_artifact(self, owner, repo): results = [] - self.check_ratelimit() - url = "https://api.github.com/repos/{}/{}/actions/artifacts?per_page=1".format(owner, repo) response = self.fetcher.fetch_and_parse_content(url, "json") @@ -48,9 +44,7 @@ class Github: return results def get_latest_release(self, owner, repo): - result = [] - - self.check_ratelimit() + results = [] url = "https://api.github.com/repos/{}/{}/releases?per_page=1".format(owner, repo) @@ -62,13 +56,13 @@ class Github: asset_name = self.extract_asset_name(asset.get("name")) if "tlwm" in download_url or ("tlwm" not in download_url and "DEBUG" not in download_url.upper()): - result.append({ + results.append({ "product_name": asset_name, "id": asset_id, "url": download_url }) - return result + return results def extract_asset_name(self, file_name): end_idx = len(file_name) diff --git a/updater.py b/updater.py index 0cba37d..c446bfc 100644 --- a/updater.py +++ b/updater.py @@ -60,21 +60,32 @@ class Updater: self.utils.write_file(self.sha_version, sha_version_info) def run_update(self): - self.utils.head("Check for update") + self.utils.head("Check for Updates") print("") current_sha_version = self.get_current_sha_version() + + if not self.github.check_ratelimit(): + print("GitHub REST API request quota has been exhausted. Automatic update check is unavailable now.") + print("Please check for updates manually if needed.") + print("") + self.utils.request_input() + return False + latest_sha_version = self.get_latest_sha_version() print("Current script SHA version: {}".format(current_sha_version)) print("Latest script SHA version: {}".format(latest_sha_version)) print("") if latest_sha_version != current_sha_version: - print("Updating from version {} to {}\n".format(current_sha_version, latest_sha_version)) + print("Updating from version {} to {}".format(current_sha_version, latest_sha_version)) + print("") self.download_update() self.update_files() self.save_latest_sha_version(latest_sha_version) - print("\n\n{}\n".format(self.utils.message("The program needs to restart to complete the update process.", "reminder"))) - return True + print("\n") + print("The program needs to restart to complete the update process.") + print("\n") + self.utils.request_input() else: print("You are already using the latest version") - return False - + + return latest_sha_version != current_sha_version \ No newline at end of file