config.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import json
  2. import os
  3. from typing import Any
  4. CONFIG_FILE_PATH = '../zs_config.json'
  5. ROOT_PATH = 'ROOT_PATH'
  6. ROOT_PODCAST_PATH = 'ROOT_PODCAST_PATH'
  7. SKIP_EXISTING_FILES = 'SKIP_EXISTING_FILES'
  8. SKIP_PREVIOUSLY_DOWNLOADED = 'SKIP_PREVIOUSLY_DOWNLOADED'
  9. DOWNLOAD_FORMAT = 'DOWNLOAD_FORMAT'
  10. FORCE_PREMIUM = 'FORCE_PREMIUM'
  11. ANTI_BAN_WAIT_TIME = 'ANTI_BAN_WAIT_TIME'
  12. OVERRIDE_AUTO_WAIT = 'OVERRIDE_AUTO_WAIT'
  13. CHUNK_SIZE = 'CHUNK_SIZE'
  14. SPLIT_ALBUM_DISCS = 'SPLIT_ALBUM_DISCS'
  15. DOWNLOAD_REAL_TIME = 'DOWNLOAD_REAL_TIME'
  16. LANGUAGE = 'LANGUAGE'
  17. BITRATE = 'BITRATE'
  18. SONG_ARCHIVE = 'SONG_ARCHIVE'
  19. CREDENTIALS_LOCATION = 'CREDENTIALS_LOCATION'
  20. OUTPUT = 'OUTPUT'
  21. PRINT_SPLASH = 'PRINT_SPLASH'
  22. PRINT_SKIPS = 'PRINT_SKIPS'
  23. PRINT_DOWNLOAD_PROGRESS = 'PRINT_DOWNLOAD_PROGRESS'
  24. PRINT_ERRORS = 'PRINT_ERRORS'
  25. PRINT_DOWNLOADS = 'PRINT_DOWNLOADS'
  26. PRINT_API_ERRORS = 'PRINT_API_ERRORS'
  27. TEMP_DOWNLOAD_DIR = 'TEMP_DOWNLOAD_DIR'
  28. MD_ALLGENRES = 'MD_ALLGENRES'
  29. MD_GENREDELIMITER = 'MD_GENREDELIMITER'
  30. PRINT_PROGRESS_INFO = 'PRINT_PROGRESS_INFO'
  31. PRINT_WARNINGS = 'PRINT_WARNINGS'
  32. RETRY_ATTEMPTS = 'RETRY_ATTEMPTS'
  33. CONFIG_VALUES = {
  34. ROOT_PATH: { 'default': '../ZSpotify Music/', 'type': str, 'arg': '--root-path' },
  35. ROOT_PODCAST_PATH: { 'default': '../ZSpotify Podcasts/', 'type': str, 'arg': '--root-podcast-path' },
  36. SKIP_EXISTING_FILES: { 'default': 'True', 'type': bool, 'arg': '--skip-existing-files' },
  37. SKIP_PREVIOUSLY_DOWNLOADED: { 'default': 'False', 'type': bool, 'arg': '--skip-previously-downloaded' },
  38. RETRY_ATTEMPTS: { 'default': '5', 'type': int, 'arg': '--retry-attemps' },
  39. DOWNLOAD_FORMAT: { 'default': 'ogg', 'type': str, 'arg': '--download-format' },
  40. FORCE_PREMIUM: { 'default': 'False', 'type': bool, 'arg': '--force-premium' },
  41. ANTI_BAN_WAIT_TIME: { 'default': '1', 'type': int, 'arg': '--anti-ban-wait-time' },
  42. OVERRIDE_AUTO_WAIT: { 'default': 'False', 'type': bool, 'arg': '--override-auto-wait' },
  43. CHUNK_SIZE: { 'default': '50000', 'type': int, 'arg': '--chunk-size' },
  44. SPLIT_ALBUM_DISCS: { 'default': 'False', 'type': bool, 'arg': '--split-album-discs' },
  45. DOWNLOAD_REAL_TIME: { 'default': 'False', 'type': bool, 'arg': '--download-real-time' },
  46. LANGUAGE: { 'default': 'en', 'type': str, 'arg': '--language' },
  47. BITRATE: { 'default': '', 'type': str, 'arg': '--bitrate' },
  48. SONG_ARCHIVE: { 'default': '.song_archive', 'type': str, 'arg': '--song-archive' },
  49. CREDENTIALS_LOCATION: { 'default': 'credentials.json', 'type': str, 'arg': '--credentials-location' },
  50. OUTPUT: { 'default': '', 'type': str, 'arg': '--output' },
  51. PRINT_SPLASH: { 'default': 'False', 'type': bool, 'arg': '--print-splash' },
  52. PRINT_SKIPS: { 'default': 'True', 'type': bool, 'arg': '--print-skips' },
  53. PRINT_DOWNLOAD_PROGRESS: { 'default': 'True', 'type': bool, 'arg': '--print-download-progress' },
  54. PRINT_ERRORS: { 'default': 'True', 'type': bool, 'arg': '--print-errors' },
  55. PRINT_DOWNLOADS: { 'default': 'False', 'type': bool, 'arg': '--print-downloads' },
  56. PRINT_API_ERRORS: { 'default': 'False', 'type': bool, 'arg': '--print-api-errors' },
  57. PRINT_PROGRESS_INFO: { 'default': 'True', 'type': bool, 'arg': '--print-progress-info' },
  58. PRINT_WARNINGS: { 'default': 'True', 'type': bool, 'arg': '--print-warnings' },
  59. MD_ALLGENRES: { 'default': 'False', 'type': bool, 'arg': '--md-allgenres' },
  60. MD_GENREDELIMITER: { 'default': ';', 'type': str, 'arg': '--md-genredelimiter' },
  61. TEMP_DOWNLOAD_DIR: { 'default': '', 'type': str, 'arg': '--temp-download-dir' }
  62. }
  63. OUTPUT_DEFAULT_PLAYLIST = '{playlist}/{artist} - {song_name}.{ext}'
  64. OUTPUT_DEFAULT_PLAYLIST_EXT = '{playlist}/{playlist_num} - {artist} - {song_name}.{ext}'
  65. OUTPUT_DEFAULT_LIKED_SONGS = 'Liked Songs/{artist} - {song_name}.{ext}'
  66. OUTPUT_DEFAULT_SINGLE = '{artist} - {song_name}.{ext}'
  67. OUTPUT_DEFAULT_ALBUM = '{artist}/{album}/{album_num} - {artist} - {song_name}.{ext}'
  68. class Config:
  69. Values = {}
  70. @classmethod
  71. def load(cls, args) -> None:
  72. app_dir = os.path.dirname(__file__)
  73. config_fp = CONFIG_FILE_PATH
  74. if args.config_location:
  75. config_fp = args.config_location
  76. true_config_file_path = os.path.join(app_dir, config_fp)
  77. # Load config from zs_config.json
  78. if not os.path.exists(true_config_file_path):
  79. with open(true_config_file_path, 'w', encoding='utf-8') as config_file:
  80. json.dump(cls.get_default_json(), config_file, indent=4)
  81. cls.Values = cls.get_default_json()
  82. else:
  83. with open(true_config_file_path, encoding='utf-8') as config_file:
  84. jsonvalues = json.load(config_file)
  85. cls.Values = {}
  86. for key in CONFIG_VALUES:
  87. if key in jsonvalues:
  88. cls.Values[key] = cls.parse_arg_value(key, jsonvalues[key])
  89. # Add default values for missing keys
  90. for key in CONFIG_VALUES:
  91. if key not in cls.Values:
  92. cls.Values[key] = cls.parse_arg_value(key, CONFIG_VALUES[key]['default'])
  93. # Override config from commandline arguments
  94. for key in CONFIG_VALUES:
  95. if key.lower() in vars(args) and vars(args)[key.lower()] is not None:
  96. cls.Values[key] = cls.parse_arg_value(key, vars(args)[key.lower()])
  97. if args.no_splash:
  98. cls.Values[PRINT_SPLASH] = False
  99. @classmethod
  100. def get_default_json(cls) -> Any:
  101. r = {}
  102. for key in CONFIG_VALUES:
  103. r[key] = CONFIG_VALUES[key]['default']
  104. return r
  105. @classmethod
  106. def parse_arg_value(cls, key: str, value: Any) -> Any:
  107. if type(value) == CONFIG_VALUES[key]['type']:
  108. return value
  109. if CONFIG_VALUES[key]['type'] == str:
  110. return str(value)
  111. if CONFIG_VALUES[key]['type'] == int:
  112. return int(value)
  113. if CONFIG_VALUES[key]['type'] == bool:
  114. if str(value).lower() in ['yes', 'true', '1']:
  115. return True
  116. if str(value).lower() in ['no', 'false', '0']:
  117. return False
  118. raise ValueError("Not a boolean: " + value)
  119. raise ValueError("Unknown Type: " + value)
  120. @classmethod
  121. def get(cls, key: str) -> Any:
  122. return cls.Values.get(key)
  123. @classmethod
  124. def get_root_path(cls) -> str:
  125. return os.path.join(os.path.dirname(__file__), cls.get(ROOT_PATH))
  126. @classmethod
  127. def get_root_podcast_path(cls) -> str:
  128. return os.path.join(os.path.dirname(__file__), cls.get(ROOT_PODCAST_PATH))
  129. @classmethod
  130. def get_skip_existing_files(cls) -> bool:
  131. return cls.get(SKIP_EXISTING_FILES)
  132. @classmethod
  133. def get_skip_previously_downloaded(cls) -> bool:
  134. return cls.get(SKIP_PREVIOUSLY_DOWNLOADED)
  135. @classmethod
  136. def get_split_album_discs(cls) -> bool:
  137. return cls.get(SPLIT_ALBUM_DISCS)
  138. @classmethod
  139. def get_chunk_size(cls) -> int:
  140. return cls.get(CHUNK_SIZE)
  141. @classmethod
  142. def get_override_auto_wait(cls) -> bool:
  143. return cls.get(OVERRIDE_AUTO_WAIT)
  144. @classmethod
  145. def get_force_premium(cls) -> bool:
  146. return cls.get(FORCE_PREMIUM)
  147. @classmethod
  148. def get_download_format(cls) -> str:
  149. return cls.get(DOWNLOAD_FORMAT)
  150. @classmethod
  151. def get_anti_ban_wait_time(cls) -> int:
  152. return cls.get(ANTI_BAN_WAIT_TIME)
  153. @classmethod
  154. def get_language(cls) -> str:
  155. return cls.get(LANGUAGE)
  156. @classmethod
  157. def get_download_real_time(cls) -> bool:
  158. return cls.get(DOWNLOAD_REAL_TIME)
  159. @classmethod
  160. def get_bitrate(cls) -> str:
  161. return cls.get(BITRATE)
  162. @classmethod
  163. def get_song_archive(cls) -> str:
  164. return os.path.join(cls.get_root_path(), cls.get(SONG_ARCHIVE))
  165. @classmethod
  166. def get_credentials_location(cls) -> str:
  167. return os.path.join(os.getcwd(), cls.get(CREDENTIALS_LOCATION))
  168. @classmethod
  169. def get_temp_download_dir(cls) -> str:
  170. if cls.get(TEMP_DOWNLOAD_DIR) == '':
  171. return ''
  172. return os.path.join(cls.get_root_path(), cls.get(TEMP_DOWNLOAD_DIR))
  173. @classmethod
  174. def get_all_genres(cls) -> bool:
  175. return cls.get(MD_ALLGENRES)
  176. @classmethod
  177. def get_all_genres_delimiter(cls) -> bool:
  178. return cls.get(MD_GENREDELIMITER)
  179. @classmethod
  180. def get_output(cls, mode: str) -> str:
  181. v = cls.get(OUTPUT)
  182. if v:
  183. return v
  184. if mode == 'playlist':
  185. if cls.get_split_album_discs():
  186. split = os.path.split(OUTPUT_DEFAULT_PLAYLIST)
  187. return os.path.join(split[0], 'Disc {disc_number}', split[0])
  188. return OUTPUT_DEFAULT_PLAYLIST
  189. if mode == 'extplaylist':
  190. if cls.get_split_album_discs():
  191. split = os.path.split(OUTPUT_DEFAULT_PLAYLIST_EXT)
  192. return os.path.join(split[0], 'Disc {disc_number}', split[0])
  193. return OUTPUT_DEFAULT_PLAYLIST_EXT
  194. if mode == 'liked':
  195. if cls.get_split_album_discs():
  196. split = os.path.split(OUTPUT_DEFAULT_LIKED_SONGS)
  197. return os.path.join(split[0], 'Disc {disc_number}', split[0])
  198. return OUTPUT_DEFAULT_LIKED_SONGS
  199. if mode == 'single':
  200. if cls.get_split_album_discs():
  201. split = os.path.split(OUTPUT_DEFAULT_SINGLE)
  202. return os.path.join(split[0], 'Disc {disc_number}', split[0])
  203. return OUTPUT_DEFAULT_SINGLE
  204. if mode == 'album':
  205. if cls.get_split_album_discs():
  206. split = os.path.split(OUTPUT_DEFAULT_ALBUM)
  207. return os.path.join(split[0], 'Disc {disc_number}', split[0])
  208. return OUTPUT_DEFAULT_ALBUM
  209. raise ValueError()
  210. @classmethod
  211. def get_retry_attempts(cls) -> int:
  212. return cls.get(RETRY_ATTEMPTS)