From abaf445562b9d083d7fcb117e4caa37ddfafc46f Mon Sep 17 00:00:00 2001 From: Remy Date: Sat, 30 Jul 2011 18:31:34 -0700 Subject: [PATCH] Fixed folder.jpg bug, added forced postprocessing --- data/css/style.css | 23 +++++++++++++++ headphones/helpers.py | 14 +++++++++- headphones/mb.py | 27 +++++++++++++++++- headphones/postprocessor.py | 56 +++++++++++++++++++++++++++++++++---- headphones/webserve.py | 27 ++++++++++++++---- lib/beets/mediafile.py | 6 ++-- 6 files changed, 135 insertions(+), 18 deletions(-) diff --git a/data/css/style.css b/data/css/style.css index 3ae8ea48..8462d8b9 100644 --- a/data/css/style.css +++ b/data/css/style.css @@ -66,6 +66,29 @@ h1{ -moz-border-radius: 20px; border-radius: 20px; } +.tableleft{ + padding: 3px; + background-color: #ffffff; + float: left; + width: 46%; + height: 200px; + margin-top: 25px; + margin-left: 25px; + margin-right: auto; + -moz-border-radius: 20px; + border-radius: 20px; + } +.tableright{ + padding: 3px; + background-color: #ffffff; + width: 46%; + height: 200px; + margin-top: 25px; + margin-left: auto; + margin-right: 0px; + -moz-border-radius: 20px; + border-radius: 20px; + } .nav{ padding: 2px; font-size:19px; diff --git a/headphones/helpers.py b/headphones/helpers.py index 6ea9c923..cc6e4c21 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -1,6 +1,7 @@ import time from operator import itemgetter import datetime +import re import headphones @@ -97,4 +98,15 @@ def bytes_to_mb(bytes): def replace_all(text, dic): for i, j in dic.iteritems(): text = text.replace(i, j) - return text \ No newline at end of file + return text + +def extract_data(s): + pattern = re.compile(r'(?P.*?)\s\-\s(?P.*?)\s\[(?P.*?)\]', re.VERBOSE) + + match = pattern.match(s) + + name = match.group("name") + album = match.group("album") + year = match.group("year") + + return (name, album, year) \ No newline at end of file diff --git a/headphones/mb.py b/headphones/mb.py index 99abe7c7..df83ce0a 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -360,4 +360,29 @@ def findArtistbyAlbum(name): artist_dict['score'] = result.score return artist_dict - \ No newline at end of file + +def findAlbumID(artist=None, album=None): + + term = '"'+album+'" AND artist:"'+artist+'"' + + f = ws.ReleaseGroupFilter(query=term, limit=1) + results = None + attempt = 0 + + while attempt < 5: + + try: + results = q.getReleaseGroups(f) + break + except WebServiceError, e: + logger.warn('Attempt to query MusicBrainz for %s failed: %s. Sleeping 5 seconds.' % (name, e)) + attempt += 1 + time.sleep(5) + + time.sleep(1) + + if not results: + return False + + rgid = u.extractUuid(results[0].releaseGroup.id) + return rgid \ No newline at end of file diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 8bc0bc0b..49e98dc3 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -127,8 +127,9 @@ def embedAlbumArt(artwork, downloaded_track_list): try: f = MediaFile(downloaded_track) except: - continue - + logger.error('Could not read %s. Not adding album art' % downloaded_track) + + logger.debug('Adding album art to: %s' % downloaded_track) f.art = artwork f.save() @@ -136,7 +137,7 @@ def addAlbumArt(artwork, albumpath): logger.info('Adding album art to folder') artwork_file_name = os.path.join(albumpath, 'folder.jpg') - file = open(artwork_file_name, 'w') + file = open(artwork_file_name, 'wb') file.write(artwork) file.close() @@ -145,8 +146,12 @@ def cleanupFiles(albumpath): for r,d,f in os.walk(albumpath): for files in f: if not any(files.endswith(x) for x in (".mp3", ".flac", ".aac", ".ogg", ".ape", ".m4a")): - os.remove(os.path.join(r, files)) - + logger.debug('Removing: %s' % files) + try: + os.remove(os.path.join(r, files)) + except Exception, e: + logger.error('Could not remove file: %s. Error: %s' % (files, e)) + def moveFiles(albumpath, release, tracks): try: @@ -253,6 +258,7 @@ def renameFiles(albumpath, downloaded_track_list, release): new_file = os.path.join(albumpath, new_file_name) + logger.debug('Renaming %s ---> %s' % (downloaded_track, new_file_name)) try: shutil.move(downloaded_track, new_file) except Exception, e: @@ -303,4 +309,42 @@ def renameUnprocessedFolder(albumpath): else: os.rename(albumpath, new_folder_name) - return \ No newline at end of file + return + +def forcePostProcess(): + + if not headphones.DOWNLOAD_DIR: + return + else: + download_dir = headphones.DOWNLOAD_DIR + + logger.info('Checking to see if there are any folders to process in download_dir: %s' % download_dir) + # Get a list of folders in the download_dir + folders = [d for d in os.listdir(download_dir) if os.path.isdir(os.path.join(download_dir, d))] + + if len(folders): + logger.info('Found %i folders: %s' % (len(folders), str(folders))) + pass + else: + logger.info('Found no folders to process in: %s' % download_dir) + return + + # Parse the folder names to get artist album info + for folder in folders: + + albumpath = unicode(os.path.join(download_dir, folder)) + name, album, year = helpers.extract_data(folder) + + myDB = db.DBConnection() + release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName=? and AlbumTitle=?', [name, album]).fetchone() + if release: + logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle'])) + verify(release['AlbumID'], albumpath) + else: + logger.info('Querying MusicBrainz for the release group id for: %s - %s' % (name, album)) + from headphones import mb + rgid = unicode(mb.findAlbumID(name, album)) + if rgid: + verify(rgid, albumpath) + + \ No newline at end of file diff --git a/headphones/webserve.py b/headphones/webserve.py index 86a05078..31f22821 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -375,9 +375,10 @@ class WebInterface(object): else: lastfm_user_text = 'Last.FM Username' if headphones.MUSIC_DIR: - music_dir_text = headphones.MUSIC_DIR + music_dir_input = '''''' % headphones.MUSIC_DIR else: - music_dir_text = 'Enter a directory to scan' + music_dir_input = '''''' page = [templates._header] page.append(templates._logobar) page.append(templates._nav) @@ -392,18 +393,25 @@ class WebInterface(object):

- + %s


-

Import Your Last.FM Artists


+

Import Last.FM Artists


+ Enter the username whose artists you want to import:



+

Placeholder :-)


