initialize.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import argparse
  2. import configparser
  3. import logging
  4. import os.path
  5. import sys
  6. import subprocess
  7. import time
  8. from .auth import login
  9. from .logger import log, seperator, supports_color
  10. from .downloader import main
  11. from .settings import settings
  12. script_version = "2.2.8"
  13. python_version = sys.version.split(' ')[0]
  14. bool_values = {'True', 'False'}
  15. def check_ffmpeg():
  16. try:
  17. FNULL = open(os.devnull, 'w')
  18. subprocess.call(["ffmpeg"], stdout=FNULL, stderr=subprocess.STDOUT)
  19. return True
  20. except OSError as e:
  21. return False
  22. def check_config_validity(config):
  23. try:
  24. settings.username = config.get('pyinstalive', 'username')
  25. settings.password = config.get('pyinstalive', 'password')
  26. try:
  27. settings.show_cookie_expiry = config.get('pyinstalive', 'show_cookie_expiry').title()
  28. if not settings.show_cookie_expiry in bool_values:
  29. log("[W] Invalid or missing setting detected for 'show_cookie_expiry', using default value (True)", "YELLOW")
  30. settings.show_cookie_expiry = 'true'
  31. except:
  32. log("[W] Invalid or missing setting detected for 'show_cookie_expiry', using default value (True)", "YELLOW")
  33. settings.show_cookie_expiry = 'true'
  34. try:
  35. settings.clear_temp_files = config.get('pyinstalive', 'clear_temp_files').title()
  36. if not settings.clear_temp_files in bool_values:
  37. log("[W] Invalid or missing setting detected for 'clear_temp_files', using default value (True)", "YELLOW")
  38. settings.clear_temp_files = 'true'
  39. except:
  40. log("[W] Invalid or missing setting detected for 'clear_temp_files', using default value (True)", "YELLOW")
  41. settings.clear_temp_files = 'true'
  42. try:
  43. settings.save_replays = config.get('pyinstalive', 'save_replays').title()
  44. if not settings.save_replays in bool_values:
  45. log("[W] Invalid or missing setting detected for 'save_replays', using default value (True)", "YELLOW")
  46. settings.save_replays = 'true'
  47. except:
  48. log("[W] Invalid or missing setting detected for 'save_replays', using default value (True)", "YELLOW")
  49. settings.save_replays = 'true'
  50. try:
  51. settings.save_path = config.get('pyinstalive', 'save_path')
  52. if (os.path.exists(settings.save_path)):
  53. pass
  54. else:
  55. log("[W] Invalid or missing setting detected for 'save_path', falling back to path: " + os.getcwd(), "YELLOW")
  56. settings.save_path = os.getcwd()
  57. if not settings.save_path.endswith('/'):
  58. settings.save_path = settings.save_path + '/'
  59. except:
  60. log("[W] Invalid or missing setting detected for 'save_path', falling back to path: " + os.getcwd(), "YELLOW")
  61. settings.save_path = os.getcwd()
  62. if not (len(settings.username) > 0):
  63. log("[E] Invalid or missing setting detected for 'username'.", "RED")
  64. return False
  65. if not (len(settings.password) > 0):
  66. log("[E] Invalid or missing setting detected for 'password'.", "RED")
  67. return False
  68. return True
  69. except Exception as e:
  70. return False
  71. def show_info():
  72. log("[I] To see all the available flags, use the -h flag.", "BLUE")
  73. log("", "GREEN")
  74. log("[I] PyInstaLive version: " + script_version, "GREEN")
  75. log("[I] Python version: " + python_version, "GREEN")
  76. if check_ffmpeg() == False:
  77. log("[E] FFmpeg framework: Not found", "RED")
  78. else:
  79. log("[I] FFmpeg framework: Available", "GREEN")
  80. if not os.path.isfile(settings.username + '.json'):
  81. log("[W] Cookie file: Not found", "YELLOW")
  82. else:
  83. log("[I] Cookie file: Available", "GREEN")
  84. log("[I] CLI supports color: " + str(supports_color()), "GREEN")
  85. if os.path.exists('pyinstalive.ini'):
  86. log("[I] Config file:", "GREEN")
  87. log("", "GREEN")
  88. with open('pyinstalive.ini') as f:
  89. for line in f:
  90. log(" " + line.rstrip(), "YELLOW")
  91. else:
  92. log("[E] Config file: Not found", "RED")
  93. log("", "GREEN")
  94. log("[I] End of PyInstaLive information screen.", "GREEN")
  95. seperator("GREEN")
  96. def new_config():
  97. try:
  98. if os.path.exists('pyinstalive.ini'):
  99. log("[I] A configuration file is already present:", "GREEN")
  100. log("", "GREEN")
  101. with open('pyinstalive.ini') as f:
  102. for line in f:
  103. log(" " + line.rstrip(), "YELLOW")
  104. log("", "GREEN")
  105. log("[W] To create a default config file, delete 'pyinstalive.ini' and ", "YELLOW")
  106. log(" run this script again.", "YELLOW")
  107. seperator("GREEN")
  108. else:
  109. try:
  110. log("[W] Could not find configuration file, creating a default one ...", "YELLOW")
  111. config_template = "[pyinstalive]\nusername = johndoe\npassword = grapefruits\nsave_path = " + os.getcwd() + "\nshow_cookie_expiry = true\nclear_temp_files = false\nsave_replays = true"
  112. config_file = open("pyinstalive.ini", "w")
  113. config_file.write(config_template)
  114. config_file.close()
  115. log("[W] Edit the created 'pyinstalive.ini' file and run this script again.", "YELLOW")
  116. seperator("GREEN")
  117. sys.exit(0)
  118. except Exception as e:
  119. log("[E] Could not create default config file: " + str(e), "RED")
  120. log("[W] You must manually create and edit it with the following template: ", "YELLOW")
  121. log("", "GREEN")
  122. log(config_template, "YELLOW")
  123. log("", "GREEN")
  124. log("[W] Save it as 'pyinstalive.ini' and run this script again.", "YELLOW")
  125. log("", "GREEN")
  126. except Exception as e:
  127. log("[E] An error occurred: " + str(e), "RED")
  128. log("[W] If you don't have a configuration file, you must", "YELLOW")
  129. log(" manually create and edit it with the following template: ", "YELLOW")
  130. log("", "GREEN")
  131. log(config_template, "YELLOW")
  132. log("", "GREEN")
  133. log("[W] Save it as 'pyinstalive.ini' and run this script again.", "YELLOW")
  134. log("", "GREEN")
  135. def run():
  136. seperator("GREEN")
  137. log('PYINSTALIVE (SCRIPT V{} - PYTHON V{}) - {}'.format(script_version, python_version, time.strftime('%H:%M:%S %p')), "GREEN")
  138. seperator("GREEN")
  139. logging.disable(logging.CRITICAL)
  140. config = configparser.ConfigParser()
  141. parser = argparse.ArgumentParser(description='You are running PyInstaLive ' + script_version + " with Python " + python_version)
  142. parser.add_argument('-u', '--username', dest='username', type=str, required=False, help="Instagram username to login with.")
  143. parser.add_argument('-p', '--password', dest='password', type=str, required=False, help="Instagram password to login with.")
  144. 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.")
  145. parser.add_argument('-i', '--info', dest='info', action='store_true', help="View information about PyInstaLive.")
  146. parser.add_argument('-c', '--config', dest='config', action='store_true', help="Create a default configuration file if it doesn't exist.")
  147. args = parser.parse_args()
  148. if (args.username == None and args.password == None and args.record == None and args.info == False and args.config == False) or (args.info != False):
  149. show_info()
  150. sys.exit(0)
  151. if (args.config == True):
  152. new_config()
  153. sys.exit(0)
  154. if os.path.exists('pyinstalive.ini'):
  155. try:
  156. config.read('pyinstalive.ini')
  157. except Exception:
  158. log("[E] Could not read configuration file. Try passing the required arguments manually.", "RED")
  159. seperator("GREEN")
  160. else:
  161. new_config()
  162. sys.exit(1)
  163. if check_config_validity(config):
  164. if check_ffmpeg() == False:
  165. log("[E] Could not find ffmpeg, the script will now exit. ", "RED")
  166. seperator("GREEN")
  167. sys.exit(1)
  168. if (args.username is not None) and (args.password is not None):
  169. api = login(args.username, args.password, settings.show_cookie_expiry, True)
  170. else:
  171. api = login(settings.username, settings.password, settings.show_cookie_expiry, False)
  172. main(api, args.record, settings)
  173. else:
  174. log("[E] The configuration file is not valid. Please check your configuration settings and try again.", "RED")
  175. seperator("GREEN")
  176. sys.exit(0)