瀏覽代碼

Tweak run py script option, show stream duration on finish download

Cammy 7 年之前
父節點
當前提交
2ff8ab1952
共有 3 個文件被更改,包括 55 次插入42 次删除
  1. 35 27
      pyinstalive/downloader.py
  2. 18 13
      pyinstalive/initialize.py
  3. 2 2
      pyinstalive/settings.py

+ 35 - 27
pyinstalive/downloader.py

@@ -35,17 +35,23 @@ def run_script(file):
 	except OSError as e:
 		pass
 
+def get_stream_duration(broadcast):
+	try:
+		started_mins, started_secs = divmod((int(time.time()) - broadcast['published_time']), 60)
+		started_label = '%d minutes' % started_mins
+		if started_secs:
+			started_label += ' and %d seconds' % started_secs
+		return started_label
+	except:
+		return "not available"
+
 def record_stream(broadcast):
 	try:
 		def print_status():
 			heartbeat_info = api.broadcast_heartbeat_and_viewercount(broadcast['id'])
 			viewers = broadcast.get('viewer_count', 0)
-			started_mins, started_secs = divmod((int(time.time()) - broadcast['published_time']), 60)
-			started_label = '%d minutes' % started_mins
-			if started_secs:
-				started_label += ' and %d seconds' % started_secs
 			log('[I] Viewers     : ' + str(int(viewers)) + " watching", "GREEN")
-			log('[I] Airing time : ' + started_label, "GREEN")
+			log('[I] Airing time : ' + get_stream_duration(broadcast).title(), "GREEN")
 			log('[I] Status      : ' + heartbeat_info['broadcast_status'].title(), "GREEN")
 			seperator("GREEN")
 			return heartbeat_info['broadcast_status'] not in ['active', 'interrupted'] 
@@ -76,15 +82,17 @@ def record_stream(broadcast):
 		print_status()
 		log('[I] Recording livestream... press [CTRL+C] to abort.', "GREEN")
 
-		if (settings.run_at_start is not ""):
+		if (settings.run_at_start is not "None"):
 			try:
 				thread = threading.Thread(target=run_script, args=(settings.run_at_start,))
 				thread.daemon = True
 				thread.start()
+				log("[I] Executed file to run at start.", "GREEN")
 			except Exception as e:
 				log('[W] Could not run file: ' + str(e), "YELLOW")
 
 		dl.run()
+		log('[I] The livestream has ended. (Duration: ' + get_stream_duration(broadcast) + ")")
 		stitch_video(dl, broadcast)
 	except KeyboardInterrupt:
 		log("", "GREEN")
@@ -95,29 +103,29 @@ def record_stream(broadcast):
 			stitch_video(dl, broadcast)
 
 def stitch_video(dl, broadcast):
-		log('[I] Stitching downloaded files into video...', "GREEN")
-
-		if (settings.run_at_finish is not ""):
-			try:
-				thread = threading.Thread(target=run_script, args=(settings.run_at_finish,))
-				thread.daemon = True
-				thread.start()
-			except Exception as e:
-				log('[W] Could not run file: ' + str(e), "YELLOW")
-
-		output_file = settings.save_path + '{}_{}_{}_{}_live.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time)
+	if (settings.run_at_finish is not "None"):
 		try:
-			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)
+			thread = threading.Thread(target=run_script, args=(settings.run_at_finish,))
+			thread.daemon = True
+			thread.start()
+			log("[I] Executed file to run at finish.", "GREEN")
 		except Exception as e:
-			log('[E] Could not stitch downloaded files: ' + str(e), "RED")
-			seperator("GREEN")
-			sys.exit(1)
+			log('[W] Could not run file: ' + str(e), "YELLOW")
+
+	log('[I] Stitching downloaded files into video...', "GREEN")
+	output_file = settings.save_path + '{}_{}_{}_{}_live.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time)
+	try:
+		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)
+	except Exception as e:
+		log('[E] Could not stitch downloaded files: ' + str(e), "RED")
+		seperator("GREEN")
+		sys.exit(1)
 
 def get_user_info(record):
 	try:

+ 18 - 13
pyinstalive/initialize.py

