2020-02-24 17:36:11 +00:00
|
|
|
import re
|
|
|
|
from os.path import sep as pathSep
|
2020-09-27 21:44:37 +00:00
|
|
|
from pathlib import Path
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2020-09-27 21:44:37 +00:00
|
|
|
sepName = name.split(pathSep)
|
2020-04-17 10:31:47 +00:00
|
|
|
name = ""
|
2020-09-27 21:44:37 +00:00
|
|
|
for txt in sepName:
|
|
|
|
txt = fixLongName(txt)
|
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
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
def pad(num, max, settings):
|
|
|
|
if int(settings['paddingSize']) == 0:
|
|
|
|
paddingSize = len(str(max))
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
2020-09-27 21:44:37 +00:00
|
|
|
paddingSize = 10 ** (int(settings['paddingSize']) - 1)
|
|
|
|
if paddingSize == 1:
|
|
|
|
paddingSize = 2
|
|
|
|
if settings['padTracks']:
|
|
|
|
return str(num).zfill(paddingSize)
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
2020-09-27 21:44:37 +00:00
|
|
|
return str(num)
|
2020-04-17 10:31:47 +00:00
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
def generateFilename(track, settings, template):
|
|
|
|
filename = template or "%artist% - %title%"
|
|
|
|
return settingsRegex(filename, track, settings)
|
2020-02-24 17:36:11 +00:00
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
def generateFilepath(track, settings):
|
|
|
|
filepath = Path(settings['downloadLocation'])
|
2020-04-17 10:31:47 +00:00
|
|
|
artistPath = None
|
|
|
|
coverPath = None
|
|
|
|
extrasPath = None
|
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
if settings['createPlaylistFolder'] and track.playlist and not settings['tags']['savePlaylistAsCompilation']:
|
|
|
|
filepath = filepath / settingsRegexPlaylist(settings['playlistNameTemplate'], track.playlist, settings)
|
2020-04-17 10:31:47 +00:00
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
if track.playlist and not settings['tags']['savePlaylistAsCompilation']:
|
2020-04-17 10:31:47 +00:00
|
|
|
extrasPath = filepath
|
|
|
|
|
|
|
|
if (
|
2020-09-27 21:44:37 +00:00
|
|
|
(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'])
|
2020-04-17 10:31:47 +00:00
|
|
|
):
|
2020-09-27 21:44:37 +00:00
|
|
|
filepath = filepath / settingsRegexArtist(settings['artistNameTemplate'], track.album['mainArtist'], settings)
|
2020-04-17 10:31:47 +00:00
|
|
|
artistPath = filepath
|
|
|
|
|
|
|
|
if (settings['createAlbumFolder'] and
|
2020-09-27 21:44:37 +00:00
|
|
|
(not track.singleDownload or (track.singleDownload and settings['createSingleFolder'])) and
|
|
|
|
(not track.playlist or
|
|
|
|
(track.playlist and settings['tags']['savePlaylistAsCompilation']) or
|
|
|
|
(track.playlist and settings['createStructurePlaylist'])
|
|
|
|
)
|
2020-04-17 10:31:47 +00:00
|
|
|
):
|
2020-09-27 21:44:37 +00:00
|
|
|
filepath = filepath / settingsRegexAlbum(settings['albumNameTemplate'], track.album, settings, track.playlist if track.playlist else None)
|
2020-04-17 10:31:47 +00:00
|
|
|
coverPath = filepath
|
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
if not (track.playlist and not settings['tags']['savePlaylistAsCompilation']):
|
2020-04-17 10:31:47 +00:00
|
|
|
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
|
2020-09-27 21:44:37 +00:00
|
|
|
(not track.singleDownload or (track.singleDownload and settings['createSingleFolder'])) and
|
|
|
|
(not track.playlist or
|
|
|
|
(track.playlist and settings['tags']['savePlaylistAsCompilation']) or
|
|
|
|
(track.playlist and settings['createStructurePlaylist'])
|
|
|
|
)
|
2020-04-17 10:31:47 +00:00
|
|
|
)):
|
2020-09-27 21:44:37 +00:00
|
|
|
filepath = filepath / 'CD' + str(track.discNumber)
|
2020-04-17 10:31:47 +00:00
|
|
|
|
|
|
|
return (filepath, artistPath, coverPath, extrasPath)
|
|
|
|
|
2020-02-24 17:36:11 +00:00
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
def settingsRegex(filename, track, settings):
|
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-09-27 21:44:37 +00:00
|
|
|
if track.featArtistsString:
|
|
|
|
filename = filename.replace("%featartists%", fixName('('+track.featArtistsString+')', settings['illegalCharacterReplacer']))
|
|
|
|
else:
|
|
|
|
filename = filename.replace("%featartists%", '')
|
2020-08-15 19:34:10 +00:00
|
|
|
filename = filename.replace("%album%", fixName(track.album['title'], settings['illegalCharacterReplacer']))
|
2020-09-27 21:44:37 +00:00
|
|
|
filename = filename.replace("%albumartist%", fixName(track.album['mainArtist']['name'], settings['illegalCharacterReplacer']))
|
|
|
|
filename = filename.replace("%tracknumber%", pad(track.trackNumber, track.album['trackTotal'], settings))
|
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-09-27 21:44:37 +00:00
|
|
|
if track.playlist:
|
|
|
|
filename = filename.replace("%playlist_id%", str(track.playlist['playlistId']))
|
|
|
|
filename = filename.replace("%position%", pad(track.position, track.playlist['trackTotal'], settings))
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
2020-09-27 21:44:37 +00:00
|
|
|
filename = filename.replace("%playlist_id%", '')
|
|
|
|
filename = filename.replace("%position%", pad(track.trackNumber, track.album['trackTotal'], settings))
|
2020-04-17 10:31:47 +00:00
|
|
|
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']:
|
2020-09-27 21:44:37 +00:00
|
|
|
foldername = foldername.replace("%album_id%", "pl_" + str(playlist['playlistId']))
|
|
|
|
foldername = foldername.replace("%genre%", "Compile")
|
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']))
|
2020-09-27 21:44:37 +00:00
|
|
|
foldername = foldername.replace("%type%", fixName(album['recordType'].capitalize(), settings['illegalCharacterReplacer']))
|
2020-04-17 10:31:47 +00:00
|
|
|
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']))
|
2020-09-27 21:44:37 +00:00
|
|
|
foldername = foldername.replace("%playlist_id%", fixName(playlist['playlistId'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%owner%", fixName(playlist['owner']['name'], settings['illegalCharacterReplacer']))
|
|
|
|
foldername = foldername.replace("%owner_id%", str(playlist['owner']['id']))
|
|
|
|
foldername = foldername.replace("%year%", str(playlist['date']['year']))
|
|
|
|
foldername = foldername.replace("%date%", str(playlist['dateString']))
|
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))
|