2020-02-24 17:36:11 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import re
|
|
|
|
from os.path import sep as pathSep
|
2020-08-27 09:37:30 +00:00
|
|
|
from unicodedata import normalize
|
2020-02-24 17:36:11 +00:00
|
|
|
|
|
|
|
bitrateLabels = {
|
2020-04-17 10:31:47 +00:00
|
|
|
15: "360 HQ",
|
|
|
|
14: "360 MQ",
|
|
|
|
13: "360 LQ",
|
|
|
|
9: "FLAC",
|
|
|
|
3: "320",
|
|
|
|
1: "128",
|
2020-07-02 14:49:17 +00:00
|
|
|
8: "128",
|
|
|
|
0: "MP3"
|
2020-02-24 17:36:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
def fixName(txt, char='_'):
|
2020-04-17 10:31:47 +00:00
|
|
|
txt = str(txt)
|
|
|
|
txt = re.sub(r'[\0\/\\:*?"<>|]', char, txt)
|
2020-08-27 09:37:30 +00:00
|
|
|
txt = normalize("NFC", txt)
|
2020-04-17 10:31:47 +00:00
|
|
|
return txt
|
|
|
|
|
2020-07-29 09:32:25 +00:00
|
|
|
def fixEndOfData(bString):
|
|
|
|
try:
|
|
|
|
bString.decode()
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2020-02-24 17:36:11 +00:00
|
|
|
|
|
|
|
def fixLongName(name):
|
2020-04-17 10:31:47 +00:00
|
|
|
if pathSep in name:
|
|
|
|
name2 = name.split(pathSep)
|
|
|
|
name = ""
|
|
|
|
for txt in name2:
|
2020-07-29 09:32:25 +00:00
|
|
|
txt = txt.encode('utf-8')[:200]
|
|
|
|
while not fixEndOfData(txt):
|
|
|
|
txt = txt[:-1]
|
|
|
|
txt = txt.decode()
|
2020-04-17 10:31:47 +00:00
|
|
|
name += txt + pathSep
|
|
|
|
name = name[:-1]
|
|
|
|
else:
|
2020-07-29 09:32:25 +00:00
|
|
|
name = name.encode('utf-8')[:200]
|
|
|
|
while not fixEndOfData(name):
|
|
|
|
name = name[:-1]
|
|
|
|
name = name.decode()
|
2020-04-17 10:31:47 +00:00
|
|
|
return name
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
2020-03-30 09:59:18 +00:00
|
|
|
def antiDot(string):
|
2020-04-17 10:31:47 +00:00
|
|
|
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
|
|
|
|
|
|
|
def pad(num, max, dopad=True):
|
2020-04-17 10:31:47 +00:00
|
|
|
paddingsize = len(str(max))
|
2020-06-02 09:55:31 +00:00
|
|
|
if paddingsize == 1:
|
|
|
|
paddingsize = 2
|
2020-04-17 10:31:47 +00:00
|
|
|
if dopad:
|
|
|
|
return str(num).zfill(paddingsize)
|
|
|
|
else:
|
|
|
|
return str(num)
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
|
|
|
def generateFilename(track, trackAPI, settings):
|
2020-04-17 10:31:47 +00:00
|
|
|
if trackAPI['FILENAME_TEMPLATE'] == "":
|
|
|
|
filename = "%artist% - %title%"
|
|
|
|
else:
|
|
|
|
filename = trackAPI['FILENAME_TEMPLATE']
|
|
|
|
return settingsRegex(filename, track, settings,
|
|
|
|
trackAPI['_EXTRA_PLAYLIST'] if '_EXTRA_PLAYLIST' in trackAPI else None)
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
|
|
|
def generateFilepath(track, trackAPI, settings):
|
2020-04-17 10:31:47 +00:00
|
|
|
filepath = settings['downloadLocation']
|
|
|
|
if filepath[-1:] != pathSep:
|
|
|
|
filepath += pathSep
|
|
|
|
artistPath = None
|
|
|
|
coverPath = None
|
|
|
|
extrasPath = None
|
|
|
|
|
|
|
|
if settings['createPlaylistFolder'] and '_EXTRA_PLAYLIST' in trackAPI and not settings['tags'][
|
|
|
|
'savePlaylistAsCompilation']:
|
|
|
|
filepath += antiDot(
|
|
|
|
settingsRegexPlaylist(settings['playlistNameTemplate'], trackAPI['_EXTRA_PLAYLIST'], settings)) + pathSep
|
|
|
|
|
|
|
|
if '_EXTRA_PLAYLIST' in trackAPI and not settings['tags']['savePlaylistAsCompilation']:
|
|
|
|
extrasPath = filepath
|
|
|
|
|
|
|
|
if (
|
|
|
|
settings['createArtistFolder'] and not '_EXTRA_PLAYLIST' in trackAPI or
|
|
|
|
(settings['createArtistFolder'] and '_EXTRA_PLAYLIST' in trackAPI and settings['tags'][
|
|
|
|
'savePlaylistAsCompilation']) or
|
|
|
|
(settings['createArtistFolder'] and '_EXTRA_PLAYLIST' in trackAPI and settings['createStructurePlaylist'])
|
|
|
|
):
|
2020-08-15 19:34:10 +00:00
|
|
|
if (int(track.id) < 0 and not 'mainArtist' in track.album):
|
|
|
|
track.album['mainArtist'] = track.mainArtist
|
2020-04-17 10:31:47 +00:00
|
|
|
filepath += antiDot(
|
2020-08-15 19:34:10 +00:00
|
|
|
settingsRegexArtist(settings['artistNameTemplate'], track.album['mainArtist'], settings)) + pathSep
|
2020-04-17 10:31:47 +00:00
|
|
|
artistPath = filepath
|
|
|
|
|
|
|
|
if (settings['createAlbumFolder'] and
|
|
|
|
(not 'SINGLE_TRACK' in trackAPI or ('SINGLE_TRACK' in trackAPI and settings['createSingleFolder'])) and
|
|
|
|
(not '_EXTRA_PLAYLIST' in trackAPI or (
|
|
|
|
'_EXTRA_PLAYLIST' in trackAPI and settings['tags']['savePlaylistAsCompilation']) or (
|
|
|
|
'_EXTRA_PLAYLIST' in trackAPI and settings['createStructurePlaylist']))
|
|
|
|
):
|
|
|
|
filepath += antiDot(
|
2020-08-15 19:34:10 +00:00
|
|
|
settingsRegexAlbum(settings['albumNameTemplate'], track.album, settings,
|
2020-06-10 17:47:57 +00:00
|
|
|
trackAPI['_EXTRA_PLAYLIST'] if'_EXTRA_PLAYLIST' in trackAPI else None)) + pathSep
|
2020-04-17 10:31:47 +00:00
|
|
|
coverPath = filepath
|
|
|
|
|
|
|
|
if not ('_EXTRA_PLAYLIST' in trackAPI and not settings['tags']['savePlaylistAsCompilation']):
|
|
|
|
extrasPath = filepath
|
|
|
|
|
|
|
|
if (
|
2020-08-15 19:34:10 +00:00
|
|
|
int(track.album['discTotal']) > 1 and (
|
2020-04-17 10:31:47 +00:00
|
|
|
(settings['createAlbumFolder'] and settings['createCDFolder']) and
|
|
|
|
(not 'SINGLE_TRACK' in trackAPI or ('SINGLE_TRACK' in trackAPI and settings['createSingleFolder'])) and
|
|
|
|
(not '_EXTRA_PLAYLIST' in trackAPI or (
|
|
|
|
'_EXTRA_PLAYLIST' in trackAPI and settings['tags']['savePlaylistAsCompilation']) or (
|
|
|
|
'_EXTRA_PLAYLIST' in trackAPI and settings['createStructurePlaylist']))
|
|
|
|
)):
|
2020-08-15 19:34:10 +00:00
|
|
|
filepath += 'CD' + str(track.discNumber) + pathSep
|
2020-04-17 10:31:47 +00:00
|
|
|
|
|
|
|
return (filepath, artistPath, coverPath, extrasPath)
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
|
|
|
def settingsRegex(filename, track, settings, playlist=None):
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%title%", fixName(track.title, settings['illegalCharacterReplacer']))
|
|
|
|
filename = filename.replace("%artist%", fixName(track.mainArtist['name'], settings['illegalCharacterReplacer']))
|
2020-08-15 21:03:05 +00:00
|
|
|
filename = filename.replace("%artists%", fixName(", ".join(track.artists), settings['illegalCharacterReplacer']))
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%allartists%", fixName(track.artistsString, settings['illegalCharacterReplacer']))
|
|
|
|
filename = filename.replace("%mainartists%", fixName(track.mainArtistsString, settings['illegalCharacterReplacer']))
|
2020-08-15 21:03:05 +00:00
|
|
|
filename = filename.replace("%featartists%", fixName('('+track.featArtistsString+')', settings['illegalCharacterReplacer']) if track.featArtistsString else "")
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%album%", fixName(track.album['title'], settings['illegalCharacterReplacer']))
|
2020-04-17 10:31:47 +00:00
|
|
|
filename = filename.replace("%albumartist%",
|
2020-08-15 19:34:10 +00:00
|
|
|
fixName(track.album['mainArtist']['name'], settings['illegalCharacterReplacer']))
|
|
|
|
filename = filename.replace("%tracknumber%", pad(track.trackNumber, track.album['trackTotal'] if int(
|
2020-04-17 10:31:47 +00:00
|
|
|
settings['paddingSize']) == 0 else 10 ** (int(settings['paddingSize']) - 1), settings['padTracks']))
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%tracktotal%", str(track.album['trackTotal']))
|
|
|
|
filename = filename.replace("%discnumber%", str(track.discNumber))
|
|
|
|
filename = filename.replace("%disctotal%", str(track.album['discTotal']))
|
|
|
|
if len(track.album['genre']) > 0:
|
2020-04-17 10:31:47 +00:00
|
|
|
filename = filename.replace("%genre%",
|
2020-08-15 19:34:10 +00:00
|
|
|
fixName(track.album['genre'][0], settings['illegalCharacterReplacer']))
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
|
|
|
filename = filename.replace("%genre%", "Unknown")
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%year%", str(track.date['year']))
|
|
|
|
filename = filename.replace("%date%", track.dateString)
|
|
|
|
filename = filename.replace("%bpm%", str(track.bpm))
|
|
|
|
filename = filename.replace("%label%", fixName(track.album['label'], settings['illegalCharacterReplacer']))
|
|
|
|
filename = filename.replace("%isrc%", track.ISRC)
|
|
|
|
filename = filename.replace("%upc%", track.album['barcode'])
|
|
|
|
filename = filename.replace("%explicit%", "(Explicit)" if track.explicit else "")
|
|
|
|
|
|
|
|
filename = filename.replace("%track_id%", str(track.id))
|
|
|
|
filename = filename.replace("%album_id%", str(track.album['id']))
|
|
|
|
filename = filename.replace("%artist_id%", str(track.mainArtist['id']))
|
2020-04-17 10:31:47 +00:00
|
|
|
if playlist:
|
|
|
|
filename = filename.replace("%playlist_id%", str(playlist['id']))
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%position%", pad(track.position, playlist['nb_tracks'] if int(
|
2020-04-17 10:31:47 +00:00
|
|
|
settings['paddingSize']) == 0 else 10 ** (int(settings['paddingSize']) - 1), settings['padTracks']))
|
|
|
|
else:
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%position%", pad(track.trackNumber, track.album['trackTotal'] if int(
|
2020-04-17 10:31:47 +00:00
|
|
|
settings['paddingSize']) == 0 else 10 ** (int(settings['paddingSize']) - 1), settings['padTracks']))
|
|
|
|
filename = filename.replace('\\', pathSep).replace('/', pathSep)
|
|
|
|
return antiDot(fixLongName(filename))
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
2020-06-10 17:47:57 +00:00
|
|
|
def settingsRegexAlbum(foldername, album, settings, playlist=None):
|
|
|
|
if playlist and settings['tags']['savePlaylistAsCompilation']:
|
|
|
|
foldername = foldername.replace("%album_id%", "pl_" + str(playlist['id']))
|
2020-06-22 22:27:43 +00:00
|
|
|
foldername = foldername.replace("%genre%", "Compilation")
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
|
|
|
foldername = foldername.replace("%album_id%", str(album['id']))
|
2020-06-22 22:27:43 +00:00
|
|
|
if len(album['genre']) > 0:
|
|
|
|
foldername = foldername.replace("%genre%", fixName(album['genre'][0], settings['illegalCharacterReplacer']))
|
|
|
|
else:
|
|
|
|
foldername = foldername.replace("%genre%", "Unknown")
|
2020-04-17 10:31:47 +00:00
|
|
|
foldername = foldername.replace("%album%", fixName(album['title'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%artist%",
|
|
|
|
fixName(album['mainArtist']['name'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%artist_id%", str(album['mainArtist']['id']))
|
|
|
|
foldername = foldername.replace("%tracktotal%", str(album['trackTotal']))
|
|
|
|
foldername = foldername.replace("%disctotal%", str(album['discTotal']))
|
|
|
|
foldername = foldername.replace("%type%", fixName(album['recordType'][0].upper() + album['recordType'][1:].lower(),
|
|
|
|
settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%upc%", album['barcode'])
|
2020-05-13 21:30:35 +00:00
|
|
|
foldername = foldername.replace("%explicit%", "(Explicit)" if album['explicit'] else "")
|
2020-04-17 10:31:47 +00:00
|
|
|
foldername = foldername.replace("%label%", fixName(album['label'], settings['illegalCharacterReplacer']))
|
|
|
|
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
|
|
|
|
|
|
|
def settingsRegexArtist(foldername, artist, settings):
|
2020-04-17 10:31:47 +00:00
|
|
|
foldername = foldername.replace("%artist%", fixName(artist['name'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%artist_id%", str(artist['id']))
|
|
|
|
foldername = foldername.replace('\\', pathSep).replace('/', pathSep)
|
|
|
|
return antiDot(fixLongName(foldername))
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
|
|
|
def settingsRegexPlaylist(foldername, playlist, settings):
|
2020-04-17 10:31:47 +00:00
|
|
|
foldername = foldername.replace("%playlist%", fixName(playlist['title'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%playlist_id%", fixName(playlist['id'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%owner%",
|
|
|
|
fixName(playlist['creator']['name'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%owner_id%", str(playlist['creator']['id']))
|
|
|
|
foldername = foldername.replace("%year%", str(playlist['creation_date'][:4]))
|
|
|
|
foldername = foldername.replace("%date%", str(playlist['creation_date'][:10]))
|
2020-06-22 22:27:43 +00:00
|
|
|
foldername = foldername.replace("%explicit%", "(Explicit)" if playlist['explicit'] else "")
|
2020-04-17 10:31:47 +00:00
|
|
|
foldername = foldername.replace('\\', pathSep).replace('/', pathSep)
|
|
|
|
return antiDot(fixLongName(foldername))
|
2020-06-10 17:47:57 +00:00
|
|
|
|
|
|
|
def settingsRegexPlaylistFile(foldername, queueItem, settings):
|
2020-08-15 13:49:45 +00:00
|
|
|
foldername = foldername.replace("%title%", fixName(queueItem.title, settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%artist%", fixName(queueItem.artist, settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%size%", str(queueItem.size))
|
|
|
|
foldername = foldername.replace("%type%", fixName(queueItem.type, settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%id%", fixName(queueItem.id, settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%bitrate%", bitrateLabels[int(queueItem.bitrate)])
|
2020-06-10 17:47:57 +00:00
|
|
|
foldername = foldername.replace('\\', pathSep).replace('/', pathSep).replace(pathSep, settings['illegalCharacterReplacer'])
|
|
|
|
return antiDot(fixLongName(foldername))
|