initialize.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  9. from .downloader import main
  10. def check_ffmpeg():
  11. try:
  12. FNULL = open(os.devnull, 'w')
  13. subprocess.call(["ffmpeg"], stdout=FNULL, stderr=subprocess.STDOUT)
  14. return True
  15. except OSError as e:
  16. return False
  17. def check_config_validity(config):
  18. try:
  19. username = config['pyinstalive']['username']
  20. password = config['pyinstalive']['password']
  21. if not (len(username) > 0):
  22. log("[E] Invalid setting detected for 'username'.", "RED")
  23. return False
  24. if not (len(password) > 0):
  25. log("[E] Invalid setting detected for 'password'.", "RED")
  26. return False
  27. return True
  28. except KeyError as e:
  29. return False
  30. def run():
  31. script_version = "2.2.2"
  32. bool_values = {'True', 'False'}
  33. log('PYINSTALIVE DOWNLOADER (SCRIPT v{0!s})'.format(script_version), "GREEN")
  34. seperator("GREEN")
  35. logging.disable(logging.CRITICAL)
  36. config = configparser.ConfigParser()
  37. if os.path.exists('pyinstalive.ini'):
  38. try:
  39. config.read('pyinstalive.ini')
  40. except Exception:
  41. log("[E] Could not read configuration file. Try passing the required arguments manually.", "RED")
  42. seperator("GREEN")
  43. else:
  44. log("[W] Could not find configuration file, creating a default one ...", "YELLOW")
  45. try:
  46. config_template = "[pyinstalive]\nusername = johndoe\npassword = grapefruits\nsave_path = /\nshow_cookie_expiry = true"
  47. config_file = open("pyinstalive.ini", "w")
  48. config_file.write(config_template)
  49. config_file.close()
  50. log("[W] Edit the created 'pyinstalive.ini' file and run this script again.", "YELLOW")
  51. seperator("GREEN")
  52. sys.exit(0)
  53. except Exception as e:
  54. log("[E] Could not create default config file: " + str(e), "RED")
  55. log("[W] You must manually create and edit it with the following template: ", "YELLOW")
  56. log("", "GREEN")
  57. log(config_template, "BLUE")
  58. log("", "GREEN")
  59. log("[W] Save it as 'pyinstalive.ini' and run this script again.", "YELLOW")
  60. log("", "GREEN")
  61. sys.exit(1)
  62. parser = argparse.ArgumentParser(description='Login')
  63. parser.add_argument('-u', '--username', dest='username', type=str, required=False)
  64. parser.add_argument('-p', '--password', dest='password', type=str, required=False)
  65. parser.add_argument('-r', '--record', dest='record', type=str, required=True)
  66. args = parser.parse_args()
  67. if check_config_validity(config):
  68. username = config['pyinstalive']['username']
  69. password = config['pyinstalive']['password']
  70. if check_ffmpeg() == False:
  71. log("[E] Could not find ffmpeg, the script will now exit. ", "RED")
  72. seperator("GREEN")
  73. sys.exit(1)
  74. try:
  75. show_cookie_expiry = config['pyinstalive']['show_cookie_expiry']
  76. if not config['pyinstalive']['show_cookie_expiry'].title() in bool_values:
  77. log("[W] Invalid setting detected for 'show_cookie_expiry', falling back to default value (True)", "YELLOW")
  78. show_cookie_expiry = 'True'
  79. except:
  80. log("[W] Invalid setting detected for 'show_cookie_expiry', falling back to default value (True)", "YELLOW")
  81. show_cookie_expiry = 'True'
  82. try:
  83. save_path = config['pyinstalive']['save_path']
  84. if (os.path.exists(save_path)):
  85. pass
  86. else:
  87. log("[W] Invalid setting detected for 'save_path', falling back to location: " + os.getcwd(), "YELLOW")
  88. save_path = os.getcwd()
  89. if not save_path.endswith('/'):
  90. save_path = save_path + '/'
  91. except:
  92. log("[W] Invalid setting detected for 'save_path', falling back to location: " + os.getcwd(), "YELLOW")
  93. save_path = os.getcwd()
  94. if (args.username is not None) and (args.password is not None):
  95. api = login(args.username, args.password, show_cookie_expiry)
  96. else:
  97. api = login(username, password, show_cookie_expiry)
  98. main(api, args.record, save_path)
  99. else:
  100. log("[E] The configuration file is not valid. Please check your configuration settings and try again.", "RED")
  101. seperator("GREEN")
  102. sys.exit(0)