initialize.py 3.6 KB

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