Selaa lähdekoodia

Improved config handling, added option to remove temp files

Cammy 7 vuotta sitten
vanhempi
commit
6bf0282777
4 muutettua tiedostoa jossa 75 lisäystä ja 54 poistoa
  1. 1 1
      pyinstalive/auth.py
  2. 16 16
      pyinstalive/downloader.py
  3. 48 37
      pyinstalive/initialize.py
  4. 10 0
      pyinstalive/settings.py

+ 1 - 1
pyinstalive/auth.py

@@ -84,7 +84,7 @@ def login(username, password, show_cookie_expiry):
 		sys.exit(99)
 
 	log('[I] Login to "' + api.authenticated_user_name + '" OK!', "GREEN")
-	if show_cookie_expiry == 'True':
+	if show_cookie_expiry.title() == 'True':
 		cookie_expiry = api.cookie_jar.expires_earliest
 		log('[I] Login cookie expiry date: {0!s}'.format(datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%d at %H:%M:%S')), "GREEN")
 

+ 16 - 16
pyinstalive/downloader.py

@@ -4,29 +4,23 @@ import os
 import shutil
 
 from instagram_private_api_extensions import live, replay
-
 from .logger import log, seperator
 
-
 class NoLivestreamException(Exception):
 	pass
 
 class NoReplayException(Exception):
 	pass
 
-def main(api_arg, record_arg, save_path_arg):
+def main(api_arg, record_arg, settings_arg):
 	global api
 	global record
-	global save_path
-	global current_date
-	global current_time
 	global broadcast
 	global mpd_url
-	current_time = str(int(time.time()))
-	current_date = time.strftime("%Y%m%d")
+	global settings
+	settings = settings_arg
 	api = api_arg
 	record = record_arg
-	save_path = save_path_arg
 	get_user_info(record)
 
 def record_stream(broadcast):
@@ -48,7 +42,7 @@ def record_stream(broadcast):
 				   or broadcast.get('dash_abr_playback_url')
 				   or broadcast['dash_playback_url'])
 
-		output_dir = save_path + '{}_{}_{}_{}_live_downloads'.format(current_date, record, broadcast['id'], current_time)
+		output_dir = settings.save_path + '{}_{}_{}_{}_live_downloads'.format(settings.current_date, record, broadcast['id'], settings.current_time)
 
 		dl = live.Downloader(
 			mpd=mpd_url,
@@ -81,9 +75,12 @@ def record_stream(broadcast):
 
 def stitch_video(dl, broadcast):
 		log('[I] Stitching downloaded files into video...', "GREEN")
-		output_file = save_path + '{}_{}_{}_{}_live.mp4'.format(current_date, record, broadcast['id'], current_time)
+		output_file = settings.save_path + '{}_{}_{}_{}_live.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time)
 		try:
-			dl.stitch(output_file, cleartempfiles=False)
+			if settings.clear_temp_files.title() == "True":
+				dl.stitch(output_file, cleartempfiles=True)
+			else:
+				dl.stitch(output_file, cleartempfiles=False)
 			log('[I] Successfully stitched downloaded files.', "GREEN")
 			seperator("GREEN")
 			sys.exit(0)
@@ -140,9 +137,9 @@ def get_replays(user_id):
 				exists = False
 
 				if sys.version.split(' ')[0].startswith('2'):
-					directories = (os.walk(save_path).next()[1])
+					directories = (os.walk(settings.save_path).next()[1])
 				else:
-					directories = (os.walk(save_path).__next__()[1])
+					directories = (os.walk(settings.save_path).__next__()[1])
 
 				for directory in directories:
 					if (str(broadcast['id']) in directory) and ("_live_" not in directory):
@@ -152,12 +149,15 @@ def get_replays(user_id):
 					current = index + 1
 					log("[I] Downloading replay " + str(current) + " of "  + str(len(broadcasts)) + " with ID '" + str(broadcast['id']) + "'...", "GREEN")
 					current_time = str(int(time.time()))
-					output_dir = save_path + '{}_{}_{}_{}_replay_downloads'.format(current_date, record, broadcast['id'], current_time)
+					output_dir = settings.save_path + '{}_{}_{}_{}_replay_downloads'.format(settings.current_date, record, broadcast['id'], settings.current_time)
 					dl = replay.Downloader(
 						mpd=broadcast['dash_manifest'],
 						output_dir=output_dir,
 						user_agent=api.user_agent)
-					replay_saved = dl.download(save_path + '{}_{}_{}_{}_replay.mp4'.format(current_date, record, broadcast['id'], current_time), cleartempfiles=False)
+					if settings.clear_temp_files.title() == "True":
+						replay_saved = dl.download(settings.save_path + '{}_{}_{}_{}_replay.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time), cleartempfiles=True)
+					else:
+						replay_saved = dl.download(settings.save_path + '{}_{}_{}_{}_replay.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time), cleartempfiles=False)
 					if (len(replay_saved) == 1):
 						log("[I] Finished downloading replay " + str(current) + " of "  + str(len(broadcasts)) + ".", "GREEN")
 						log("", "GREEN")

+ 48 - 37
pyinstalive/initialize.py

@@ -8,13 +8,12 @@ import subprocess
 from .auth import login
 from .logger import log, seperator, supports_color
 from .downloader import main
-
+from .settings import settings
 
 script_version = "2.2.5"
 python_version = sys.version.split(' ')[0]
 bool_values = {'True', 'False'}
 
-
 def check_ffmpeg():
 	try:
 		FNULL = open(os.devnull, 'w')
@@ -26,14 +25,53 @@ def check_ffmpeg():
 
 def check_config_validity(config):
 	try:
-		username = config.get('pyinstalive', 'username')
-		password = config.get('pyinstalive', 'password')
+		settings.username = config.get('pyinstalive', 'username')
+		settings.password = config.get('pyinstalive', 'password')
+
+
+
+		try:
+			settings.show_cookie_expiry = config.get('pyinstalive', 'show_cookie_expiry').title()
+			if not settings.show_cookie_expiry in bool_values:
+				log("[W] Invalid setting detected for 'show_cookie_expiry', falling back to default value (True)", "YELLOW")
+				settings.show_cookie_expiry = 'true'
+		except:
+			log("[W] Invalid setting detected for 'show_cookie_expiry', falling back to default value (True)", "YELLOW")
+			settings.show_cookie_expiry = 'true'
+
+
 
-		if not (len(username) > 0):
+		try:
+			settings.clear_temp_files = config.get('pyinstalive', 'clear_temp_files').title()
+			if not settings.show_cookie_expiry in bool_values:
+				log("[W] Invalid setting detected for 'clear_temp_files', falling back to default value (True)", "YELLOW")
+				settings.show_cookie_expiry = 'true'
+		except:
+			log("[W] Invalid setting detected for 'clear_temp_files', falling back to default value (True)", "YELLOW")
+			settings.show_cookie_expiry = 'true'
+
+
+
+		try:
+			settings.save_path = config.get('pyinstalive', 'save_path')
+
+			if (os.path.exists(settings.save_path)):
+				pass
+			else:
+				log("[W] Invalid setting detected for 'save_path', falling back to location: " + os.getcwd(), "YELLOW")
+				settings.save_path = os.getcwd()
+
+			if not settings.save_path.endswith('/'):
+				settings.save_path = settings.save_path + '/'
+		except:
+			log("[W] Invalid setting detected for 'save_path', falling back to location: " + os.getcwd(), "YELLOW")
+			settings.save_path = os.getcwd()
+
+		if not (len(settings.username) > 0):
 			log("[E] Invalid setting detected for 'username'.", "RED")
 			return False
 
-		if not (len(password) > 0):
+		if not (len(settings.password) > 0):
 			log("[E] Invalid setting detected for 'password'.", "RED")
 			return False
 
@@ -88,9 +126,6 @@ def run():
 
 	if check_config_validity(config):
 
-		username = config.get('pyinstalive', 'username')
-		password = config.get('pyinstalive', 'password')
-
 		def show_info():
 			log("[I] To see all the available flags, use the -h flag.", "BLUE")
 			log("", "GREEN")
@@ -124,36 +159,12 @@ def run():
 			seperator("GREEN")
 			sys.exit(1)
 
-		try:
-			show_cookie_expiry = config.get('pyinstalive', 'show_cookie_expiry').title()
-			if not show_cookie_expiry in bool_values:
-				log("[W] Invalid setting detected for 'show_cookie_expiry', falling back to default value (True)", "YELLOW")
-				show_cookie_expiry = 'True'
-		except:
-			log("[W] Invalid setting detected for 'show_cookie_expiry', falling back to default value (True)", "YELLOW")
-			show_cookie_expiry = 'True'
-
-		try:
-			save_path = config.get('pyinstalive', 'save_path')
-
-			if (os.path.exists(save_path)):
-				pass
-			else:
-				log("[W] Invalid setting detected for 'save_path', falling back to location: " + os.getcwd(), "YELLOW")
-				save_path = os.getcwd()
-
-			if not save_path.endswith('/'):
-				save_path = save_path + '/'
-		except:
-			log("[W] Invalid setting detected for 'save_path', falling back to location: " + os.getcwd(), "YELLOW")
-			save_path = os.getcwd()
-
 		if (args.username is not None) and (args.password is not None):
-			api = login(args.username, args.password, show_cookie_expiry)
+			api = login(args.username, args.password, settings.show_cookie_expiry)
 		else:
-			api = login(username, password, show_cookie_expiry)
-		
-		main(api, args.record, save_path)
+			api = login(settings.username, settings.password, settings.show_cookie_expiry)
+
+		main(api, args.record, settings)
 	else:
 		log("[E] The configuration file is not valid. Please check your configuration settings and try again.", "RED")
 		seperator("GREEN")

+ 10 - 0
pyinstalive/settings.py

@@ -0,0 +1,10 @@
+import time
+
+class settings:
+	username = ""
+	password = ""
+	save_path = "/"
+	show_cookie_expiry = "true"
+	clear_temp_files = "false"
+	current_time = str(int(time.time()))
+	current_date = time.strftime("%Y%m%d")