deemix-py/deemix/utils/pathtemplates.py

244 lines
11 KiB
Python
Raw Normal View History

2020-02-24 17:36:11 +00:00
import re
from os.path import sep as pathSep
from pathlib import Path
from unicodedata import normalize
from deezer import TrackFormats
2020-02-24 17:36:11 +00:00
bitrateLabels = {
TrackFormats.MP4_RA3: "360 HQ",
TrackFormats.MP4_RA2: "360 MQ",
TrackFormats.MP4_RA1: "360 LQ",
TrackFormats.FLAC : "FLAC",
TrackFormats.MP3_320: "320",
TrackFormats.MP3_128: "128",
TrackFormats.DEFAULT: "128",
TrackFormats.LOCAL : "MP3"
2020-02-24 17:36:11 +00:00
}
def fixName(txt, char='_'):
txt = str(txt)
txt = re.sub(r'[\0\/\\:*?"<>|]', char, txt)
txt = normalize("NFC", txt)
return txt
2020-02-24 17:36:11 +00:00
def fixLongName(name):
2021-06-07 18:25:51 +00:00
def fixEndOfData(bString):
try:
bString.decode()
return True
except Exception:
return False
if pathSep in name:
sepName = name.split(pathSep)
name = ""
for txt in sepName:
txt = fixLongName(txt)
name += txt + pathSep
name = name[:-1]
else:
name = name.encode('utf-8')[:200]
while not fixEndOfData(name):
name = name[:-1]
name = name.decode()
return name
2020-02-24 17:36:11 +00:00
def antiDot(string):
while string[-1:] == "." or string[-1:] == " " or string[-1:] == "\n":
string = string[:-1]
if len(string) < 1:
string = "dot"
return string
2020-02-24 17:36:11 +00:00
2021-04-10 09:53:52 +00:00
def pad(num, max_val, settings):
if int(settings['paddingSize']) == 0:
2021-04-10 09:53:52 +00:00
paddingSize = len(str(max_val))
else:
2020-09-30 18:27:19 +00:00
paddingSize = len(str(10 ** (int(settings['paddingSize']) - 1)))
if paddingSize == 1:
paddingSize = 2
if settings['padTracks']:
return str(num).zfill(paddingSize)
2021-04-10 09:53:52 +00:00
return str(num)
2021-06-07 18:25:51 +00:00
def generatePath(track, downloadObject, settings):
filenameTemplate = "%artist% - %title%"
singleTrack = False
if downloadObject.type == "track":
if settings['createSingleFolder']:
filenameTemplate = settings['albumTracknameTemplate']
else:
filenameTemplate = settings['tracknameTemplate']
singleTrack = True
elif downloadObject.type == "album":
filenameTemplate = settings['albumTracknameTemplate']
else:
filenameTemplate = settings['plyalistTracknameTemplate']
filename = generateTrackName(filenameTemplate, track, settings)
2020-02-24 17:36:11 +00:00
2021-06-07 18:25:51 +00:00
filepath = Path(settings['downloadLocation'] or '.')
artistPath = None
coverPath = None
extrasPath = None
if settings['createPlaylistFolder'] and track.playlist and not settings['tags']['savePlaylistAsCompilation']:
2021-06-07 18:25:51 +00:00
filepath = filepath / generatePlaylistName(settings['playlistNameTemplate'], track.playlist, settings)
if track.playlist and not settings['tags']['savePlaylistAsCompilation']:
extrasPath = filepath
if (
(settings['createArtistFolder'] and not track.playlist) or
(settings['createArtistFolder'] and track.playlist and settings['tags']['savePlaylistAsCompilation']) or
(settings['createArtistFolder'] and track.playlist and settings['createStructurePlaylist'])
):
2021-06-07 18:25:51 +00:00
filepath = filepath / generateArtistName(settings['artistNameTemplate'], track.album.mainArtist, settings, rootArtist=track.album.rootArtist)
artistPath = filepath
if (settings['createAlbumFolder'] and
2021-06-07 18:25:51 +00:00
(not singleTrack or (singleTrack and settings['createSingleFolder'])) and
(not track.playlist or
(track.playlist and settings['tags']['savePlaylistAsCompilation']) or
(track.playlist and settings['createStructurePlaylist'])
)
):
2021-06-07 18:25:51 +00:00
filepath = filepath / generateAlbumName(settings['albumNameTemplate'], track.album, settings, track.playlist)
coverPath = filepath
2021-06-07 18:25:51 +00:00
if not extrasPath: extrasPath = filepath
if (
2021-06-07 18:25:51 +00:00
int(track.album.discTotal) > 1 and (
(settings['createAlbumFolder'] and settings['createCDFolder']) and
2021-06-07 18:25:51 +00:00
(not singleTrack or (singleTrack and settings['createSingleFolder'])) and
(not track.playlist or
(track.playlist and settings['tags']['savePlaylistAsCompilation']) or
(track.playlist and settings['createStructurePlaylist'])
2021-06-07 18:25:51 +00:00
)
)):
2021-06-07 18:25:51 +00:00
filepath = filepath / f'CD{track.discNumber}'
# Remove subfolders from filename and add it to filepath
if pathSep in filename:
tempPath = filename[:filename.rfind(pathSep)]
filepath = filepath / tempPath
filename = filename[filename.rfind(pathSep) + len(pathSep):]
2021-06-07 18:25:51 +00:00
return (filename, filepath, artistPath, coverPath, extrasPath)
2020-02-24 17:36:11 +00:00
2021-06-07 18:25:51 +00:00
def generateTrackName(filename, track, settings):
c = settings['illegalCharacterReplacer']
filename = filename.replace("%title%", fixName(track.title, c))
filename = filename.replace("%artist%", fixName(track.mainArtist.name, c))
filename = filename.replace("%artists%", fixName(", ".join(track.artists), c))
filename = filename.replace("%allartists%", fixName(track.artistsString, c))
filename = filename.replace("%mainartists%", fixName(track.mainArtistsString, c))
if track.featArtistsString:
2021-06-07 18:25:51 +00:00
filename = filename.replace("%featartists%", fixName('('+track.featArtistsString+')', c))
else:
filename = filename.replace("%featartists%", '')
2021-06-07 18:25:51 +00:00
filename = filename.replace("%album%", fixName(track.album.title, c))
filename = filename.replace("%albumartist%", fixName(track.album.mainArtist.name, c))
2021-01-31 16:59:15 +00:00
filename = filename.replace("%tracknumber%", pad(track.trackNumber, track.album.trackTotal, settings))
filename = filename.replace("%tracktotal%", str(track.album.trackTotal))
2020-08-15 19:34:10 +00:00
filename = filename.replace("%discnumber%", str(track.discNumber))
2021-01-31 16:59:15 +00:00
filename = filename.replace("%disctotal%", str(track.album.discTotal))
if len(track.album.genre) > 0:
2021-06-07 18:25:51 +00:00
filename = filename.replace("%genre%", fixName(track.album.genre[0], c))
else:
filename = filename.replace("%genre%", "Unknown")
2021-01-31 16:59:15 +00:00
filename = filename.replace("%year%", str(track.date.year))
2020-08-15 19:34:10 +00:00
filename = filename.replace("%date%", track.dateString)
filename = filename.replace("%bpm%", str(track.bpm))
2021-06-07 18:25:51 +00:00
filename = filename.replace("%label%", fixName(track.album.label, c))
2020-08-15 19:34:10 +00:00
filename = filename.replace("%isrc%", track.ISRC)
2021-01-31 16:59:15 +00:00
filename = filename.replace("%upc%", track.album.barcode)
2020-08-15 19:34:10 +00:00
filename = filename.replace("%explicit%", "(Explicit)" if track.explicit else "")
filename = filename.replace("%track_id%", str(track.id))
2021-01-31 16:59:15 +00:00
filename = filename.replace("%album_id%", str(track.album.id))
filename = filename.replace("%artist_id%", str(track.mainArtist.id))
if track.playlist:
2021-03-19 14:44:21 +00:00
filename = filename.replace("%playlist_id%", str(track.playlist.playlistID))
2021-01-31 16:59:15 +00:00
filename = filename.replace("%position%", pad(track.position, track.playlist.trackTotal, settings))
else:
filename = filename.replace("%playlist_id%", '')
2021-06-07 18:25:51 +00:00
filename = filename.replace("%position%", pad(track.position, track.album.trackTotal, settings))
filename = filename.replace('\\', pathSep).replace('/', pathSep)
return antiDot(fixLongName(filename))
2020-02-24 17:36:11 +00:00
2021-06-07 18:25:51 +00:00
def generateAlbumName(foldername, album, settings, playlist=None):
c = settings['illegalCharacterReplacer']
if playlist and settings['tags']['savePlaylistAsCompilation']:
2021-03-19 14:44:21 +00:00
foldername = foldername.replace("%album_id%", "pl_" + str(playlist.playlistID))
foldername = foldername.replace("%genre%", "Compile")
else:
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%album_id%", str(album.id))
if len(album.genre) > 0:
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%genre%", fixName(album.genre[0], c))
else:
foldername = foldername.replace("%genre%", "Unknown")
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%album%", fixName(album.title, c))
foldername = foldername.replace("%artist%", fixName(album.mainArtist.name, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%artist_id%", str(album.mainArtist.id))
if album.rootArtist:
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%root_artist%", fixName(album.rootArtist.name, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%root_artist_id%", str(album.rootArtist.id))
2020-11-21 15:45:40 +00:00
else:
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%root_artist%", fixName(album.mainArtist.name, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%root_artist_id%", str(album.mainArtist.id))
foldername = foldername.replace("%tracktotal%", str(album.trackTotal))
foldername = foldername.replace("%disctotal%", str(album.discTotal))
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%type%", fixName(album.recordType.capitalize(), c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%upc%", album.barcode)
foldername = foldername.replace("%explicit%", "(Explicit)" if album.explicit else "")
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%label%", fixName(album.label, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%year%", str(album.date.year))
foldername = foldername.replace("%date%", album.dateString)
foldername = foldername.replace("%bitrate%", bitrateLabels[int(album.bitrate)])
foldername = foldername.replace('\\', pathSep).replace('/', pathSep)
return antiDot(fixLongName(foldername))
2020-02-24 17:36:11 +00:00
2021-06-07 18:25:51 +00:00
def generateArtistName(foldername, artist, settings, rootArtist=None):
c = settings['illegalCharacterReplacer']
foldername = foldername.replace("%artist%", fixName(artist.name, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%artist_id%", str(artist.id))
2020-11-21 15:45:40 +00:00
if rootArtist:
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%root_artist%", fixName(rootArtist.name, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%root_artist_id%", str(rootArtist.id))
2020-11-21 15:45:40 +00:00
else:
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%root_artist%", fixName(artist.name, c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%root_artist_id%", str(artist.id))
foldername = foldername.replace('\\', pathSep).replace('/', pathSep)
return antiDot(fixLongName(foldername))
2020-02-24 17:36:11 +00:00
2021-06-07 18:25:51 +00:00
def generatePlaylistName(foldername, playlist, settings):
c = settings['illegalCharacterReplacer']
foldername = foldername.replace("%playlist%", fixName(playlist.title, c))
foldername = foldername.replace("%playlist_id%", fixName(playlist.playlistID, c))
foldername = foldername.replace("%owner%", fixName(playlist.owner['name'], c))
2021-01-31 16:59:15 +00:00
foldername = foldername.replace("%owner_id%", str(playlist.owner['id']))
foldername = foldername.replace("%year%", str(playlist.date.year))
foldername = foldername.replace("%date%", str(playlist.dateString))
foldername = foldername.replace("%explicit%", "(Explicit)" if playlist.explicit else "")
foldername = foldername.replace('\\', pathSep).replace('/', pathSep)
return antiDot(fixLongName(foldername))
2021-06-07 18:25:51 +00:00
def generateDownloadObjectName(foldername, queueItem, settings):
c = settings['illegalCharacterReplacer']
foldername = foldername.replace("%title%", fixName(queueItem.title, c))
foldername = foldername.replace("%artist%", fixName(queueItem.artist, c))
2020-08-15 13:49:45 +00:00
foldername = foldername.replace("%size%", str(queueItem.size))
2021-06-07 18:25:51 +00:00
foldername = foldername.replace("%type%", fixName(queueItem.type, c))
foldername = foldername.replace("%id%", fixName(queueItem.id, c))
2020-08-15 13:49:45 +00:00
foldername = foldername.replace("%bitrate%", bitrateLabels[int(queueItem.bitrate)])
2021-06-07 18:25:51 +00:00
foldername = foldername.replace('\\', pathSep).replace('/', pathSep).replace(pathSep, c)
return antiDot(fixLongName(foldername))