initialize.py 5.9 KB

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