Преглед на файлове

Error handling improvements (hopefully), also some log changes

Cammy преди 7 години
родител
ревизия
b8fd2edfc3
променени са 2 файла, в които са добавени 64 реда и са изтрити 24 реда
  1. 33 15
      pyinstalive/auth.py
  2. 31 9
      pyinstalive/downloader.py

+ 33 - 15
pyinstalive/auth.py

@@ -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

+ 31 - 9
pyinstalive/downloader.py

@@ -227,27 +227,40 @@ def get_user_info(user_to_download):
 	try:
 		user_res = instagram_api.username_info(user_to_download)
 		user_id = user_res.get('user', {}).get('pk')
-	except ClientConnectionError as e:
-		if "timed out" in str(e):
-			log('[E] Could not get information for "{:s}": The connection has timed out.'.format(user_to_download), "RED")
-		else:
-			log('[E] Could not get information for "{:s}".\n[E] Error message: {:s}\n[E] Code: {:d}\n[E] Response: {:s}'.format(user_to_download, str(e), e.code, e.error_response), "RED")
+	except ClientConnectionError as cce:
+		log('[E] Could not get user info for "{:s}": {:d} {:s}'.format(user_to_download, cce.code, str(cce)), "RED")
+		if "getaddrinfo failed" in str(cce):
+			log('[E] Could not resolve host, check your internet connection.', "RED")
+		if "timed out" in str(cce):
+			log('[E] The connection timed out, check your internet connection.', "RED")
+		seperator("GREEN")
+		sys.exit(1)
+	except ClientThrottledError as cte:
+		log('[E] Could not get user info for "{:s}": {:d} {:s}\n[E] You are making too many requests at this time.'.format(user_to_download, cte.code, str(cte)), "RED")
+		seperator("GREEN")
+		sys.exit(1)
+	except ClientError as ce:
+		log('[E] Could not get user info for "{:s}": {:d} {:s}'.format(user_to_download, ce.code, str(ce)), "RED")
+		if ("Not Found") in str(ce):
+			log('[E] The specified user does not exist.', "RED")
 		seperator("GREEN")
 		sys.exit(1)
 	except Exception as e:
-		log('[E] Could not get information for "{:s}".\n[E] Error message: {:s}\n[E] Code: {:d}\n[E] Response: {:s}'.format(user_to_download, str(e), e.code, e.error_response), "RED")
+		log('[E] Could not get user info for "{:s}": {:s}'.format(user_to_download, str(e)), "RED")
+
 		seperator("GREEN")
 		sys.exit(1)
 	except KeyboardInterrupt:
-		log('[W] Aborted getting information for "{:s}", exiting...'.format(user_to_download), "YELLOW")
+		log('[W] Aborted getting user info for "{:s}", exiting...'.format(user_to_download), "YELLOW")
 		seperator("GREEN")
-		sys.exit(1)
+		sys.exit(0)
 	log('[I] Getting info for "{:s}" successful.'.format(user_to_download), "GREEN")
 	get_broadcasts_info(user_id)
 
 
 
 def get_broadcasts_info(user_id):
+	seperator("GREEN")
 	try:
 		log('[I] Checking for livestreams and replays...', "GREEN")
 		seperator("GREEN")
@@ -273,9 +286,18 @@ def get_broadcasts_info(user_id):
 		seperator("GREEN")
 	except Exception as e:
 		log('[E] Could not finish checking: {:s}'.format(str(e)), "RED")
+		if "timed out" in str(e):
+			log('[E] The connection timed out, check your internet connection.', "RED")
+		seperator("GREEN")
+		exit(1)
+	except KeyboardInterrupt:
+		log('[W] Aborted checking for livestreams and replays, exiting...'.format(user_to_download), "YELLOW")
+		seperator("GREEN")
+		sys.exit(1)
 	except ClientThrottledError as cte:
 		log('[E] Could not check because you are making too many requests at this time.', "RED")
-		log('[E] Error response: {:s}'.format(str(cte)), "RED")
+		seperator("GREEN")
+		exit(1)
 
 def download_replays(broadcasts):
 	try: