Procházet zdrojové kódy

Add setting to toggle use of lock files, small logger color change

Cammy před 6 roky
rodič
revize
374a66f442
5 změnil soubory, kde provedl 168 přidání a 146 odebrání
  1. 4 0
      .gitignore
  2. 6 3
      pyinstalive/downloader.py
  3. 14 0
      pyinstalive/initialize.py
  4. 142 142
      pyinstalive/logger.py
  5. 2 1
      pyinstalive/settings.py

+ 4 - 0
.gitignore

@@ -53,3 +53,7 @@ pyinstalive_win\.py
 win_building/
 
 *.exe
+
+pildev\.ini
+
+pildev/

+ 6 - 3
pyinstalive/downloader.py

@@ -30,7 +30,8 @@ def start_single(instagram_api_arg, download_arg, settings_arg):
 	user_to_download = download_arg
 	try:
 		if not os.path.isfile(os.path.join(settings.save_path, user_to_download + '.lock')):
-			open(os.path.join(settings.save_path, user_to_download + '.lock'), 'a').close()
+			if settings.use_locks.title() == "True":
+				open(os.path.join(settings.save_path, user_to_download + '.lock'), 'a').close()
 		else:
 			log_warn("Lock file is already present for this user, there is probably another download ongoing!")
 			log_warn("If this is not the case, manually delete the file '{:s}' and try again.".format(user_to_download + '.lock'))
@@ -198,7 +199,8 @@ def download_livestream(broadcast):
 		print_status(False)
 		log_info_green('MPD URL     : {:s}'.format(mpd_url))
 		log_seperator()
-		open(os.path.join(output_dir, 'folder.lock'), 'a').close()
+		if settings.use_locks.title() == "True":
+			open(os.path.join(output_dir, 'folder.lock'), 'a').close()
 		log_info_green('Downloading livestream... press [CTRL+C] to abort.')
 
 		if (settings.run_at_start is not "None"):
@@ -495,7 +497,8 @@ def download_replays(broadcasts):
 					output_dir=output_dir,
 					user_agent=instagram_api.user_agent,
 					ffmpeg_binary=settings.ffmpeg_path)
-				open(os.path.join(output_dir, 'folder.lock'), 'a').close()
+				if settings.use_locks.title() == "True":
+					open(os.path.join(output_dir, 'folder.lock'), 'a').close()
 				replay_mp4_file = '{}{}_{}_{}_{}_replay.mp4'.format(settings.save_path, settings.current_date, user_to_download, broadcast.get('id'), settings.current_time)
 				replay_json_file = os.path.join(output_dir, '{}_{}_{}_{}_replay_comments.json'.format(settings.current_date, user_to_download, broadcast.get('id'), settings.current_time))
 

+ 14 - 0
pyinstalive/initialize.py

@@ -43,6 +43,17 @@ def check_config_validity(config, args=None):
 		settings.username = config.get('pyinstalive', 'username')
 		settings.password = config.get('pyinstalive', 'password')
 
+		try:
+			settings.use_locks = config.get('pyinstalive', 'use_locks').title()
+			if not settings.use_locks in bool_values:
+				log_warn("Invalid or missing setting detected for 'use_locks', using default value (True)")
+				settings.use_locks = 'true'
+				has_thrown_errors = True
+		except:
+			log_warn("Invalid or missing setting detected for 'use_locks', using default value (True)")
+			settings.use_locks = 'true'
+			has_thrown_errors = True
+
 		try:
 			settings.show_cookie_expiry = config.get('pyinstalive', 'show_cookie_expiry').title()
 			if not settings.show_cookie_expiry in bool_values:
@@ -541,6 +552,9 @@ def run():
 			if args.download and not args.downloadfollowing:
 				start_single(api, args.download, settings)
 			if not args.download and args.downloadfollowing:
+				if settings.use_locks.title() == "False":
+					log_warn("The use of lock files is disabled, this might cause trouble!")
+					log_seperator()
 				if check_pyinstalive():
 					start_multiple(api, settings, "pyinstalive", args)
 				else:

+ 142 - 142
pyinstalive/logger.py

