From 5904e52ac8a0540bdb15df71fd0a9f643132d44a Mon Sep 17 00:00:00 2001 From: Romeo Phillips Date: Mon, 22 Apr 2024 09:57:16 -0700 Subject: [PATCH] feat: add error for when a user is region blocked --- src/meta_ai_api/exceptions.py | 4 ++++ src/meta_ai_api/main.py | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/meta_ai_api/exceptions.py b/src/meta_ai_api/exceptions.py index b9a763e..ff5e4d8 100644 --- a/src/meta_ai_api/exceptions.py +++ b/src/meta_ai_api/exceptions.py @@ -1,2 +1,6 @@ class FacebookInvalidCredentialsException(Exception): pass + + +class FacebookRegionBlocked(Exception): + pass diff --git a/src/meta_ai_api/main.py b/src/meta_ai_api/main.py index cade73a..27d600e 100644 --- a/src/meta_ai_api/main.py +++ b/src/meta_ai_api/main.py @@ -16,6 +16,8 @@ from meta_ai_api.utils import get_fb_session +from meta_ai_api.exceptions import FacebookRegionBlocked + MAX_RETRIES = 3 @@ -68,7 +70,13 @@ def get_access_token(self) -> str: } response = self.session.post(url, headers=headers, data=payload) - auth_json = response.json() + try: + auth_json = response.json() + except json.JSONDecodeError: + raise FacebookRegionBlocked( + "Unable to receive a valid response from Meta AI. This is likely due to your region being blocked. " + "Try manually accessing https://www.meta.ai/ to confirm." + ) access_token = auth_json["data"]["xab_abra_accept_terms_of_service"][ "new_temp_user_auth" ]["access_token"]