From 65019d8323e85dd0ecb94a669f2d5fc252eea2c8 Mon Sep 17 00:00:00 2001 From: rembo10 Date: Wed, 8 Jul 2015 02:41:17 -0700 Subject: [PATCH] Fix for #2260 & #2129 & 2175 & #2163 : Better artist directory scanning --- data/interfaces/default/config.html | 2 +- headphones/webserve.py | 68 ++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 7b8a1275..7478e5ed 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -1166,7 +1166,7 @@
- Use: $Artist/$artist, $Album/$album, $Year/$year, $Type/$type (release type) and $First/$first (first letter in artist name), $OriginalFolder/$originalfolder (downloaded directory name) + Use: $Artist/$artist, $SortArtist/$sortartist, $Album/$album, $Year/$year, $Type/$type (release type) and $First/$first (first letter in artist name), $OriginalFolder/$originalfolder (downloaded directory name) E.g.: $Type/$First/$artist/$album [$year] = Album/G/girl talk/all day [2010]
diff --git a/headphones/webserve.py b/headphones/webserve.py index 610a705a..60ffac79 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -35,6 +35,7 @@ import time import cgi import sys import os +import re try: # pylint:disable=E0611 @@ -268,12 +269,67 @@ class WebInterface(object): def scanArtist(self, ArtistID): logger.info(u"Scanning artist: %s", ArtistID) myDB = db.DBConnection() - artistname = myDB.select('SELECT DISTINCT ArtistName FROM artists WHERE ArtistID=?', [ArtistID]) - artistfolder = os.path.join(headphones.CONFIG.DESTINATION_DIR, artistname[0][0]) - try: - threading.Thread(target=librarysync.libraryScan(dir=artistfolder)).start() - except Exception as e: - logger.error('Unable to complete the scan: %s', e) + full_folder_format = headphones.CONFIG.FOLDER_FORMAT + folder_format = re.findall(r'(.*[Aa]rtist?)\.*', full_folder_format)[0] + acceptable_formats = ["$artist","$sortartist","$first/$artist","$first/$sortartist"] + if not folder_format.lower() in acceptable_formats: + logger.info("Can't determine the artist folder from the configured folder_format. Not scanning") + return + + # Format the folder to match the settings + artist = myDB.select('SELECT DISTINCT ArtistName FROM artists WHERE ArtistID=?', [ArtistID])[0][0] + artist = artist.replace('/', '_') + + if headphones.CONFIG.FILE_UNDERSCORES: + artist = artist.replace(' ', '_') + + if artist.startswith('The '): + sortname = artist[4:] + ", The" + else: + sortname = artist + + if sortname[0].isdigit(): + firstchar = u'0-9' + else: + firstchar = sortname[0] + + values = {'$Artist': artist, + '$SortArtist': sortname, + '$First': firstchar.upper(), + '$artist': artist.lower(), + '$sortartist': sortname.lower(), + '$first': firstchar.lower(), + } + + folder = helpers.replace_all(folder_format.strip(), values, normalize=True) + + folder = helpers.replace_illegal_chars(folder, type="folder") + folder = folder.replace('./', '_/').replace('/.', '/_') + + if folder.endswith('.'): + folder = folder[:-1] + '_' + + if folder.startswith('.'): + folder = '_' + folder[1:] + + dirs = [] + if headphones.CONFIG.MUSIC_DIR: + dirs.append(headphones.CONFIG.MUSIC_DIR) + if headphones.CONFIG.DESTINATION_DIR: + dirs.append(headphones.CONFIG.DESTINATION_DIR) + if headphones.CONFIG.LOSSLESS_DESTINATION_DIR: + dirs.append(headphones.CONFIG.LOSSLESS_DESTINATION_DIR) + + dirs = set(dirs) + + for dir in dirs: + artistfolder = os.path.join(dir, folder) + if not os.path.isdir(artistfolder): + continue + try: + threading.Thread(target=librarysync.libraryScan(dir=artistfolder)).start() + except Exception as e: + logger.error('Unable to complete the scan: %s', e) raise cherrypy.HTTPRedirect("artistPage?ArtistID=%s" % ArtistID) @cherrypy.expose