Skip to content

Instantly share code, notes, and snippets.

@koddr koddr/auth.py forked from notcammy/auth.py
Last active Dec 17, 2017

Embed
What would you like to do?
Hook for Python Instagram private API for avoid re-login
import json
import codecs
import os.path
try:
from instagram_private_api import (
Client, ClientError, ClientLoginError,
ClientCookieExpiredError, ClientLoginRequiredError,
__version__ as client_version)
except ImportError:
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from instagram_private_api import (
Client, ClientError, ClientLoginError,
ClientCookieExpiredError, ClientLoginRequiredError,
__version__ as client_version)
def to_json(python_object):
if isinstance(python_object, bytes):
return {'__class__': 'bytes',
'__value__': codecs.encode(python_object, 'base64').decode()}
raise TypeError(repr(python_object) + ' is not JSON serializable')
def from_json(json_object):
if '__class__' in json_object and json_object['__class__'] == 'bytes':
return codecs.decode(json_object['__value__'].encode(), 'base64')
return json_object
def onlogin_callback(api, new_settings_file):
cache_settings = api.settings
with open(new_settings_file, 'w') as outfile:
json.dump(cache_settings, outfile, default=to_json)
print('SAVED: {0!s}'.format(new_settings_file))
def login_instagram(username, password):
print('Client version: {0!s}'.format(client_version))
print("Login {} with {}".format(username, password))
device_id = None
try:
settings_file = 'credentials.json'
if not os.path.isfile(settings_file):
# settings file does not exist
print('Unable to find file: {0!s}'.format(settings_file))
# login new
api = Client(
username, password,
on_login=lambda x: onlogin_callback(x, settings_file))
else:
with open(settings_file) as file_data:
cached_settings = json.load(file_data, object_hook=from_json)
print('Reusing settings: {0!s}'.format(settings_file))
device_id = cached_settings.get('device_id')
# reuse auth settings
api = Client(
username, password,
settings=cached_settings)
except (ClientCookieExpiredError, ClientLoginRequiredError) as e:
print('ClientCookieExpiredError/ClientLoginRequiredError: {0!s}'.format(e))
# Login expired
# Do relogin but use default ua, keys and such
api = Client(
username, password,
device_id=device_id,
on_login=lambda x: onlogin_callback(x, settings_file))
except ClientLoginError as e:
print('ClientLoginError {0!s}'.format(e))
exit(9)
except ClientError as e:
print('ClientError {0!s} (Code: {1:d}, Response: {2!s})'.format(e.msg, e.code, e.error_response))
exit(9)
except Exception as e:
print('Unexpected Exception: {0!s}'.format(e))
exit(99)
return api
import auth
from instagram_private_api import Client, ClientCompatPatch
def do_something():
instagram_app_api = auth.login_instagram("username", "password")
# Do stuff with instagram_app_api
do_something()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.