@@ -65,29 +65,29 @@ def check_config_validity(config):
 			settings.run_at_start = config.get('pyinstalive', 'run_at_start')
 			if (len(settings.run_at_start) > 0):
 				if not os.path.isfile(settings.run_at_start):
-					log("[W] Path to file given for 'run_at_start' does not exist, using default value (Empty)", "YELLOW")
-					settings.run_at_start = ""
+					log("[W] Path to file given for 'run_at_start' does not exist, using default value (None)", "YELLOW")
+					settings.run_at_start = "None"
 				else:
 					if not settings.run_at_start.split('.')[-1] == 'py':
-						log("[W] File given for 'run_at_start' is not a Python script, using default value (Empty)", "YELLOW")
-						settings.run_at_start = ""
+						log("[W] File given for 'run_at_start' is not a Python script, using default value (None)", "YELLOW")
+						settings.run_at_start = "None"
 		except:
-			log("[W] Invalid or missing settings detected for 'run_at_start', using default value (Empty)", "YELLOW")
-			settings.run_at_start = ""
+			log("[W] Invalid or missing settings detected for 'run_at_start', using default value (None)", "YELLOW")
+			settings.run_at_start = "None"
 
 		try:
 			settings.run_at_finish = config.get('pyinstalive', 'run_at_finish')
 			if (len(settings.run_at_finish) > 0):
 				if not os.path.isfile(settings.run_at_finish):
-					log("[W] Path to file given for 'run_at_finish' does not exist, using default value (Empty)", "YELLOW")
-					settings.run_at_finish = ""
+					log("[W] Path to file given for 'run_at_finish' does not exist, using default value (None)", "YELLOW")
+					settings.run_at_finish = "None"
 				else:
 					if not settings.run_at_finish.split('.')[-1] == 'py':
-						log("[W] File given for 'run_at_finish' is not a Python script, using default value (Empty)", "YELLOW")
-						settings.run_at_finish = ""
+						log("[W] File given for 'run_at_finish' is not a Python script, using default value (None)", "YELLOW")
+						settings.run_at_finish = "None"
 		except:
-			log("[W] Invalid or missing settings detected for 'run_at_finish', using default value (Empty)", "YELLOW")
-			settings.run_at_finish = ""
+			log("[W] Invalid or missing settings detected for 'run_at_finish', using default value (None)", "YELLOW")
+			settings.run_at_finish = "None"
 
 		try:
 			settings.save_path = config.get('pyinstalive', 'save_path')
@@ -156,6 +156,11 @@ def show_info(config):
 		log("[W] Cookie files:            	None found", "YELLOW")
 
 	log("[I] CLI supports color:     	" + str(supports_color()), "GREEN")
+	log("[I] File to run at start:       " + settings.run_at_start, "GREEN")
+	log("[I] File to run at finish:      " + settings.run_at_finish, "GREEN")
+	log("", "GREEN")
+
+
 
 	if os.path.exists('pyinstalive.ini'):
 		log("[I] Config file:", "GREEN")
@@ -184,7 +189,7 @@ def new_config():
 		else:
 			try:
 				log("[W] Could not find configuration file, creating a default one...", "YELLOW")
-				config_template = "[pyinstalive]\nusername = johndoe\npassword = grapefruits\nsave_path = " + os.getcwd() + "\nshow_cookie_expiry = true\nclear_temp_files = false\nsave_replays = true"
+				config_template = "[pyinstalive]\nusername = johndoe\npassword = grapefruits\nsave_path = " + os.getcwd() + "\nshow_cookie_expiry = true\nclear_temp_files = false\nsave_replays = true\nrun_at_start = \nrun_at_finish = \n"
 				config_file = open("pyinstalive.ini", "w")
 				config_file.write(config_template)
 				config_file.close()

+ 2 - 2
pyinstalive/settings.py

@@ -9,5 +9,5 @@ class settings:
 	current_time = str(int(time.time()))
 	current_date = time.strftime("%Y%m%d")
 	save_replays = "true"
-	run_at_start = ""
-	run_at_finish = ""
+	run_at_start = "None"
+	run_at_finish = "None"