deemix-py/deemix/settings.py

153 lines
5.2 KiB
Python
Raw Permalink Normal View History

import json
2021-10-07 14:18:50 +00:00
from copy import deepcopy
from pathlib import Path
from os import makedirs
from deezer import TrackFormats
import deemix.utils.localpaths as localpaths
class OverwriteOption():
"""Should the lib overwrite files?"""
OVERWRITE = 'y' # Yes, overwrite the file
DONT_OVERWRITE = 'n' # No, don't overwrite the file
DONT_CHECK_EXT = 'e' # No, and don't check for extensions
KEEP_BOTH = 'b' # No, and keep both files
ONLY_TAGS = 't' # Overwrite only the tags
class FeaturesOption():
"""What should I do with featured artists?"""
NO_CHANGE = "0" # Do nothing
REMOVE_TITLE = "1" # Remove from track title
REMOVE_TITLE_ALBUM = "3" # Remove from track title and album title
MOVE_TITLE = "2" # Move to track title
DEFAULTS = {
2024-07-29 15:53:37 +00:00
"albumNameTemplate": "%artist% - %album%",
"albumTracknameTemplate": "%tracknumber% - %title%",
2024-07-29 15:53:37 +00:00
"albumVariousArtists": True,
"artistCasing": "nothing",
"artistImageTemplate": "folder",
"artistNameTemplate": "%artist%",
2024-07-29 15:53:37 +00:00
"coverImageTemplate": "cover",
"createAlbumFolder": True,
2024-07-29 15:53:37 +00:00
"createArtistFolder": False,
"createCDFolder": True,
2024-07-29 15:53:37 +00:00
"createM3U8File": False,
"createPlaylistFolder": True,
"createSingleFolder": False,
2024-07-29 15:53:37 +00:00
"createStructurePlaylist": False,
"dateFormat": "Y-M-D",
"downloadLocation": str(localpaths.getMusicFolder()),
"embeddedArtworkPNG": False,
"embeddedArtworkSize": 800,
"executeCommand": "",
2021-12-19 16:27:01 +00:00
"fallbackBitrate": False,
2021-12-23 18:02:33 +00:00
"fallbackISRC": False,
2024-07-29 15:53:37 +00:00
"fallbackSearch": False,
"featuredToTitle": FeaturesOption.NO_CHANGE,
"feelingLucky": False,
"illegalCharacterReplacer": "_",
"jpegImageQuality": 90,
"localArtworkFormat": "jpg",
"localArtworkSize": 1400,
"logErrors": True,
"logSearched": False,
"maxBitrate": TrackFormats.MP3_320,
"overwriteFile": OverwriteOption.DONT_OVERWRITE,
2024-07-29 15:53:37 +00:00
"paddingSize": "0",
"padTracks": True,
"playlistFilenameTemplate": "playlist",
2024-07-29 15:53:37 +00:00
"playlistNameTemplate": "%playlist%",
"playlistTracknameTemplate": "%position% - %artist% - %title%",
"queueConcurrency": 3,
"removeAlbumVersion": False,
2021-12-19 16:27:01 +00:00
"removeDuplicateArtists": True,
2024-07-29 15:53:37 +00:00
"saveArtwork": True,
"saveArtworkArtist": False,
"syncedLyrics": False,
"tags": {
2024-07-29 15:53:37 +00:00
"album": True,
"albumArtist": True,
"artist": True,
2021-12-19 16:28:56 +00:00
"artists": True,
2024-07-29 15:53:37 +00:00
"barcode": True,
"bpm": True,
"composer": False,
"copyright": False,
"cover": True,
2024-07-29 15:53:37 +00:00
"coverDescriptionUTF8": False,
"date": True,
"discNumber": True,
"discTotal": False,
"explicit": False,
2024-07-29 15:53:37 +00:00
"genre": True,
"involvedPeople": False,
"isrc": True,
"label": True,
2024-07-29 15:53:37 +00:00
"length": True,
"lyrics": False,
2024-07-29 15:53:37 +00:00
"multiArtistSeparator": "default",
2021-08-04 18:39:51 +00:00
"rating": False,
2024-07-29 15:53:37 +00:00
"replayGain": False,
"saveID3v1": True,
2024-07-29 15:53:37 +00:00
"savePlaylistAsCompilation": False,
"singleAlbumArtist": False,
2024-07-29 15:53:37 +00:00
"source": False,
"syncedLyrics": False,
"title": True,
"trackNumber": True,
"trackTotal": False,
"useNullSeparator": False,
"year": True,
},
"titleCasing": "nothing",
"tracknameTemplate": "%artist% - %title%",
}
def save(settings, configFolder=None):
configFolder = Path(configFolder or localpaths.getConfigFolder())
makedirs(configFolder, exist_ok=True) # Create config folder if it doesn't exsist
2021-12-21 11:40:35 +00:00
with open(configFolder / 'config.json', 'w', encoding="utf-8") as configFile:
json.dump(settings, configFile, indent=2)
def load(configFolder=None):
configFolder = Path(configFolder or localpaths.getConfigFolder())
makedirs(configFolder, exist_ok=True) # Create config folder if it doesn't exsist
if not (configFolder / 'config.json').is_file(): save(DEFAULTS, configFolder) # Create config file if it doesn't exsist
# Read config file
2021-12-21 11:40:35 +00:00
with open(configFolder / 'config.json', 'r', encoding="utf-8") as configFile:
2021-10-07 14:18:50 +00:00
try:
settings = json.load(configFile)
except json.decoder.JSONDecodeError:
save(DEFAULTS, configFolder)
settings = deepcopy(DEFAULTS)
except Exception:
settings = deepcopy(DEFAULTS)
2024-07-30 11:32:26 +00:00
if check(settings) > 0:
try:
save(settings, configFolder) # Check the settings and save them if something changed
except:
print(f"Error saving config file {configFile.name}, continuing without saving.")
return settings
def check(settings):
changes = 0
for i_set in DEFAULTS:
2024-07-30 11:32:40 +00:00
if not i_set in settings or not type(settings[i_set] is type(DEFAULTS[i_set])):
settings[i_set] = DEFAULTS[i_set]
changes += 1
for i_set in DEFAULTS['tags']:
if not i_set in settings['tags'] or not isinstance(settings['tags'][i_set], type(DEFAULTS['tags'][i_set])):
settings['tags'][i_set] = DEFAULTS['tags'][i_set]
changes += 1
if settings['downloadLocation'] == "":
settings['downloadLocation'] = DEFAULTS['downloadLocation']
changes += 1
for template in ['tracknameTemplate', 'albumTracknameTemplate', 'playlistTracknameTemplate', 'playlistNameTemplate', 'artistNameTemplate', 'albumNameTemplate', 'playlistFilenameTemplate', 'coverImageTemplate', 'artistImageTemplate', 'paddingSize']:
if settings[template] == "":
settings[template] = DEFAULTS[template]
changes += 1
return changes