diff --git a/instpector/__init__.py b/instpector/__init__.py index b54159e..64d9608 100644 --- a/instpector/__init__.py +++ b/instpector/__init__.py @@ -1 +1,4 @@ from .instpector import Instpector +from . import endpoints + +__ALL__ = ["Instpector", "endpoints"] diff --git a/instpector/apis/http_request.py b/instpector/apis/http_request.py index 763df0a..35a70b2 100644 --- a/instpector/apis/http_request.py +++ b/instpector/apis/http_request.py @@ -1,5 +1,6 @@ from enum import Enum from urllib.parse import urlencode +from requests import RequestException class HttpRequestMode(Enum): JSON = 1 @@ -36,11 +37,15 @@ def get(self, url_path, **options): get_params = {} if options.get("params"): get_params = options.get("params") - return self._session.get(f"{self._base_url}{url_path}", - headers=request_headers, - params=get_params, - allow_redirects=options.get("redirects", False), - timeout=10) + try: + return self._session.get(f"{self._base_url}{url_path}", + headers=request_headers, + params=get_params, + allow_redirects=options.get("redirects", False), + timeout=10) + except RequestException as req_exception: + print(f"GET RequestException: {req_exception}") + return None def post(self, url_path, data, **options): request_headers = self._get_headers( @@ -48,11 +53,15 @@ def post(self, url_path, data, **options): options.get("headers")) post_data = urlencode(data) post_headers = {**request_headers, "Content-Length": str(len(post_data))} - return self._session.post(f"{self._base_url}{url_path}", - data=post_data, - headers=post_headers, - allow_redirects=options.get("redirects", False), - timeout=10) + try: + return self._session.post(f"{self._base_url}{url_path}", + data=post_data, + headers=post_headers, + allow_redirects=options.get("redirects", False), + timeout=10) + except RequestException as req_exception: + print(f"POST RequestException: {req_exception}") + return None @classmethod def _get_headers(cls, mode, headers): diff --git a/instpector/apis/instagram/authenticate.py b/instpector/apis/instagram/authenticate.py index c1c1e3f..2f796d3 100644 --- a/instpector/apis/instagram/authenticate.py +++ b/instpector/apis/instagram/authenticate.py @@ -36,7 +36,7 @@ def logout(self): mode=HttpRequestMode.FORM, data=data, headers=headers) - if response.status_code != 301: + if not response or response.status_code != 301: raise AuthenticateRevokeException("Logout unsuccessful") print("Logged out") @@ -56,6 +56,8 @@ def _login_prepare(self): mode=HttpRequestMode.FORM, headers=headers, redirects=True) + if not response: + return self._auth_headers = response.cookies.get_dict(".instagram.com") if not self._app_info: app_id, ajax_id = self._lookup_headers() @@ -86,7 +88,7 @@ def _login_execute(self): data = json.loads(response.text) self._auth_cookies = response.cookies.get_dict(".instagram.com") return self._parse_login(data) - except json.decoder.JSONDecodeError: + except (json.decoder.JSONDecodeError, AttributeError): raise AuthenticateFailException("Unexpected login response.") def _parse_login(self, data): diff --git a/instpector/apis/instagram/base_api.py b/instpector/apis/instagram/base_api.py index ca859e2..91767fb 100644 --- a/instpector/apis/instagram/base_api.py +++ b/instpector/apis/instagram/base_api.py @@ -6,6 +6,8 @@ class BaseApi(HttpRequest): def get(self, url_path, **options): response = super().get(url_path, **options) + if not response: + return None # Retry just once when rate limit has been exceeded if response.status_code == 429: print("The rate limit has been reached. Resuming in 3 min...") diff --git a/instpector/endpoint_factory.py b/instpector/endpoint_factory.py index a3b5512..23be2a0 100644 --- a/instpector/endpoint_factory.py +++ b/instpector/endpoint_factory.py @@ -8,5 +8,6 @@ def register_endpoint(self, name, endpoint): def create(self, endpoint_name, instpector): endpoint = self._endpoints.get(endpoint_name) if not endpoint: + print(f"No endpoint '{endpoint_name}' available.") raise ValueError(endpoint_name) return endpoint(instpector.session()) diff --git a/setup.py b/setup.py index 1164220..4e9d42a 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="instpector", - version="0.1.4", + version="0.1.5", description="A simple Instagram's web API library", author="Erik Lopez", long_description=README,