+

+
+ +



''' % (music_dir_text, lastfm_user_text)) + Force Post-Process Albums in Download Folder


+ Check for Headphones Updates


''' % (music_dir_input, lastfm_user_text)) page.append(templates._footer % headphones.CURRENT_VERSION) return page manage.exposed = True @@ -449,6 +457,13 @@ class WebInterface(object): raise cherrypy.HTTPRedirect("home") forceSearch.exposed = True + def forcePostProcess(self): + from headphones import postprocessor + threading.Thread(target=postprocessor.forcePostProcess).start() + time.sleep(5) + raise cherrypy.HTTPRedirect("home") + forcePostProcess.exposed = True + def checkGithub(self): from headphones import versioncheck versioncheck.checkGithub() diff --git a/lib/beets/mediafile.py b/lib/beets/mediafile.py index 588434f2..c9e7b0b2 100644 --- a/lib/beets/mediafile.py +++ b/lib/beets/mediafile.py @@ -790,16 +790,14 @@ class MediaFile(object): albumartist = MediaField( mp3 = StorageStyle('TPE2'), mp4 = StorageStyle( - '----:com.apple.iTunes:Album Artist', - as_type=str), + '----:com.apple.iTunes:Album Artist'), etc = [StorageStyle('album artist'), StorageStyle('albumartist')] ) albumtype = MediaField( mp3 = StorageStyle('TXXX', id3_desc=u'MusicBrainz Album Type'), mp4 = StorageStyle( - '----:com.apple.iTunes:MusicBrainz Album Type', - as_type=str), + '----:com.apple.iTunes:MusicBrainz Album Type'), etc = StorageStyle('musicbrainz_albumtype') )