Bläddra i källkod

Improve logging and config file handling, add flag for config file

Cammy 7 år sedan
förälder
incheckning
5b6e53336c
2 ändrade filer med 90 tillägg och 60 borttagningar
  1. 88 58
      pyinstalive/initialize.py
  2. 2 2
      pyinstalive/logger.py

+ 88 - 58
pyinstalive/initialize.py

@@ -4,6 +4,7 @@ import logging
 import os.path
 import sys
 import subprocess
+import time
 
 from .auth import login
 from .logger import log, seperator, supports_color
@@ -76,84 +77,113 @@ def check_config_validity(config):
 			return False
 
 		return True
-	except KeyError as e:
+	except Exception as e:
 		return False
 
-
-def run():
-
-	log('PYINSTALIVE (SCRIPT V{} - PYTHON V{})'.format(script_version, python_version), "GREEN")
-	seperator("GREEN")
-
-	logging.disable(logging.CRITICAL)
-	config = configparser.ConfigParser()
+def show_info():
+	log("[I] To see all the available flags, use the -h flag.", "BLUE")
+	log("", "GREEN")
+	log("[I] PyInstaLive version:    	" + script_version, "GREEN")
+	log("[I] Python version:         	" + python_version, "GREEN")
+	if check_ffmpeg() == False:
+		log("[E] FFmpeg framework:       	Not found", "RED")
+	else:
+		log("[I] FFmpeg framework:       	Available", "GREEN")
+	if not os.path.isfile('credentials.json'):
+		log("[W] Cookie file:            	Not found", "YELLOW")
+	else:
+		log("[I] Cookie file:            	Available", "GREEN")
+	log("[I] CLI supports color:     	" + str(supports_color()), "GREEN")
 
 	if os.path.exists('pyinstalive.ini'):
-		try:
-			config.read('pyinstalive.ini')
-		except Exception:
-			log("[E] Could not read configuration file. Try passing the required arguments manually.", "RED")
-			seperator("GREEN")
+		log("[I] Config file:", "GREEN")
+		log("", "GREEN")
+		with open('pyinstalive.ini') as f:
+			for line in f:
+				log("    " + line.rstrip(), "YELLOW")
 	else:
-		log("[W] Could not find configuration file, creating a default one ...", "YELLOW")
-		try:
-			config_template = "[pyinstalive]\nusername = johndoe\npassword = grapefruits\nsave_path = /\nshow_cookie_expiry = true"
-			config_file = open("pyinstalive.ini", "w")
-			config_file.write(config_template)
-			config_file.close()
-			log("[W] Edit the created 'pyinstalive.ini' file and run this script again.", "YELLOW")
-			seperator("GREEN")
-			sys.exit(0)
-		except Exception as e:
-			log("[E] Could not create default config file: " + str(e), "RED")
-			log("[W] You must manually create and edit it with the following template: ", "YELLOW")
-			log("", "GREEN")
-			log(config_template, "YELLOW")
+		log("[E] Config file:	    	Not found", "RED")
+	log("", "GREEN")
+	log("[I] End of PyInstaLive information screen.", "GREEN")
+	seperator("GREEN")
+
+def new_config():
+	try:
+		if os.path.exists('pyinstalive.ini'):
+			log("[I] A configuration file is already present:", "GREEN")
 			log("", "GREEN")
-			log("[W] Save it as 'pyinstalive.ini' and run this script again.", "YELLOW")
+			with open('pyinstalive.ini') as f:
+				for line in f:
+					log("    " + line.rstrip(), "YELLOW")
 			log("", "GREEN")
