|
@@ -38,7 +38,7 @@ def onlogin_callback(api, cookie_file):
|
|
|
cache_settings = api.settings
|
|
|
with open(cookie_file, 'w') as outfile:
|
|
|
json.dump(cache_settings, outfile, default=to_json)
|
|
|
- log('[I] New user cookie file was made: {0!s}'.format(cookie_file), "GREEN")
|
|
|
+ log('[I] New cookie file was made: {0!s}'.format(cookie_file), "GREEN")
|
|
|
|
|
|
|
|
|
def login(username, password, show_cookie_expiry, force_use_login_args):
|
|
@@ -52,7 +52,8 @@ def login(username, password, show_cookie_expiry, force_use_login_args):
|
|
|
cookie_file = "{}.json".format(username)
|
|
|
if not os.path.isfile(cookie_file):
|
|
|
# settings file does not exist
|
|
|
- log('[W] Unable to find user cookie file: {0!s}'.format(cookie_file), "YELLOW")
|
|
|
+ log('[W] Unable to find cookie file: {0!s}'.format(cookie_file), "YELLOW")
|
|
|
+ log('[I] Creating a new cookie file...', "YELLOW")
|
|
|
|
|
|
# login new
|
|
|
api = Client(
|
|
@@ -64,20 +65,29 @@ def login(username, password, show_cookie_expiry, force_use_login_args):
|
|
|
# log('[I] Using settings file: {0!s}'.format(cookie_file), "GREEN")
|
|
|
|
|
|
device_id = cached_settings.get('device_id')
|
|
|
- # reuse auth settings
|
|
|
+ # reuse auth cached_settings
|
|
|
api = Client(
|
|
|
username, password,
|
|
|
settings=cached_settings)
|
|
|
|
|
|
- except (ClientCookieExpiredError, ClientLoginRequiredError) as e:
|
|
|
- log('[E] ClientCookieExpiredError/ClientLoginRequiredError: {0!s}'.format(e), "RED")
|
|
|
+ except (ClientCookieExpiredError) as e:
|
|
|
+ log('[W] The current cookie file for "{:s}" has expired, creating a new one...'.format(username), "YELLOW")
|
|
|
|
|
|
# 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, cookie_file))
|
|
|
+ try:
|
|
|
+ api = Client(
|
|
|
+ username, password,
|
|
|
+ device_id=device_id,
|
|
|
+ on_login=lambda x: onlogin_callback(x, cookie_file))
|
|
|
+ except Exception as ee:
|
|
|
+ seperator("GREEN")
|
|
|
+ log('[E] An error occurred while trying to create a new cookie file: {:s}'.format(str(ee)), "RED")
|
|
|
+ if "getaddrinfo failed" in str(ee):
|
|
|
+ log('[E] Could not resolve host, check your internet connection.', "RED")
|
|
|
+ if "timed out" in str(ee):
|
|
|
+ log('[E] The connection timed out, check your internet connection.', "RED")
|
|
|
+ exit(1)
|
|
|
|
|
|
except ClientLoginError as e:
|
|
|
seperator("GREEN")
|
|
@@ -86,7 +96,14 @@ def login(username, password, show_cookie_expiry, force_use_login_args):
|
|
|
sys.exit(9)
|
|
|
except ClientError as e:
|
|
|
seperator("GREEN")
|
|
|
- log('[E] Client Error: {0!s}\n[E] Message: {1!s}\n[E] Code: {2:d}\n\n[E] Full response:\n{3!s}\n'.format(e.msg, json.loads(e.error_response).get("message", "Additional error information not available."), e.code, e.error_response), "RED")
|
|
|
+ try:
|
|
|
+ log('[E] Unexpected exception: {0!s}\n[E] Message: {1!s}\n[E] Code: {2:d}\n\n[E] Full response:\n{3!s}\n'.format(e.msg, json.loads(e.error_response).get("message", "Additional error information not available."), e.code, e.error_response), "RED")
|
|
|
+ except Exception as ee:
|
|
|
+ log('[E] An error occurred while trying to handle a previous exception.\n[E] 1: {:s}\n[E] 2: {:s}'.format(str(e), str(ee)), "RED")
|
|
|
+ if "getaddrinfo failed" in str(ee):
|
|
|
+ log('[E] Could not resolve host, check your internet connection.', "RED")
|
|
|
+ if "timed out" in str(ee):
|
|
|
+ log('[E] The connection timed out, check your internet connection.', "RED")
|
|
|
seperator("GREEN")
|
|
|
sys.exit(9)
|
|
|
except Exception as e:
|
|
@@ -94,7 +111,8 @@ def login(username, password, show_cookie_expiry, force_use_login_args):
|
|
|
log("[W] This cookie file is not compatible with Python {}.".format(sys.version.split(' ')[0][0]), "YELLOW")
|
|
|
log("[W] Please delete your cookie file '{}.json' and try again.".format(username), "YELLOW")
|
|
|
else:
|
|
|
- log('[E] Unexpected Exception: {0!s}'.format(e), "RED")
|
|
|
+ seperator("GREEN")
|
|
|
+ log('[E] Unexpected exception: {0!s}'.format(e), "RED")
|
|
|
seperator("GREEN")
|
|
|
sys.exit(99)
|
|
|
except KeyboardInterrupt as e:
|
|
@@ -103,13 +121,13 @@ def login(username, password, show_cookie_expiry, force_use_login_args):
|
|
|
seperator("GREEN")
|
|
|
sys.exit(0)
|
|
|
|
|
|
- log('[I] Using user cookie for "{:s}".'.format(str(api.authenticated_user_name)), "GREEN")
|
|
|
+ log('[I] Successfully logged into user "{:s}".'.format(str(api.authenticated_user_name)), "GREEN")
|
|
|
if show_cookie_expiry.title() == 'True' and not force_use_login_args:
|
|
|
try:
|
|
|
cookie_expiry = api.cookie_jar.auth_expires
|
|
|
- log('[I] User cookie expiry date: {0!s}'.format(datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%d at %I:%M:%S %p')), "GREEN")
|
|
|
+ log('[I] Cookie file expiry date: {0!s}'.format(datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%d at %I:%M:%S %p')), "GREEN")
|
|
|
except AttributeError as e:
|
|
|
- log('[W] An error occurred while getting the cookie expiry date: {0!s}'.format(e), "YELLOW")
|
|
|
-
|
|
|
+ log('[W] An error occurred while getting the cookie file expiry date: {0!s}'.format(e), "YELLOW")
|
|
|
|
|
|
+ seperator("GREEN")
|
|
|
return api
|