diff --git a/Headphones.py b/Headphones.py index 4487df68..e75c60c4 100644 --- a/Headphones.py +++ b/Headphones.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -import os, sys +import os, sys, locale import time from lib.configobj import ConfigObj @@ -25,6 +25,19 @@ def main(): headphones.PROG_DIR = os.path.dirname(headphones.FULL_PATH) headphones.ARGS = sys.argv[1:] + # From sickbeard + headphones.SYS_ENCODING = None + + try: + locale.setlocale(locale.LC_ALL, "") + headphones.SYS_ENCODING = locale.getpreferredencoding() + except (locale.Error, IOError): + pass + + # for OSes that are poorly configured I'll just force UTF-8 + if not headphones.SYS_ENCODING or headphones.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): + headphones.SYS_ENCODING = 'UTF-8' + # Set up and gather command line arguments parser = argparse.ArgumentParser(description='Music add-on for SABnzbd+') diff --git a/headphones/__init__.py b/headphones/__init__.py index b31968bd..fa05d1b1 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -20,6 +20,8 @@ PROG_DIR = None ARGS = None SIGNAL = None +SYS_ENCODING = None + QUIET = False DAEMON = False diff --git a/headphones/importer.py b/headphones/importer.py index 893115c5..b186d0c7 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -32,7 +32,7 @@ def artistlist_to_mbids(artistlist, forced=False): results = mb.findArtist(artist, limit=1) if not results: - logger.info('No results found for: %' % artist) + logger.info('No results found for: %s' % artist) continue try: diff --git a/headphones/librarysync.py b/headphones/librarysync.py index 62c1bc7b..73d53e99 100644 --- a/headphones/librarysync.py +++ b/headphones/librarysync.py @@ -11,6 +11,11 @@ def libraryScan(dir=None): if not dir: dir = headphones.MUSIC_DIR + try: + dir = str(dir) + except UnicodeEncodeError: + dir = unicode(dir).encode('unicode_escape') + logger.info('Scanning music directory: %s' % dir) new_artists = [] @@ -23,10 +28,12 @@ def libraryScan(dir=None): for files in f: # MEDIA_FORMATS = music file extensions, e.g. mp3, flac, etc if any(files.endswith('.' + x) for x in headphones.MEDIA_FORMATS): - file = os.path.join(r, files) + song = os.path.join(r, files) + file = unicode(os.path.join(r, files), headphones.SYS_ENCODING, errors='replace') + # Try to read the metadata try: - f = MediaFile(file) + f = MediaFile(song) except: logger.error('Cannot read file: ' + file) continue @@ -71,6 +78,9 @@ def libraryScan(dir=None): # The have table will become the new database for unmatched tracks (i.e. tracks with no associated links in the database myDB.action('INSERT INTO have VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [f_artist, f.album, f.track, f.title, f.length, f.bitrate, f.genre, f.date, f.mb_trackid, file, helpers.cleanName(f_artist+' '+f.album+' '+f.title)]) + logger.info('Completed scanning of directory: %s' % dir) + logger.info('Checking filepaths to see if we can find any matches') + # Now check empty file paths to see if we can find a match based on their folder format tracks = myDB.select('SELECT * from tracks WHERE Location IS NULL') for track in tracks: @@ -131,6 +141,7 @@ def libraryScan(dir=None): if match: + logger.info('Found a match: %s. Writing MBID to metadata' % match[0]) myDB.action('UPDATE tracks SET Location=? WHERE TrackID=?', [match[0], track['TrackID']]) myDB.action('DELETE from have WHERE Location=?', [match[0]]) @@ -140,11 +151,11 @@ def libraryScan(dir=None): f.mb_trackid = track['TrackID'] f.save() myDB.action('UPDATE tracks SET BitRate=? WHERE TrackID=?', [f.bitrate, track['TrackID']]) - logger.debug('Wrote mbid to track: %s' % match[0]) except: logger.error('Error embedding track id into: %s' % match[0]) continue + logger.info('Done checking empty filepaths') # Clean up bad filepaths tracks = myDB.select('SELECT Location, TrackID from tracks WHERE Location IS NOT NULL') @@ -152,7 +163,7 @@ def libraryScan(dir=None): if not os.path.isfile(track['Location']): myDB.action('UPDATE tracks SET Location=? WHERE TrackID=?', [None, track['TrackID']]) - logger.info('Completed scanning of directory: %s. Updating track counts' % dir) + logger.info('Done syncing library with directory: %s' % dir) # Clean up the new artist list unique_artists = {}.fromkeys(new_artists).keys() @@ -161,18 +172,20 @@ def libraryScan(dir=None): artist_list = [f for f in unique_artists if f.lower() not in [x[0].lower() for x in current_artists]] # Update track counts + logger.info('Updating track counts') for artist in current_artists: havetracks = len(myDB.select('SELECT TrackTitle from tracks WHERE ArtistID like ? AND Location IS NOT NULL', [artist['ArtistID']])) + len(myDB.select('SELECT TrackTitle from have WHERE ArtistName like ?', [artist['ArtistName']])) myDB.action('UPDATE artists SET HaveTracks=? WHERE ArtistID=?', [havetracks, artist['ArtistID']]) logger.info('Found %i new artists' % len(artist_list)) - if headphones.ADD_ARTISTS: - logger.info('Importing %i new artists' % len(artist_list)) - importer.artistlist_to_mbids(artist_list) - else: - logger.info('To add these artists, go to Manage->Manage New Artists') - headphones.NEW_ARTISTS = artist_list + if len(artist_list): + if headphones.ADD_ARTISTS: + logger.info('Importing %i new artists' % len(artist_list)) + importer.artistlist_to_mbids(artist_list) + else: + logger.info('To add these artists, go to Manage->Manage New Artists') + headphones.NEW_ARTISTS = artist_list if headphones.DETECT_BITRATE: headphones.PREFERRED_BITRATE = sum(bitrates)/len(bitrates)/1000 \ No newline at end of file diff --git a/headphones/webstart.py b/headphones/webstart.py index e5c14cd7..4b6c95f1 100644 --- a/headphones/webstart.py +++ b/headphones/webstart.py @@ -20,7 +20,9 @@ def initialize(options={}): conf = { '/': { - 'tools.staticdir.root': os.path.join(headphones.PROG_DIR, 'data') + 'tools.staticdir.root': os.path.join(headphones.PROG_DIR, 'data'), + 'tools.encode.on': True, + 'tools.encode.encoding': 'utf-8' }, '/interfaces':{ 'tools.staticdir.on': True,