initialize.py 5.3 KB

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