config.py 11 KB

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