Quellcode durchsuchen

More code improvements and log changes

Cammy vor 7 Jahren
Ursprung
Commit
b12c7fc3af
3 geänderte Dateien mit 31 neuen und 24 gelöschten Zeilen
  1. 22 16
      pyinstalive/auth.py
  2. 8 8
      pyinstalive/downloader.py
  3. 1 0
      pyinstalive/initialize.py

+ 22 - 16
pyinstalive/auth.py

@@ -34,33 +34,34 @@ def from_json(json_object):
 	return json_object
 
 
-def onlogin_callback(api, settings_file):
+def onlogin_callback(api, cookie_file):
 	cache_settings = api.settings
-	with open(settings_file, 'w') as outfile:
+	with open(cookie_file, 'w') as outfile:
 		json.dump(cache_settings, outfile, default=to_json)
-		log('[I] New auth cookie file was made: {0!s}'.format(settings_file), "GREEN")
+		log('[I] New user cookie file was made: {0!s}'.format(cookie_file), "GREEN")
 
 
-def login(username, password, show_cookie_expiry, ignore_existing_cookie):
+def login(username, password, show_cookie_expiry, force_use_login_args):
 	device_id = None
 	try:
-		if ignore_existing_cookie:
+		if force_use_login_args:
+			log("[I] Overriding standard login with -u and -p arguments...", "GREEN")
 			api = Client(
 				username, password)
 		else:
-			settings_file = username + ".json"
-			if not os.path.isfile(settings_file):
+			cookie_file = "{}.json".format(username)
+			if not os.path.isfile(cookie_file):
 				# settings file does not exist
-				log('[W] Unable to find auth cookie file: {0!s}'.format(settings_file), "YELLOW")
+				log('[W] Unable to find user cookie file: {0!s}'.format(cookie_file), "YELLOW")
 
 				# login new
 				api = Client(
 					username, password,
-					on_login=lambda x: onlogin_callback(x, settings_file))
+					on_login=lambda x: onlogin_callback(x, cookie_file))
 			else:
-				with open(settings_file) as file_data:
+				with open(cookie_file) as file_data:
 					cached_settings = json.load(file_data, object_hook=from_json)
-				# log('[I] Using settings file: {0!s}'.format(settings_file), "GREEN")
+				# log('[I] Using settings file: {0!s}'.format(cookie_file), "GREEN")
 
 				device_id = cached_settings.get('device_id')
 				# reuse auth settings
@@ -76,7 +77,7 @@ def login(username, password, show_cookie_expiry, ignore_existing_cookie):
 		api = Client(
 			username, password,
 			device_id=device_id,
-			on_login=lambda x: onlogin_callback(x, settings_file))
+			on_login=lambda x: onlogin_callback(x, cookie_file))
 
 	except ClientLoginError as e:
 		seperator("GREEN")
@@ -91,17 +92,22 @@ def login(username, password, show_cookie_expiry, ignore_existing_cookie):
 	except Exception as e:
 		if (str(e).startswith("unsupported pickle protocol")):
 			log("[W] This cookie file is not compatible with Python {}.".format(sys.version.split(' ')[0][0]), "YELLOW")
-			log("[W] Please delete your cookie file '" + username + ".json' and try again.", "YELLOW")
+			log("[W] Please delete your cookie file '{}.json' and try again.".format(useranem), "YELLOW")
 		else:
 			log('[E] Unexpected Exception: {0!s}'.format(e), "RED")
 		seperator("GREEN")
 		sys.exit(99)
+	except KeyboardInterrupt as e:
+		seperator("GREEN")
+		log("[W] The user authentication has been aborted.", "YELLOW")
+		seperator("GREEN")
+		sys.exit(0)
 
-	log('[I] Using login cookie for "' + api.authenticated_user_name + '".', "GREEN")
-	if show_cookie_expiry.title() == 'True' and not ignore_existing_cookie:
+	log('[I] Using user cookie for "{: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] Login cookie expiry date: {0!s}'.format(datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%d at %I:%M:%S %p')), "GREEN")
+			log('[I] User cookie 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")
 

+ 8 - 8
pyinstalive/downloader.py

@@ -33,7 +33,7 @@ def run_command(command):
 	try:
 		FNULL = open(os.devnull, 'w')
 		subprocess.Popen(shlex.split(command), stdout=FNULL, stderr=subprocess.STDOUT)
-	except OSError as e:
+	except Exception as e:
 		pass
 
 
@@ -154,7 +154,7 @@ def download_livestream(broadcast):
 		log("[E] Could not download livestream: {:s}".format(str(e)), "RED")
 		try:
 		    os.remove(os.path.join(output_dir, 'folder.lock'))
-		except OSError:
+		except Exception:
 		    pass
 
 
@@ -185,7 +185,7 @@ def stitch_video(broadcast_downloader, broadcast, comment_thread_worker):
 			log('[I] Successfully stitched downloaded files into video.', "GREEN")
 			try:
 			    os.remove(os.path.join(live_folder_path, 'folder.lock'))
-			except OSError:
+			except Exception:
 			    pass
 			if settings.clear_temp_files.title() == "True":
 				try:
@@ -199,7 +199,7 @@ def stitch_video(broadcast_downloader, broadcast, comment_thread_worker):
 			seperator("GREEN")
 			try:
 			    os.remove(os.path.join(live_folder_path, 'folder.lock'))
-			except OSError:
+			except Exception:
 			    pass
 			sys.exit(1)
 		except Exception as e:
@@ -207,7 +207,7 @@ def stitch_video(broadcast_downloader, broadcast, comment_thread_worker):
 			seperator("GREEN")
 			try:
 			    os.remove(os.path.join(live_folder_path, 'folder.lock'))
-			except OSError:
+			except Exception:
 			    pass
 			sys.exit(1)
 	except KeyboardInterrupt:
@@ -215,7 +215,7 @@ def stitch_video(broadcast_downloader, broadcast, comment_thread_worker):
 			seperator("GREEN")
 			try:
 			    os.remove(os.path.join(live_folder_path, 'folder.lock'))
-			except OSError:
+			except Exception:
 			    pass
 			sys.exit(0)
 
@@ -318,7 +318,7 @@ def download_replays(broadcasts):
 					log("[I] Finished downloading replay {:s} of {:s}.".format(str(current), str(len(broadcasts))), "GREEN")
 					try:
 					    os.remove(os.path.join(output_dir, 'folder.lock'))
-					except OSError:
+					except Exception:
 					    pass
 
 					if (current != len(broadcasts)):
@@ -338,7 +338,7 @@ def download_replays(broadcasts):
 		seperator("GREEN")
 		try:
 		    os.remove(os.path.join(output_dir, 'folder.lock'))
-		except OSError:
+		except Exception:
 		    pass
 		sys.exit(1)
 	except KeyboardInterrupt:

+ 1 - 0
pyinstalive/initialize.py

@@ -365,6 +365,7 @@ def run():
 			log("[I] \033[94mpyinstalive -h\033[92m can be used to display command help.", "GREEN")
 		else:
 			log("[I] pyinstalive -h can be used to display command help.", "GREEN")
+		seperator("GREEN")
 		exit(1)
 
 	if (args.info) or (not