-
-
Save koddr/6bb6720db5969f6a075a92bac8f1fdef to your computer and use it in GitHub Desktop.
Hook for Python Instagram private API for avoid re-login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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