initialize.py 4.0 KB

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