-			sys.exit(1)
+			log("[W] To create a default config file, delete 'pyinstalive.ini' and ", "YELLOW")
+			log("    run this script again.", "YELLOW")
+			seperator("GREEN")
+		else:
+			try:
+				log("[W] Could not find configuration file, creating a default one ...", "YELLOW")
+				config_template = "[pyinstalive]\nusername = johndoe\npassword = grapefruits\nsave_path = /\nshow_cookie_expiry = true\nclear_temp_files = false"
+				config_file = open("pyinstalive.ini", "w")
+				config_file.write(config_template)
+				config_file.close()
+				log("[W] Edit the created 'pyinstalive.ini' file and run this script again.", "YELLOW")
+				seperator("GREEN")
+				sys.exit(0)
+			except Exception as e:
+				log("[E] Could not create default config file: " + str(e), "RED")
+				log("[W] You must manually create and edit it with the following template: ", "YELLOW")
+				log("", "GREEN")
+				log(config_template, "YELLOW")
+				log("", "GREEN")
+				log("[W] Save it as 'pyinstalive.ini' and run this script again.", "YELLOW")
+				log("", "GREEN")
+	except Exception as e:
+		log("[E] An error occurred: " + str(e), "RED")
+		log("[W] If you don't have a configuration file,", "YELLOW")
+		log("    you must manually create and edit it with the following template: ", "YELLOW")
+		log("", "GREEN")
+		log(config_template, "YELLOW")
+		log("", "GREEN")
+		log("[W] Save it as 'pyinstalive.ini' and run this script again.", "YELLOW")
+		log("", "GREEN")	
+
 
+def run():
+	seperator("GREEN")
+	log('PYINSTALIVE (SCRIPT V{} - PYTHON V{}) - {}'.format(script_version, python_version, time.strftime('%H:%M:%S %p')), "GREEN")
+	seperator("GREEN")
 
+	logging.disable(logging.CRITICAL)
+
+	config = configparser.ConfigParser()
 	parser = argparse.ArgumentParser(description='You are running PyInstaLive ' + script_version + " with Python " + python_version)
 	parser.add_argument('-u', '--username', dest='username', type=str, required=False, help="Instagram username to login with.")
 	parser.add_argument('-p', '--password', dest='password', type=str, required=False, help="Instagram password to login with.")
 	parser.add_argument('-r', '--record', dest='record', type=str, required=False, help="The username of the Instagram whose livestream or replay you want to save.")
 	parser.add_argument('-i', '--info', dest='info', action='store_true', help="View information about PyInstaLive.")
-
+	parser.add_argument('-c', '--config', dest='config', action='store_true', help="Create a default configuration file if it doesn't exist.")
 	args = parser.parse_args()
 
+	if (args.username == None and args.password == None and args.record == None and args.info == False and args.config == False) or (args.info != False):
+		show_info()
+		sys.exit(0)
+	
+	if (args.config == True):
+		new_config()
+		sys.exit(0)
 
-	if check_config_validity(config):
-
-		def show_info():
-			log("[I] To see all the available flags, use the -h flag.", "BLUE")
-			log("", "GREEN")
-			log("[I] PyInstaLive version:    " + script_version, "GREEN")
-			log("[I] Python version:         " + python_version, "GREEN")
-			if check_ffmpeg() == False:
-				log("[E] FFmpeg available:       False", "RED")
-			else:
-				log("[I] FFmpeg available:       True", "GREEN")
-			if not os.path.isfile('credentials.json'):
-				log("[I] Cookie file:            Not available", "GREEN")
-			else:
-				log("[I] Cookie file:            Available", "GREEN")
-			log("[I] CLI supports color:     " + str(supports_color()), "GREEN")
-			log("[I] Config file:", "GREEN")
-			log("", "GREEN")
-			with open('pyinstalive.ini') as f:
-				for line in f:
-					log("    " + line.rstrip(), "YELLOW")
-			log("", "GREEN")
-			log("[I] End of PyInstaLive information screen.", "GREEN")
+	if os.path.exists('pyinstalive.ini'):
+		try:
+			config.read('pyinstalive.ini')
+		except Exception:
+			log("[E] Could not read configuration file. Try passing the required arguments manually.", "RED")
 			seperator("GREEN")
+	else:
+		new_config()
+		sys.exit(1)
 
 
-		if (args.username == None and args.password == None and args.record == None and args.info == False) or (args.info != False):
-			show_info()
-			sys.exit(0)
-
+	if check_config_validity(config):
 		if check_ffmpeg() == False:
 			log("[E] Could not find ffmpeg, the script will now exit. ", "RED")
 			seperator("GREEN")

+ 2 - 2
pyinstalive/logger.py

@@ -48,6 +48,6 @@ def log(string, color):
 
 def seperator(color):
 	if not supports_color():
-		print("-" * 50)
+		print("-" * 70)
 	else:
-		print('\033[1m' + colors(color) + ("-" * 50) + colors("ENDC"))
+		print('\033[1m' + colors(color) + ("-" * 70) + colors("ENDC"))