Skip to content

Commit

Permalink
Add connection exception control
Browse files Browse the repository at this point in the history
  • Loading branch information
niuware committed Jul 8, 2019
1 parent 1d0ae79 commit e8cc170
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
3 changes: 3 additions & 0 deletions instpector/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .instpector import Instpector
from . import endpoints

__ALL__ = ["Instpector", "endpoints"]
29 changes: 19 additions & 10 deletions instpector/apis/http_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum
from urllib.parse import urlencode
from requests import RequestException

class HttpRequestMode(Enum):
JSON = 1
Expand Down Expand Up @@ -36,23 +37,31 @@ 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(
options.get("mode", HttpRequestMode.JSON),
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):
Expand Down
6 changes: 4 additions & 2 deletions instpector/apis/instagram/authenticate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions instpector/apis/instagram/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
1 change: 1 addition & 0 deletions instpector/endpoint_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e8cc170

Please sign in to comment.