@@ -1,143 +1,143 @@
-import os
-import sys
-import re
-from .settings import settings
-
-sep = "-" * 70
-
-def supports_color():
-	try:
-		"""
-		from https://github.com/django/django/blob/master/django/core/management/color.py
-		Return True if the running system's terminal supports color,
-		and False otherwise.
-		"""
-
-		plat = sys.platform
-		supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
-
-		# isatty is not always implemented, #6223.
-		is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
-		if not supported_platform or not is_a_tty:
-			return "No", False
-		return "Yes", True
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-
-def log_seperator():
-	try:
-		print(sep)
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write(sep + '\n')
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-def log_info_green(string):
-	try:
-		if supports_color()[1] == False:
-			print(string)
-		else:
-			print('[\x1B[1;32;40mI\x1B[0m] {:s}\x1B[0m'.format(string))
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write("[I] {:s}\n".format(string))
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-def log_info_blue(string):
-	try:
-		if supports_color()[1] == False:
-			print(string)
-		else:
-			print('[\x1B[1;34;40mI\x1B[0m] {:s}\x1B[0m'.format(string))
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write("[I] {:s}\n".format(string))
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-def log_warn(string):
-	try:
-		if supports_color()[1] == False:
-			print(string)
-		else:
-			print('[\x1B[1;33;40mW\x1B[0m] {:s}\x1B[0m'.format(string))
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write("[W] {:s}\n".format(string))
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-def log_error(string):
-	try:
-		if supports_color()[1] == False:
-			print(string)
-		else:
-			print('[\x1B[1;31;40mE\x1B[0m] {:s}\x1B[0m'.format(string))
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write("[E] {:s}\n".format(string))
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-def log_whiteline():
-	try:
-		print("")
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write("\n")
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
-		print("Error while logging: {}" + str(e))
-
-
-def log_plain(string):
-	try:
-		print(string)
-		if settings.log_to_file == 'True':
-			try:
-				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
-					f.write("{:s}\n".format(string))
-					f.close()
-			except:
-				pass
-		sys.stdout.flush()
-	except Exception as e:
+import os
+import sys
+import re
+from .settings import settings
+
+sep = "-" * 70
+
+def supports_color():
+	try:
+		"""
+		from https://github.com/django/django/blob/master/django/core/management/color.py
+		Return True if the running system's terminal supports color,
+		and False otherwise.
+		"""
+
+		plat = sys.platform
+		supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
+
+		# isatty is not always implemented, #6223.
+		is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
+		if not supported_platform or not is_a_tty:
+			return "No", False
+		return "Yes", True
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+
+def log_seperator():
+	try:
+		print(sep)
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write(sep + '\n')
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+def log_info_green(string):
+	try:
+		if supports_color()[1] == False:
+			print(string)
+		else:
+			print('\x1B[1;32;40m[I]\x1B[0m {:s}\x1B[0m'.format(string))
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write("[I] {:s}\n".format(string))
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+def log_info_blue(string):
+	try:
+		if supports_color()[1] == False:
+			print(string)
+		else:
+			print('\x1B[1;34;40m[I]\x1B[0m {:s}\x1B[0m'.format(string))
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write("[I] {:s}\n".format(string))
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+def log_warn(string):
+	try:
+		if supports_color()[1] == False:
+			print(string)
+		else:
+			print('\x1B[1;33;40m[W]\x1B[0m {:s}\x1B[0m'.format(string))
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write("[W] {:s}\n".format(string))
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+def log_error(string):
+	try:
+		if supports_color()[1] == False:
+			print(string)
+		else:
+			print('\x1B[1;31;40m[E]\x1B[0m {:s}\x1B[0m'.format(string))
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write("[E] {:s}\n".format(string))
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+def log_whiteline():
+	try:
+		print("")
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write("\n")
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
+		print("Error while logging: {}" + str(e))
+
+
+def log_plain(string):
+	try:
+		print(string)
+		if settings.log_to_file == 'True':
+			try:
+				with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
+					f.write("{:s}\n".format(string))
+					f.close()
+			except:
+				pass
+		sys.stdout.flush()
+	except Exception as e:
 		print("Error while logging: {}" + str(e))

+ 2 - 1
pyinstalive/settings.py

@@ -17,4 +17,5 @@ class settings:
 	run_at_finish = "None"
 	save_comments = "true"
 	log_to_file = "false"
-	custom_config_path = 'pyinstalive.ini'
+	custom_config_path = 'pyinstalive.ini'
+	use_locks = "false"