浏览代码

Merge branch 'execute_script_option'

Cammy 7 年之前
父节点
当前提交
749cd64dca
共有 3 个文件被更改,包括 63 次插入5 次删除
  1. 30 0
      pyinstalive/downloader.py
  2. 30 4
      pyinstalive/initialize.py
  3. 3 1
      pyinstalive/settings.py

+ 30 - 0
pyinstalive/downloader.py

@@ -2,6 +2,8 @@ import sys
 import time
 import os
 import shutil
+import subprocess
+import threading
 
 from instagram_private_api_extensions import live, replay
 from .logger import log, seperator
@@ -23,6 +25,16 @@ def main(api_arg, record_arg, settings_arg):
 	record = record_arg
 	get_user_info(record)
 
+def run_script(file):
+	try:
+		FNULL = open(os.devnull, 'w')
+		if sys.version.split(' ')[0].startswith('2'):
+			subprocess.call(["python", file], stdout=FNULL, stderr=subprocess.STDOUT)
+		else:
+			subprocess.call(["python3", file], stdout=FNULL, stderr=subprocess.STDOUT)
+	except OSError as e:
+		pass
+
 def record_stream(broadcast):
 	try:
 		def print_status():
@@ -63,6 +75,15 @@ def record_stream(broadcast):
 		log('[I] MPD URL     : ' + mpd_url, "GREEN")
 		print_status()
 		log('[I] Recording livestream... press [CTRL+C] to abort.', "GREEN")
+
+		if (settings.run_at_start is not ""):
+			try:
+				thread = threading.Thread(target=run_script, args=(settings.run_at_start,))
+				thread.daemon = True
+				thread.start()
+			except Exception as e:
+				log('[W] Could not run file: ' + str(e), "YELLOW")
+
 		dl.run()
 		stitch_video(dl, broadcast)
 	except KeyboardInterrupt:
@@ -75,6 +96,15 @@ def record_stream(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)
 		try:
 			if settings.clear_temp_files.title() == "True":

+ 30 - 4
pyinstalive/initialize.py

@@ -61,7 +61,33 @@ def check_config_validity(config):
 			log("[W] Invalid or missing setting detected for 'save_replays', using default value (True)", "YELLOW")
 			settings.save_replays = 'true'
 
+		try:
+			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 = ""
+				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 = ""
+		except:
+			log("[W] Invalid or missing settings detected for 'run_at_start', using default value (Empty)", "YELLOW")
+			settings.run_at_start = ""
 
+		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 = ""
+				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 = ""
+		except:
+			log("[W] Invalid or missing settings detected for 'run_at_finish', using default value (Empty)", "YELLOW")
+			settings.run_at_finish = ""
 
 		try:
 			settings.save_path = config.get('pyinstalive', 'save_path')
@@ -106,10 +132,10 @@ def show_info(config):
 	try:
 		settings.username = config.get('pyinstalive', 'username')
 		for file in os.listdir(os.getcwd()):
-		    if file.endswith(".json"):
-		        cookie_files.append(file)
-		    if settings.username == file.replace(".json", ''):
-		    	cookie_from_config = file
+			if file.endswith(".json"):
+				cookie_files.append(file)
+			if settings.username == file.replace(".json", ''):
+				cookie_from_config = file
 	except Exception as e:
 		log("[W] Could not check for cookie files: " + str(e), "YELLOW")
 		log("", "ENDC")

+ 3 - 1
pyinstalive/settings.py

@@ -8,4 +8,6 @@ class settings:
 	clear_temp_files = "false"
 	current_time = str(int(time.time()))
 	current_date = time.strftime("%Y%m%d")
-	save_replays = "true"
+	save_replays = "true"
+	run_at_start = ""
+	run_at_finish = ""