From 912a4c63ef94a3eb7f4141e9ba7e54f7e240d0da Mon Sep 17 00:00:00 2001 From: Howard20181 <40033067+Howard20181@users.noreply.github.com> Date: Fri, 20 Jan 2023 06:12:13 +0800 Subject: [PATCH] Fix Github API rate limit message --- scripts/generateGappsLink.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/generateGappsLink.py b/scripts/generateGappsLink.py index 4ef33da..b74685e 100644 --- a/scripts/generateGappsLink.py +++ b/scripts/generateGappsLink.py @@ -34,8 +34,7 @@ download_dir = Path.cwd().parent / \ tempScript = sys.argv[5] android_api = sys.argv[6] file_name = sys.argv[7] -print( - f"Generating {brand} download link: arch={arch} variant={variant}", flush=True) +print(f"Generating {brand} download link: arch={arch} variant={variant}", flush=True) abi_map = {"x64": "x86_64", "arm64": "arm64"} android_api_map = {"30": "11.0", "32": "12.1", "33": "13.0"} release = android_api_map[android_api] @@ -45,7 +44,7 @@ if brand == "OpenGApps": j = json.loads(res.content) link = {i["name"]: i for i in j["archs"][abi_map[arch]] ["apis"][release]["variants"]}[variant]["zip"] - DATE=j["archs"][abi_map[arch]]["date"] + DATE = j["archs"][abi_map[arch]]["date"] print(f"DATE={DATE}", flush=True) except Exception: print("Failed to fetch from OpenGApps API, fallbacking to SourceForge RSS...") @@ -64,18 +63,19 @@ elif brand == "MindTheGapps": print(f"Failed to fetch from SourceForge RSS, fallbacking to Github API...", flush=True) res = requests.get(f"https://api.github.com/repos/s1204IT/MindTheGappsBuilder/releases/latest") json_data = json.loads(res.content) + headers = res.headers + x_ratelimit_remaining = headers["x-ratelimit-remaining"] if res.status_code == 200: assets = json_data["assets"] for asset in assets: if re.match(f'.*{release}.*{abi_map[arch]}.*\.zip$', asset["name"]): link = asset["browser_download_url"] - elif res.status_code == 403: + break + elif res.status_code == 403 and x_ratelimit_remaining == '0': message = json_data["message"] print(f"Github API Error: {message}", flush=True) - headers = res.headers - headers_json = json.load(headers) - ratelimit_reset = headers_json["x-ratelimit-reset"] - ratelimit_reset = datetime.datetime.utcfromtimestamp(ratelimit_reset) + ratelimit_reset = headers["x-ratelimit-reset"] + ratelimit_reset = datetime.fromtimestamp(int(ratelimit_reset)) print(f"The current rate limit window resets in {ratelimit_reset}", flush=True) exit(1)