|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from pathlib import Path |
| 4 | +import json |
| 5 | + |
| 6 | +DEFAULT_CONFIG = { |
| 7 | + "downloadLocation": os.getcwd(), |
| 8 | + "tracknameTemplate": "%artist% - %title%", |
| 9 | + "albumTracknameTemplate": "%tracknumber% - %title%", |
| 10 | + "playlistTracknameTemplate": "%position% - %artist% - %title%", |
| 11 | + "createPlaylistFolder": True, |
| 12 | + "playlistNameTemplate": "%playlist%", |
| 13 | + "createArtistFolder": False, |
| 14 | + "artistNameTemplate": "%artist%", |
| 15 | + "createAlbumFolder": True, |
| 16 | + "albumNameTemplate": "%artist% - %album%", |
| 17 | + "createCDFolder": True, |
| 18 | + "createStructurePlaylist": False, |
| 19 | + "createSingleFolder": False, |
| 20 | + "padTracks": True, |
| 21 | + "paddingSize": "0", |
| 22 | + "illegalCharacterReplacer": "_", |
| 23 | + "queueConcurrency": 3, |
| 24 | + "maxBitrate": "3", |
| 25 | + "feelingLucky": False, |
| 26 | + "fallbackBitrate": True, |
| 27 | + "fallbackSearch": False, |
| 28 | + "fallbackISRC": False, |
| 29 | + "logErrors": True, |
| 30 | + "logSearched": False, |
| 31 | + "overwriteFile": "n", |
| 32 | + "createM3U8File": False, |
| 33 | + "playlistFilenameTemplate": "playlist", |
| 34 | + "syncedLyrics": False, |
| 35 | + "embeddedArtworkSize": 800, |
| 36 | + "embeddedArtworkPNG": False, |
| 37 | + "localArtworkSize": 1400, |
| 38 | + "localArtworkFormat": "jpg", |
| 39 | + "saveArtwork": True, |
| 40 | + "coverImageTemplate": "cover", |
| 41 | + "saveArtworkArtist": False, |
| 42 | + "artistImageTemplate": "folder", |
| 43 | + "jpegImageQuality": 90, |
| 44 | + "dateFormat": "Y-M-D", |
| 45 | + "albumVariousArtists": True, |
| 46 | + "removeAlbumVersion": False, |
| 47 | + "removeDuplicateArtists": True, |
| 48 | + "featuredToTitle": "0", |
| 49 | + "titleCasing": "nothing", |
| 50 | + "artistCasing": "nothing", |
| 51 | + "executeCommand": "", |
| 52 | + "tags": { |
| 53 | + "title": True, |
| 54 | + "artist": True, |
| 55 | + "artists": True, |
| 56 | + "album": True, |
| 57 | + "cover": True, |
| 58 | + "trackNumber": True, |
| 59 | + "trackTotal": False, |
| 60 | + "discNumber": True, |
| 61 | + "discTotal": False, |
| 62 | + "albumArtist": True, |
| 63 | + "genre": True, |
| 64 | + "year": True, |
| 65 | + "date": True, |
| 66 | + "explicit": False, |
| 67 | + "isrc": True, |
| 68 | + "length": True, |
| 69 | + "barcode": True, |
| 70 | + "bpm": True, |
| 71 | + "replayGain": False, |
| 72 | + "label": True, |
| 73 | + "lyrics": False, |
| 74 | + "syncedLyrics": False, |
| 75 | + "copyright": False, |
| 76 | + "composer": False, |
| 77 | + "involvedPeople": False, |
| 78 | + "source": False, |
| 79 | + "rating": False, |
| 80 | + "savePlaylistAsCompilation": False, |
| 81 | + "useNullSeparator": False, |
| 82 | + "saveID3v1": True, |
| 83 | + "multiArtistSeparator": "default", |
| 84 | + "singleAlbumArtist": False, |
| 85 | + "coverDescriptionUTF8": False |
| 86 | + } |
| 87 | +} |
| 88 | +DEFAULT_SPOTIFY_CONFIG = { |
| 89 | + "clientId": "", |
| 90 | + "clientSecret": "", |
| 91 | + "fallbackSearch": False |
| 92 | +} |
| 93 | + |
| 94 | +class DeemixConfig(): |
| 95 | + def __init__(self, config_folder: str = "config", arl: str = None, overwrite: bool = True) -> None: |
| 96 | + self.config_folder = Path(config_folder) |
| 97 | + if arl: |
| 98 | + self.arl = arl |
| 99 | + self._config = DEFAULT_CONFIG |
| 100 | + self._spotify_config = DEFAULT_SPOTIFY_CONFIG |
| 101 | + if not overwrite: |
| 102 | + self.load() |
| 103 | + |
| 104 | + def save(self): |
| 105 | + os.makedirs(self.config_folder / "spotify", exist_ok=True) |
| 106 | + |
| 107 | + with open(self.config_folder / "config.json", "w") as config_file: |
| 108 | + json.dump(self._config, config_file, indent=4) |
| 109 | + with open(self.config_folder / "spotify" / "config.json", "w") as config_file: |
| 110 | + json.dump(self._spotify_config, config_file, indent=4) |
| 111 | + with open(self.config_folder / ".arl", "w") as arl_file: |
| 112 | + arl_file.write(self.arl) |
| 113 | + |
| 114 | + def clear(self): |
| 115 | + shutil.rmtree(self.config_folder) |
| 116 | + |
| 117 | + def load(self): |
| 118 | + # TODO |
| 119 | + raise NotImplementedError() |
| 120 | + |
| 121 | + def setSpotifyConfig(self, client_id, secret, fallbackSearch=None): |
| 122 | + self._spotify_config["clientId"] = str(client_id) |
| 123 | + self._spotify_config["clientSecret"] = str(secret) |
| 124 | + if fallbackSearch is not None: |
| 125 | + self._spotify_config["fallbackSearch"] = bool(fallbackSearch) |
| 126 | + |
| 127 | + def setDefaultBitrate(self, bitrate, fallback=None): |
| 128 | + if bitrate == 128: |
| 129 | + self._config['maxBitrate'] = 1 |
| 130 | + elif bitrate == 320: |
| 131 | + self._config['maxBitrate'] = 3 |
| 132 | + elif bitrate == 'flac': |
| 133 | + self._config['maxBitrate'] = 9 |
| 134 | + else: |
| 135 | + raise ValueError(f"`bitrate` must be either 128, 320 or 'flac', got {bitrate}") |
| 136 | + |
| 137 | + if fallback is not None: |
| 138 | + self._config['fallbackBitrate'] = bool(fallback) |
| 139 | + |
| 140 | + def setOverwrite(self, overwrite: bool): |
| 141 | + self._config['overwriteFile'] = 'y' if overwrite else 'n' |
| 142 | + |
| 143 | + def __getitem__(self, key): |
| 144 | + return self._config[key] |
| 145 | + |
| 146 | + def __setitem__(self, key, value): |
| 147 | + self._config[key] = value |
0 commit comments