From f1f57b74d47b968348e4f03eaca76be04b4b983d Mon Sep 17 00:00:00 2001 From: woclass Date: Tue, 27 Sep 2022 14:17:12 +0800 Subject: [PATCH 1/3] check http status code first, then parse resp ctx --- iksm.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/iksm.py b/iksm.py index 8492f99..92c85c2 100644 --- a/iksm.py +++ b/iksm.py @@ -306,7 +306,6 @@ def get_bullet(web_service_token, web_view_ver, app_user_agent, user_lang, user_ } url = "https://api.lp1.av5ja.srv.nintendo.net/api/bullet_tokens" r = requests.post(url, headers=app_head, cookies=app_cookies) - bullet_resp = json.loads(r.text) if r.status_code == 401: print("Unauthorized error (ERROR_INVALID_GAME_WEB_TOKEN). Cannot fetch tokens at this time.") @@ -317,13 +316,14 @@ def get_bullet(web_service_token, web_view_ver, app_user_agent, user_lang, user_ elif r.status_code == 204: # No Content, USER_NOT_REGISTERED print("Cannot access SplatNet 3 without having played online.") sys.exit(1) - else: - try: - bullet_token = bullet_resp["bulletToken"] - except: - print("Error from Nintendo (in api/bullet_tokens step):") - print(json.dumps(bullet_resp, indent=2)) - sys.exit(1) + + bullet_resp = json.loads(r.text) + try: + bullet_token = bullet_resp["bulletToken"] + except: + print("Error from Nintendo (in api/bullet_tokens step):") + print(json.dumps(bullet_resp, indent=2)) + sys.exit(1) return bullet_token From 8887b205ee571f2ccbacdb94c6eee38265cf001b Mon Sep 17 00:00:00 2001 From: woclass Date: Thu, 6 Oct 2022 12:46:58 +0800 Subject: [PATCH 2/3] catch JSONDecodeError Co-Authored-By: Takeshi HASEGAWA <2528004+hasegaw@users.noreply.github.com> --- iksm.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/iksm.py b/iksm.py index 92c85c2..e5866da 100644 --- a/iksm.py +++ b/iksm.py @@ -317,10 +317,13 @@ def get_bullet(web_service_token, web_view_ver, app_user_agent, user_lang, user_ print("Cannot access SplatNet 3 without having played online.") sys.exit(1) - bullet_resp = json.loads(r.text) try: + bullet_resp = json.loads(r.text) bullet_token = bullet_resp["bulletToken"] - except: + except (json.decoder.JSONDecodeError, TypeError): + print("Got non-JSON response from Nintendo.") + sys.exit(1) + except KeyError: print("Error from Nintendo (in api/bullet_tokens step):") print(json.dumps(bullet_resp, indent=2)) sys.exit(1) From 557b0c87302abe2d167feac4ee4f4b5332e2071e Mon Sep 17 00:00:00 2001 From: woclass Date: Thu, 6 Oct 2022 12:52:00 +0800 Subject: [PATCH 3/3] Catch all other errors at the end --- iksm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iksm.py b/iksm.py index e5866da..cae0020 100644 --- a/iksm.py +++ b/iksm.py @@ -323,7 +323,7 @@ def get_bullet(web_service_token, web_view_ver, app_user_agent, user_lang, user_ except (json.decoder.JSONDecodeError, TypeError): print("Got non-JSON response from Nintendo.") sys.exit(1) - except KeyError: + except: print("Error from Nintendo (in api/bullet_tokens step):") print(json.dumps(bullet_resp, indent=2)) sys.exit(1)