initialize.py 3.7 KB

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